Skip to content

Commit

Permalink
Refactor stock item lookup a bit
Browse files Browse the repository at this point in the history
- add stock_item_for
- use to_h instead of each_with_object
- remove the order(:id) since we have db-level uniqueness now
  • Loading branch information
jordan-brough committed Dec 21, 2015
1 parent a833c25 commit bc6a0ba
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions core/app/models/spree/stock/packer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
# {<variant_id> => <stock_item>, ...}
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
Expand Down

0 comments on commit bc6a0ba

Please sign in to comment.