Skip to content

Instantly share code, notes, and snippets.

@simmsy
Last active August 29, 2015 14:14
Show Gist options
  • Save simmsy/a6b94cd5ca802a0bc9e6 to your computer and use it in GitHub Desktop.
Save simmsy/a6b94cd5ca802a0bc9e6 to your computer and use it in GitHub Desktop.
#https://github.com/spree/spree/blob/master/core/app/models/spree/stock/coordinator.rb#L32:L40
def build_packages(packages = Array.new)
StockLocation.active.each do |stock_location|
next unless stock_location.stock_items.where(:variant_id => inventory_units.map(&:variant_id).uniq).exists?
packer = build_packer(stock_location, inventory_units)
packages += packer.packages
end
packages
end
# refactored to
def build_packages(packages = Array.new)
# OR StockLocation.for_variant_ids(inventory_units.map(&:variant_id).uniq)
StockLocation.active.joins(:stock_items).merge(StockItems.where(:variant_id => inventory_units.map(&:variant_id).uniq).each do |stock_location|
packer = build_packer(stock_location, inventory_units)
packages += packer.packages
end
packages
end
@RhysStansfield
Copy link

To summarize and tidy up this would be what we'd need:

class Spree::StockLocation
  scope :for_variant_ids, ->(variant_ids) { active.joins(:stock_items).merge(StockItems.where(variant_id: variant_ids) }
end

class Spree::Stock::Coordinator
  def build_packages(packages = Array.new)
    Spree::StockLocation.for_variant_ids(inventory_units.map(&:variant_id).uniq).each do |stock_location|
      packer = build_packer(stock_location, inventory_units)
      packages += packer.packages
    end
    packages
  end
end

Do you want me to do this directly in our customized version of Spree or do it against master and open a PR then backport to ours?

@simmsy
Copy link
Author

simmsy commented Feb 5, 2015

Yes mate, add to core please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment