From 9919183cb0cab2af3deeb35fbe7122a8d091bb9a Mon Sep 17 00:00:00 2001 From: benni Date: Sun, 16 Dec 2012 16:50:09 +0100 Subject: [PATCH] Fixed missing group_order_article in group_order show. Also add eager loading for associated objects. --- app/helpers/group_orders_helper.rb | 11 +++++++++++ app/models/group_order_article.rb | 2 +- app/models/order.rb | 5 ++--- app/views/group_orders/show.html.haml | 21 ++++++++------------- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/app/helpers/group_orders_helper.rb b/app/helpers/group_orders_helper.rb index d1c40cc4..b7707744 100644 --- a/app/helpers/group_orders_helper.rb +++ b/app/helpers/group_orders_helper.rb @@ -25,4 +25,15 @@ module GroupOrdersHelper 'ignored' end end + + def get_order_results(order_article, group_order_id) + goa = order_article.group_order_articles.detect { |goa| goa.group_order_id == group_order_id } + quantity, tolerance, result, sub_total = if goa.present? + [goa.quantity, goa.tolerance, goa.result, goa.total_price(order_article)] + else + [0, 0, 0, 0] + end + + {group_order_article: goa, quantity: quantity, tolerance: tolerance, result: result, sub_total: sub_total} + end end \ No newline at end of file diff --git a/app/models/group_order_article.rb b/app/models/group_order_article.rb index 63624bb5..11f3b447 100644 --- a/app/models/group_order_article.rb +++ b/app/models/group_order_article.rb @@ -166,7 +166,7 @@ class GroupOrderArticle < ActiveRecord::Base # Until the order is finished this will be the maximum price or # the minimum price depending on configuration. When the order is finished it # will be the value depending of the article results. - def total_price + def total_price(order_article = self.order_article) unless order_article.order.finished? if FoodsoftConfig[:tolerance_is_costly] order_article.article.fc_price * (quantity + tolerance) diff --git a/app/models/order.rb b/app/models/order.rb index 050e2a60..31276a48 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -1,7 +1,6 @@ # encoding: utf-8 # class Order < ActiveRecord::Base - extend ActiveSupport::Memoizable # Ability to cache method results. Use memoize :expensive_method # Associations has_many :order_articles, :dependent => :destroy @@ -86,11 +85,11 @@ class Order < ActiveRecord::Base # The array has the following form: # e.g: [["drugs",[teethpaste, toiletpaper]], ["fruits" => [apple, banana, lemon]]] def articles_grouped_by_category - order_articles.includes(:article, :article_price).order('articles.name'). + order_articles.includes([:article_price, :group_order_articles, :article => :article_category]). + order('articles.name'). group_by { |a| a.article.article_category.name }. sort { |a, b| a[0] <=> b[0] } end - memoize :articles_grouped_by_category def articles_sort_by_category order_articles.all(:include => [:article], :order => 'articles.name').sort do |a,b| diff --git a/app/views/group_orders/show.html.haml b/app/views/group_orders/show.html.haml index 5c38b955..10b93c7c 100644 --- a/app/views/group_orders/show.html.haml +++ b/app/views/group_orders/show.html.haml @@ -54,26 +54,21 @@ = category_name %i.icon-tag %td{colspan: "9"} - - for oa in order_articles + - order_articles.each do |oa| - # get the order-results for the ordergroup - - goa = oa.group_order_articles.find_by_group_order_id(@group_order.id) - - quantity = goa.quantity - - tolerance = goa.tolerance - - result = goa.result - - sub_total = goa.total_price - - total += sub_total - %tr{class: cycle('even', 'odd', name: 'articles') + " " + order_article_class_name(quantity, tolerance, result)} + - r = get_order_results(oa, @group_order.id) + %tr{class: cycle('even', 'odd', name: 'articles') + " " + order_article_class_name(r[:quantity], r[:tolerance], r[:result])} %td{style: "width:40%"} - =h oa.article.name + = oa.article.name - unless oa.article.note.blank? = image_tag("lamp_grey.png", {alt: "Notiz anzeigen", size: "15x16", border: "0", onmouseover: "$('note_#{oa.id}').show();", onmouseout: "$('note_#{oa.id}').hide();"}) %td= "#{oa.price.unit_quantity} x #{oa.article.unit}" %td= number_to_currency(oa.price.fc_price) %td - = quantity - = "+ #{tolerance}" if oa.price.unit_quantity > 1 - %td= result > 0 ? result : "0" - %td= number_to_currency(sub_total) + = r[:quantity] + = "+ #{r[:tolerance]}" if oa.price.unit_quantity > 1 + %td= r[:result] > 0 ? r[:result] : "0" + %td= number_to_currency(r[:sub_total]) - unless oa.article.note.blank? %tr{id: "note_#{oa.id}", class: "note even", style: "display:none"} %td{colspan: "6"}=h oa.article.note