use plain Ruby memoization to fix deprecation (closes #121)

This commit is contained in:
wvengen 2013-09-18 22:45:57 +02:00
parent 6b0146eb95
commit c7f28a3b5c
2 changed files with 41 additions and 41 deletions

View file

@ -1,6 +1,5 @@
# encoding: utf-8 # encoding: utf-8
class Article < ActiveRecord::Base class Article < ActiveRecord::Base
extend ActiveSupport::Memoizable # Ability to cache method results. Use memoize :expensive_method
# Replace numeric seperator with database format # Replace numeric seperator with database format
localize_input_of :price, :tax, :deposit localize_input_of :price, :tax, :deposit
@ -44,11 +43,12 @@ class Article < ActiveRecord::Base
# If the article is used in an open Order, the Order will be returned. # If the article is used in an open Order, the Order will be returned.
def in_open_order def in_open_order
order_articles = OrderArticle.all(:conditions => ['order_id IN (?)', Order.open.collect(&:id)]) @in_open_order ||= begin
order_article = order_articles.detect {|oa| oa.article_id == id } order_articles = OrderArticle.all(:conditions => ['order_id IN (?)', Order.open.collect(&:id)])
order_article ? order_article.order : nil order_article = order_articles.detect {|oa| oa.article_id == id }
order_article ? order_article.order : nil
end
end end
memoize :in_open_order
# Returns true if the article has been ordered in the given order at least once # Returns true if the article has been ordered in the given order at least once
def ordered_in_order?(order) def ordered_in_order?(order)

View file

@ -2,7 +2,6 @@
# The chronologically order of the Ordergroup - activity are stored in GroupOrderArticleQuantity # The chronologically order of the Ordergroup - activity are stored in GroupOrderArticleQuantity
# #
class GroupOrderArticle < ActiveRecord::Base class GroupOrderArticle < ActiveRecord::Base
extend ActiveSupport::Memoizable # Ability to cache method results. Use memoize :expensive_method
belongs_to :group_order belongs_to :group_order
belongs_to :order_article belongs_to :order_article
@ -101,54 +100,55 @@ class GroupOrderArticle < ActiveRecord::Base
# #
# See description of the ordering algorithm in the general application documentation for details. # See description of the ordering algorithm in the general application documentation for details.
def calculate_result def calculate_result
quantity = tolerance = 0 @calculate_result ||= begin
stockit = order_article.article.is_a?(StockArticle) quantity = tolerance = 0
stockit = order_article.article.is_a?(StockArticle)
# Get total # Get total
total = stockit ? order_article.article.quantity : order_article.units_to_order * order_article.price.unit_quantity total = stockit ? order_article.article.quantity : order_article.units_to_order * order_article.price.unit_quantity
logger.debug("<#{order_article.article.name}>.unitsToOrder => items ordered: #{order_article.units_to_order} => #{total}") logger.debug("<#{order_article.article.name}>.unitsToOrder => items ordered: #{order_article.units_to_order} => #{total}")
if (total > 0) if (total > 0)
# In total there are enough units ordered. Now check the individual result for the ordergroup (group_order). # In total there are enough units ordered. Now check the individual result for the ordergroup (group_order).
# #
# Get all GroupOrderArticleQuantities for this OrderArticle... # Get all GroupOrderArticleQuantities for this OrderArticle...
order_quantities = GroupOrderArticleQuantity.all( order_quantities = GroupOrderArticleQuantity.all(
:conditions => ["group_order_article_id IN (?)", order_article.group_order_article_ids], :order => 'created_on') :conditions => ["group_order_article_id IN (?)", order_article.group_order_article_ids], :order => 'created_on')
logger.debug("GroupOrderArticleQuantity records found: #{order_quantities.size}") logger.debug("GroupOrderArticleQuantity records found: #{order_quantities.size}")
# Determine quantities to be ordered... # Determine quantities to be ordered...
total_quantity = i = 0 total_quantity = i = 0
while (i < order_quantities.size && total_quantity < total)
q = (order_quantities[i].quantity <= total - total_quantity ? order_quantities[i].quantity : total - total_quantity)
total_quantity += q
if (order_quantities[i].group_order_article_id == self.id)
logger.debug("increasing quantity by #{q}")
quantity += q
end
i += 1
end
# Determine tolerance to be ordered...
if (total_quantity < total)
logger.debug("determining additional items to be ordered from tolerance")
i = 0
while (i < order_quantities.size && total_quantity < total) while (i < order_quantities.size && total_quantity < total)
q = (order_quantities[i].tolerance <= total - total_quantity ? order_quantities[i].tolerance : total - total_quantity) q = (order_quantities[i].quantity <= total - total_quantity ? order_quantities[i].quantity : total - total_quantity)
total_quantity += q total_quantity += q
if (order_quantities[i].group_order_article_id == self.id) if (order_quantities[i].group_order_article_id == self.id)
logger.debug("increasing tolerance by #{q}") logger.debug("increasing quantity by #{q}")
tolerance += q quantity += q
end end
i += 1 i += 1
end end
# Determine tolerance to be ordered...
if (total_quantity < total)
logger.debug("determining additional items to be ordered from tolerance")
i = 0
while (i < order_quantities.size && total_quantity < total)
q = (order_quantities[i].tolerance <= total - total_quantity ? order_quantities[i].tolerance : total - total_quantity)
total_quantity += q
if (order_quantities[i].group_order_article_id == self.id)
logger.debug("increasing tolerance by #{q}")
tolerance += q
end
i += 1
end
end
logger.debug("determined quantity/tolerance/total: #{quantity} / #{tolerance} / #{quantity + tolerance}")
end end
logger.debug("determined quantity/tolerance/total: #{quantity} / #{tolerance} / #{quantity + tolerance}") {:quantity => quantity, :tolerance => tolerance, :total => quantity + tolerance}
end end
{:quantity => quantity, :tolerance => tolerance, :total => quantity + tolerance}
end end
memoize :calculate_result
# Returns order result, # Returns order result,
# either calcualted on the fly or fetched from result attribute # either calcualted on the fly or fetched from result attribute