Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass options to coordinator dependencies #5854

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
5 changes: 3 additions & 2 deletions core/app/models/spree/stock/allocator/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions core/app/models/spree/stock/estimator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +29,8 @@ def shipping_rates(package, frontend_only = true)

private

attr_reader :coordinator_options

def choose_default_shipping_rate(shipping_rates)
unless shipping_rates.empty?
default_shipping_rate = Spree::Config.shipping_rate_selector_class.new(shipping_rates).find_default
Expand Down
5 changes: 4 additions & 1 deletion core/app/models/spree/stock/inventory_unit_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +21,8 @@ def missing_units_for_line_item(line_item)

private

attr_reader :coordinator_options

def build_units(line_item, quantity)
Array.new(quantity) do
Spree::InventoryUnit.new(
Expand Down
7 changes: 5 additions & 2 deletions core/app/models/spree/stock/location_filter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Base
# @!attribute [r] stock_locations
# @return [Enumerable<Spree::StockLocation>]
# a collection of locations to sort
attr_reader :stock_locations
attr_reader :stock_locations, :coordinator_options

# @!attribute [r] order
# @return <Spree::Order>
Expand All @@ -25,9 +25,12 @@ class Base
# a collection of locations to sort
# @param order <Spree::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.
Expand Down
7 changes: 5 additions & 2 deletions core/app/models/spree/stock/location_sorter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ class Base
# @!attribute [r] stock_locations
# @return [Enumerable<Spree::StockLocation>]
# 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<Spree::StockLocation>]
# 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.
Expand Down
15 changes: 8 additions & 7 deletions core/app/models/spree/stock/simple_coordinator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -44,7 +45,7 @@ def initialize(order, inventory_units = nil)
stock_locations:
)

@allocator = Spree::Config.stock.allocator_class.new(availability)
@allocator = Spree::Config.stock.allocator_class.new(availability, coordinator_options:)
end

def shipments
Expand All @@ -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
Expand Down
Loading
Loading