From bc6a0ba10ab1528323c8dfd51fca9433a9a126d0 Mon Sep 17 00:00:00 2001 From: Jordan Brough Date: Sat, 19 Dec 2015 00:17:41 -0700 Subject: [PATCH] Refactor stock item lookup a bit - add stock_item_for - use to_h instead of each_with_object - remove the order(:id) since we have db-level uniqueness now --- core/app/models/spree/stock/packer.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/core/app/models/spree/stock/packer.rb b/core/app/models/spree/stock/packer.rb index 0fe39506cb2..1a24b4fd9ca 100644 --- a/core/app/models/spree/stock/packer.rb +++ b/core/app/models/spree/stock/packer.rb @@ -22,7 +22,7 @@ def default_package inventory_units.group_by(&:variant).each do |variant, variant_inventory_units| units = variant_inventory_units.clone if variant.should_track_inventory? - stock_item = stock_item_lookup[variant.id] + stock_item = stock_item_for(variant.id) next unless stock_item on_hand, backordered = stock_item.fill_status(units.count) @@ -39,18 +39,18 @@ def default_package private + def stock_item_for(variant_id) + stock_item_lookup[variant_id] + end + # Returns a lookup table in the form of: # { => , ...} def stock_item_lookup - @stock_item_lookup ||= begin - Spree::StockItem. - where(variant_id: inventory_units.map(&:variant_id).uniq). - where(stock_location_id: stock_location.id). - order(:id). - each_with_object({}) do |stock_item, hash| - hash[stock_item.variant_id] ||= stock_item - end - end + @stock_item_lookup ||= Spree::StockItem. + where(variant_id: inventory_units.map(&:variant_id).uniq). + where(stock_location_id: stock_location.id). + map { |stock_item| [stock_item.variant_id, stock_item] }. + to_h # there is only one stock item per variant in a stock location end def build_splitter