Class: Spree::Stock::Coordinator

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/stock/coordinator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ Coordinator

Returns a new instance of Coordinator.



6
7
8
# File 'app/models/spree/stock/coordinator.rb', line 6

def initialize(order)
  @order = order
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



4
5
6
# File 'app/models/spree/stock/coordinator.rb', line 4

def order
  @order
end

Instance Method Details

#build_packages(packages = Array.new) ⇒ Object

Build packages as per stock location

It needs to check whether each stock location holds at least one stock item for the order. In case none is found it wouldn’t make any sense to build a package because it would be empty. Plus we avoid errors down the stack because it would assume the stock location has stock items for the given order

Returns an array of Package instances



25
26
27
28
29
30
31
32
33
# File 'app/models/spree/stock/coordinator.rb', line 25

def build_packages(packages = Array.new)
  StockLocation.active.each do |stock_location|
    next unless stock_location.stock_items.where(:variant_id => order.line_items.pluck(:variant_id)).exists?

    packer = build_packer(stock_location, order)
    packages += packer.packages
  end
  packages
end

#packagesObject



10
11
12
13
14
# File 'app/models/spree/stock/coordinator.rb', line 10

def packages
  packages = build_packages
  packages = prioritize_packages(packages)
  packages = estimate_packages(packages)
end