diff --git a/core/app/models/spree/stock/allocator/base.rb b/core/app/models/spree/stock/allocator/base.rb index b724d6d8bdf..4aef1122bee 100644 --- a/core/app/models/spree/stock/allocator/base.rb +++ b/core/app/models/spree/stock/allocator/base.rb @@ -4,10 +4,11 @@ module Spree module Stock module Allocator class Base - attr_reader :availability + attr_reader :availability, :coordinator_options - def initialize(availability) + def initialize(availability, coordinator_options: {}) @availability = availability + @coordinator_options = coordinator_options end def allocate_inventory(_desired) diff --git a/core/app/models/spree/stock/estimator.rb b/core/app/models/spree/stock/estimator.rb index bb992f0116b..e1f19112c10 100644 --- a/core/app/models/spree/stock/estimator.rb +++ b/core/app/models/spree/stock/estimator.rb @@ -6,6 +6,10 @@ class Estimator class ShipmentRequired < StandardError; end class OrderRequired < StandardError; end + def initialize(coordinator_options: {}) + @coordinator_options = coordinator_options + end + # Estimate the shipping rates for a package. # # @param package [Spree::Stock::Package] the package to be shipped diff --git a/core/app/models/spree/stock/inventory_unit_builder.rb b/core/app/models/spree/stock/inventory_unit_builder.rb index 342adc0d258..ce813cefd0e 100644 --- a/core/app/models/spree/stock/inventory_unit_builder.rb +++ b/core/app/models/spree/stock/inventory_unit_builder.rb @@ -3,8 +3,9 @@ module Spree module Stock class InventoryUnitBuilder - def initialize(order) + def initialize(order, coordinator_options: {}) @order = order + @coordinator_options = coordinator_options end def units diff --git a/core/app/models/spree/stock/location_filter/base.rb b/core/app/models/spree/stock/location_filter/base.rb index e85246030ef..e1490e4847a 100644 --- a/core/app/models/spree/stock/location_filter/base.rb +++ b/core/app/models/spree/stock/location_filter/base.rb @@ -12,7 +12,7 @@ class Base # @!attribute [r] stock_locations # @return [Enumerable] # a collection of locations to sort - attr_reader :stock_locations + attr_reader :stock_locations, :coordinator_options # @!attribute [r] order # @return @@ -25,9 +25,12 @@ class Base # a collection of locations to sort # @param order # the order we are creating the shipment for - def initialize(stock_locations, order) + # @param coordinator_options [Hash] + # a set of options passed from the stock_coordinator + def initialize(stock_locations, order, coordinator_options: {}) @stock_locations = stock_locations @order = order + @coordinator_options = coordinator_options end # Filter the stock locations. diff --git a/core/app/models/spree/stock/location_sorter/base.rb b/core/app/models/spree/stock/location_sorter/base.rb index 821f34b1930..bab46ba3734 100644 --- a/core/app/models/spree/stock/location_sorter/base.rb +++ b/core/app/models/spree/stock/location_sorter/base.rb @@ -15,14 +15,17 @@ class Base # @!attribute [r] stock_locations # @return [Enumerable] # a collection of locations to sort - attr_reader :stock_locations + attr_reader :stock_locations, :coordinator_options # Initializes the stock location sorter. # # @param stock_locations [Enumerable] # a collection of locations to sort - def initialize(stock_locations) + # @param coordinator_options [Hash] + # a set of options passed from the stock_coordinator + def initialize(stock_locations, coordinator_options: {}) @stock_locations = stock_locations + @coordinator_options = coordinator_options end # Sorts the stock locations. diff --git a/core/app/models/spree/stock/simple_coordinator.rb b/core/app/models/spree/stock/simple_coordinator.rb index fa5a11308aa..b2735b34df4 100644 --- a/core/app/models/spree/stock/simple_coordinator.rb +++ b/core/app/models/spree/stock/simple_coordinator.rb @@ -25,16 +25,17 @@ class SimpleCoordinator # @api private attr_reader :inventory_units, :splitters, :stock_locations, :filtered_stock_locations, :inventory_units_by_variant, :desired, - :availability, :allocator, :packages + :availability, :allocator, :packages, :coordinator_options - def initialize(order, inventory_units = nil) + def initialize(order, inventory_units = nil, coordinator_options: {}) @order = order + @coordinator_options = coordinator_options @inventory_units = - inventory_units || Spree::Config.stock.inventory_unit_builder_class.new(order).units + inventory_units || Spree::Config.stock.inventory_unit_builder_class.new(order, coordinator_options:).units @splitters = Spree::Config.environment.stock_splitters - @filtered_stock_locations = Spree::Config.stock.location_filter_class.new(load_stock_locations, order).filter - sorted_stock_locations = Spree::Config.stock.location_sorter_class.new(filtered_stock_locations).sort + @filtered_stock_locations = Spree::Config.stock.location_filter_class.new(load_stock_locations, order, coordinator_options:).filter + sorted_stock_locations = Spree::Config.stock.location_sorter_class.new(filtered_stock_locations, coordinator_options:).sort @stock_locations = sorted_stock_locations @inventory_units_by_variant = @inventory_units.group_by(&:variant) @@ -44,7 +45,7 @@ def initialize(order, inventory_units = nil) stock_locations: stock_locations ) - @allocator = Spree::Config.stock.allocator_class.new(availability) + @allocator = Spree::Config.stock.allocator_class.new(availability, coordinator_options:) end def shipments @@ -69,7 +70,7 @@ def build_shipments # Turn the Stock::Packages into a Shipment with rates packages.map do |package| shipment = package.shipment = package.to_shipment - shipment.shipping_rates = Spree::Config.stock.estimator_class.new.shipping_rates(package) + shipment.shipping_rates = Spree::Config.stock.estimator_class.new(coordinator_options:).shipping_rates(package) shipment end end diff --git a/core/spec/models/spree/stock/simple_coordinator_spec.rb b/core/spec/models/spree/stock/simple_coordinator_spec.rb index 02a245b1288..f3ba604f347 100644 --- a/core/spec/models/spree/stock/simple_coordinator_spec.rb +++ b/core/spec/models/spree/stock/simple_coordinator_spec.rb @@ -11,22 +11,26 @@ module Stock describe "#shipments" do it 'uses the pluggable estimator class' do - expect(Spree::Config.stock).to receive(:estimator_class).and_call_original + expect(Spree::Config.stock.estimator_class).to receive(:new).with(coordinator_options: {}).and_call_original + subject.shipments end it 'uses the configured stock location filter' do - expect(Spree::Config.stock).to receive(:location_filter_class).and_call_original + expect(Spree::Config.stock.location_filter_class).to receive(:new).with(anything, anything, coordinator_options: {}).and_call_original + subject.shipments end it 'uses the configured stock location sorter' do - expect(Spree::Config.stock).to receive(:location_sorter_class).and_call_original + expect(Spree::Config.stock.location_sorter_class).to receive(:new).with(anything, coordinator_options: {}).and_call_original + subject.shipments end it 'uses the pluggable allocator class' do - expect(Spree::Config.stock).to receive(:allocator_class).and_call_original + expect(Spree::Config.stock.allocator_class).to receive(:new).with(anything, coordinator_options: {}).and_call_original + subject.shipments end @@ -39,9 +43,7 @@ module Stock end it 'uses the pluggable inventory unit builder class' do - expect(Spree::Config.stock) - .to receive(:inventory_unit_builder_class) - .and_call_original + expect(Spree::Config.stock.inventory_unit_builder_class).to receive(:new).with(anything, coordinator_options: {}).and_call_original subject.shipments end