From 3ab3ff2bc495f00b5800ed4bdc2a67620a4c1499 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sat, 26 Jan 2013 15:18:35 +0100 Subject: [PATCH] Minor speed improvements for group order load. --- app/models/group_order.rb | 31 +++++++++++++++++-------------- app/models/order.rb | 5 +++-- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/app/models/group_order.rb b/app/models/group_order.rb index 3a93c011..9685d3bb 100644 --- a/app/models/group_order.rb +++ b/app/models/group_order.rb @@ -38,20 +38,23 @@ class GroupOrder < ActiveRecord::Base # load prices and other stuff.... data[:order_articles] = {} - order.order_articles.each do |order_article| - data[:order_articles][order_article.id] = { - :price => order_article.article.fc_price, - :unit => order_article.article.unit_quantity, - :quantity => (new_record? ? 0 : goas[order_article.id][:quantity]), - :others_quantity => order_article.quantity - (new_record? ? 0 : goas[order_article.id][:quantity]), - :used_quantity => (new_record? ? 0 : goas[order_article.id][:quantity_result]), - :tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance]), - :others_tolerance => order_article.tolerance - (new_record? ? 0 : goas[order_article.id][:tolerance]), - :used_tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance_result]), - :total_price => (new_record? ? 0 : goas[order_article.id][:total_price]), - :missing_units => order_article.missing_units, - :quantity_available => (order.stockit? ? order_article.article.quantity_available : 0) - } + #order.order_articles.each do |order_article| + order.articles_grouped_by_category.each do |article_category, order_articles| + order_articles.each do |order_article| + data[:order_articles][order_article.id] = { + :price => order_article.article.fc_price, + :unit => order_article.article.unit_quantity, + :quantity => (new_record? ? 0 : goas[order_article.id][:quantity]), + :others_quantity => order_article.quantity - (new_record? ? 0 : goas[order_article.id][:quantity]), + :used_quantity => (new_record? ? 0 : goas[order_article.id][:quantity_result]), + :tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance]), + :others_tolerance => order_article.tolerance - (new_record? ? 0 : goas[order_article.id][:tolerance]), + :used_tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance_result]), + :total_price => (new_record? ? 0 : goas[order_article.id][:total_price]), + :missing_units => order_article.missing_units, + :quantity_available => (order.stockit? ? order_article.article.quantity_available : 0) + } + end end data diff --git a/app/models/order.rb b/app/models/order.rb index c6c4341b..32c25133 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -85,9 +85,10 @@ 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_price, :group_order_articles, :article => :article_category]). + @articles_grouped_by_category ||= 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] } end