Skip to content

Commit

Permalink
Merge pull request #565 from jhawthorn/improve_join_in_package_genera…
Browse files Browse the repository at this point in the history
…tion

Combine stock item queries in a GROUP BY query
  • Loading branch information
jordan-brough committed Dec 8, 2015
2 parents 01a2242 + 7da65bf commit 61535f9
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions core/app/models/spree/stock/coordinator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ def build_location_configured_packages(packages = Array.new)
#
# Returns an array of Package instances
def build_packages(packages = Array.new)
stock_locations_with_requested_variants.each do |stock_location|
units_for_location = unallocated_inventory_units.select { |unit| stock_location.stock_item(unit.variant) }
requested_stock_items.group_by(&:stock_location).each do |stock_location, stock_items|
variant_ids_in_stock_location = stock_items.map(&:variant_id)
units_for_location = unallocated_inventory_units.select { |unit| variant_ids_in_stock_location.include?(unit.variant_id) }
packer = build_packer(stock_location, units_for_location)
packages += packer.packages
end
Expand All @@ -69,9 +70,8 @@ def unallocated_inventory_units
inventory_units - @preallocated_inventory_units
end

def stock_locations_with_requested_variants
Spree::StockLocation.active.joins(:stock_items).
where(spree_stock_items: { variant_id: unallocated_variant_ids }).uniq
def requested_stock_items
Spree::StockItem.where(variant_id: unallocated_variant_ids).joins(:stock_location).merge(StockLocation.active).includes(:stock_location)
end

def unallocated_variant_ids
Expand Down

0 comments on commit 61535f9

Please sign in to comment.