Fixed missing group_order_article in group_order show.

Also add eager loading for associated objects.
This commit is contained in:
benni 2012-12-16 16:50:09 +01:00
parent 0fcd5abb3a
commit 9919183cb0
4 changed files with 22 additions and 17 deletions

View File

@ -25,4 +25,15 @@ module GroupOrdersHelper
'ignored' 'ignored'
end end
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 end

View File

@ -166,7 +166,7 @@ class GroupOrderArticle < ActiveRecord::Base
# Until the order is finished this will be the maximum price or # Until the order is finished this will be the maximum price or
# the minimum price depending on configuration. When the order is finished it # the minimum price depending on configuration. When the order is finished it
# will be the value depending of the article results. # 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? unless order_article.order.finished?
if FoodsoftConfig[:tolerance_is_costly] if FoodsoftConfig[:tolerance_is_costly]
order_article.article.fc_price * (quantity + tolerance) order_article.article.fc_price * (quantity + tolerance)

View File

@ -1,7 +1,6 @@
# encoding: utf-8 # encoding: utf-8
# #
class Order < ActiveRecord::Base class Order < ActiveRecord::Base
extend ActiveSupport::Memoizable # Ability to cache method results. Use memoize :expensive_method
# Associations # Associations
has_many :order_articles, :dependent => :destroy has_many :order_articles, :dependent => :destroy
@ -86,11 +85,11 @@ class Order < ActiveRecord::Base
# The array has the following form: # The array has the following form:
# e.g: [["drugs",[teethpaste, toiletpaper]], ["fruits" => [apple, banana, lemon]]] # e.g: [["drugs",[teethpaste, toiletpaper]], ["fruits" => [apple, banana, lemon]]]
def articles_grouped_by_category 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 }. group_by { |a| a.article.article_category.name }.
sort { |a, b| a[0] <=> b[0] } sort { |a, b| a[0] <=> b[0] }
end end
memoize :articles_grouped_by_category
def articles_sort_by_category def articles_sort_by_category
order_articles.all(:include => [:article], :order => 'articles.name').sort do |a,b| order_articles.all(:include => [:article], :order => 'articles.name').sort do |a,b|

View File

@ -54,26 +54,21 @@
= category_name = category_name
%i.icon-tag %i.icon-tag
%td{colspan: "9"} %td{colspan: "9"}
- for oa in order_articles - order_articles.each do |oa|
- # get the order-results for the ordergroup - # get the order-results for the ordergroup
- goa = oa.group_order_articles.find_by_group_order_id(@group_order.id) - r = get_order_results(oa, @group_order.id)
- quantity = goa.quantity %tr{class: cycle('even', 'odd', name: 'articles') + " " + order_article_class_name(r[:quantity], r[:tolerance], r[:result])}
- 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)}
%td{style: "width:40%"} %td{style: "width:40%"}
=h oa.article.name = oa.article.name
- unless oa.article.note.blank? - 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();"}) = 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= "#{oa.price.unit_quantity} x #{oa.article.unit}"
%td= number_to_currency(oa.price.fc_price) %td= number_to_currency(oa.price.fc_price)
%td %td
= quantity = r[:quantity]
= "+ #{tolerance}" if oa.price.unit_quantity > 1 = "+ #{r[:tolerance]}" if oa.price.unit_quantity > 1
%td= result > 0 ? result : "0" %td= r[:result] > 0 ? r[:result] : "0"
%td= number_to_currency(sub_total) %td= number_to_currency(r[:sub_total])
- unless oa.article.note.blank? - unless oa.article.note.blank?
%tr{id: "note_#{oa.id}", class: "note even", style: "display:none"} %tr{id: "note_#{oa.id}", class: "note even", style: "display:none"}
%td{colspan: "6"}=h oa.article.note %td{colspan: "6"}=h oa.article.note