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 4 commits into
base: main
Choose a base branch
from

Conversation

Noah-Silvera
Copy link
Contributor

It's very useful that the simple_coordinator can have so many of it's internal classes configured. However, they can only be configured for one set of hardcoded behavior, based on the two input values, an order and inventory_units

If you want the coordinator to behave differently in different scenarios e.g, the admin and the frontend, then you have to start getting creative. The simple_coordinator (and all it's configured classes) in their current state can only react to the state of the order and inventory_units argument, or they can react to globally set state (which is not a great pattern).

Currently, the simple_coordinator is only called in two places in Solidus: during exchanges, and during creating proprosed shipments. However, it is reasonable for a complicated store to want to build shipments in other scenarios.

One workaround to getting the coordinator to behave differently in these different locations is to include any arguments that you want to pass to the coordinator on the order as an attribute or database column, and then read the order attribute in your configured custom class. However, this isn't even a perfect workaround, because not every configurable class is passed the order (e.g. the location_sorter_class). To truly have the coordinator behave differently in different locations you need to do minor to extensive monkey patching

This solution addresses the problem by allowing generic options to be passed to the simple coordinator, which are then passed down to each configurable class. This means that any caller of the simple_coordinator can customize the behavior it wants through these options and overriding the configurable inner classes.

This for example, allows for customizations like shipment building behavior that is specific to the admin, where a admin user could be allowed to rebuild shipments with a stock location that is not normally available to users.

Summary

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

The following are not always needed:

  • 📖 I have updated the README to account for my changes.
  • 📑 I have documented new code with YARD.
  • 🛣️ I have opened a PR to update the guides.
  • ✅ I have added automated tests to cover my changes.
  • 📸 I have attached screenshots to demo visual changes.

@github-actions github-actions bot added the changelog:solidus_core Changes to the solidus_core gem label Sep 17, 2024
@Noah-Silvera Noah-Silvera force-pushed the supergood/simple-coordinator-extensibility-improvement branch 2 times, most recently from a724bd4 to 94b7875 Compare September 17, 2024 17:19
@jarednorman
Copy link
Member

I think this makes a lot of sense. Did you consider any other approaches?

@Noah-Silvera
Copy link
Contributor Author

Noah-Silvera commented Sep 18, 2024

I think this makes a lot of sense. Did you consider any other approaches?

@jarednorman yes! I can summarize them briefly

Using dependency injection to inject (optionally) the configurable classes

This was a solution we explored that would change the method signature of the simple_coordinator to allow dependency injection for all of it's configurable classes

e.g.

class SimpleCoordinator
	...
	def initialize(order, inventory_units = nil, inventory_unit_builder: nil, stock_location_filter: nil, stock_location_sorter: nil, allocator: nil, ...)
		...
		(inventory_unit_builder || Spree::Config.stock.inventory_unit_builder_class.new(order)).units
		...
	end
	...
end

This accomplishes the same goal of allowing users to change the behavior of the simple_coordinator on a case by case basis, in this case, by passing instance classes they build for each configurable dependency.

This would be a non-breaking change to the simple_coordinator interface, but we felt it went against the existing paradigm of using classes that you can configure and was mixing two patterns. Definitely a viable option though!

@benjaminwil do you remember the downsides to this pattern?

Fully non-breaking change - replacing the simple coordinator and dependency classes

For a fully-non-breaking change for existing users, we could create new base classes for each of the configurable base classes we touch in this change, and replace them in the default config. User's would have the option of switching over to them on upgrade or not. This felt like significant overhead to be worth it for preventing a breaking change that we think will only affect a minority of users.

One problem with this solution is that we would have to use reflection at some point in the code (where we are calling the simple coordinator I think) to determine if we should pass options to the coordinator or not

e.g.

# new BaseClass
module Spree
  module Stock
    module LocationFilter
      class BaseWithOptions
        def initialize(stock_locations, order, options: {})
          @stock_locations = stock_locations
          @order = order
          @options = options
        end
      end
    end
  end
end

# old BaseClass
module Spree
  module Stock
    module LocationFilter
      class BaseWithOptions
        def initialize(stock_locations, order, options: {})
          @stock_locations = stock_locations
          @order = order
          @options = options
        end
      end
    end
  end
end

# new Default Class
module Spree
  module Stock
    module LocationFilter
      class ActiveWithOptions
        def initialize(stock_locations, order, options: {})
          ...
        end
      end
    end
  end
end

# Simple Coordinator

class SimpleCoordinatorWithOptions
	...
	def initialize(order, inventory_units = nil, coordinator_options: {})
		...
		(inventory_unit_builder || Spree::Config.stock.inventory_unit_builder_class.new(order, coordinator_options:)).units
		...
	end
	...
end

# New default in core/stock_configuration
def coordinator_class
  @coordinator_class ||= '::Spree::Stock::SimpleCoordinatorWithOptions'
  @coordinator_class.constantize
end

...

def location_filter_class
  @location_filter_class ||= '::Spree::Stock::LocationFilter::Active'
  @location_filter_class.constantize
end

def location_filter_class
  @location_filter_class ||= '::Spree::Stock::LocationFilter::Active'
  @location_filter_class.constantize
end

# This line would be added to an initializer when running 'install' after the solidus upgrade, with a comment to delete it
# If you want to use the new base filter. We could then remove the old base classes in a major version release (or not!)

config.stock.location_filter_class = 'Spree::Stock::LocationFilter::Active'
config.stock.coordinator_class = 'Spree::Stock::SimpleCoordinatorWithOptions'

# Some place where we call the coordinator
if Spree::Config.stock.coordinator_class.instance_method(:initialize).parameters.map(&:last).include?(:coordinator_options)
  ...
else
 ..
end

@benjaminwil do you remember any other solutions we considered?

@benjaminwil
Copy link
Contributor

No, I don't think we considered anything else. Great summary!

It's very useful that the simple_coordinator can have so many of
it's internal classes configured. However, they can only be configured
for one set of hardcoded behavior, based on the two input values, an
order and inventory_units

If you want the coordinator to behave differently in different scenarios
e.g, the admin and the frontend, then you have to start getting creative.
The simple_coordinator (and all it's configured classes) in their current
state can only react to the state of the order and inventory_units argument,
or they can react to globally set state (which is not a great pattern).

Currently, the simple_coordinator is only called in two places in Solidus:
during exchanges, and during creating proprosed shipments. However, it is
reasonable for a complicated store to want to build shipments in other scenarios.

One workaround to getting the coordinator to behave differently in these different
locations is to include any arguments that you want to pass to the coordinator on
the order as an attribute or database column, and then read the order attribute
in your configured custom class. However, this isn't even a perfect workaround,
because not every configurable class is passed the order (e.g. the
location_sorter_class). To truly have the coordinator behave differently in different
locations you need to do minor to extensive monkey patching

This solution addresses the problem by allowing generic options to be passed to the
simple coordinator, which are then passed down to each configurable class. This means
that any caller of the simple_coordinator can customize the behavior it wants through
these options and overriding the configurable inner classes.

This for example, allows for customizations like shipment building behavior that is
specific to the admin, where a admin user could be allowed to rebuild shipments with
a stock location that is not normally available to users.

This is however a **breaking change** for certain consumers of Solidus. Since this
change adds an argument to the constructor of the following classes, if a consumer of
solidus has a.) configured their own custom class and b.) overrode the constructor of their
own custom class then their custom class could break. However, this error will cause shipment
building to fail, so it should be very obvious to spot and correct. Additionally, there
were few reasons to override the constructor of these configurable classes unless you had also
overridden the simple_coordinator, as you did not have control of the arguments passed to these
configurable classes.

`inventory_unit_builder_class`
`location_filter_class`
`location_sorter_class`
`allocator_class`
`estimator_class`

e.g. a custom configured stock location filter like this would be broken

class StockLocationFilter < Spree::Stock::LocationFilter::Base
  def initialize(stock_locations, order)
    super(stock_locations, order)
    @some_variable = 'Some initializer'
  end

  ...
end

However, initializers like this will be fine, as they implicitly
pass the original arguments:

class StockLocationFilter < Spree::Stock::LocationFilter::Base
  def initialize(_stock_locations, order)
    super
    @my_variable = 'Some Value'
  end
  ...
end

Co-authored-by: Benjamin Willems <[email protected]>
@benjaminwil benjaminwil force-pushed the supergood/simple-coordinator-extensibility-improvement branch from 94b7875 to cd5b4be Compare October 16, 2024 17:42
benjaminwil and others added 3 commits October 29, 2024 09:57
This example would "pass" regardless of whether we used an quality check
or unequality check. We need to `expect` here.

Co-authored-by: Noah Silvera <[email protected]>
We identified that many of these simple coordinator tests exercise all
of their dependencies. We think communicating that these are integration
tests is helpful.

We have not otherwise refactored or modified the tests's contents.
This commit ensures that the simple coordinator and a custom estimator
work when custom coordinator options are passed in.

Co-authored-by: Noah Silvera <[email protected]>
@benjaminwil benjaminwil force-pushed the supergood/simple-coordinator-extensibility-improvement branch from 7900a2f to b4eed42 Compare October 29, 2024 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_core Changes to the solidus_core gem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants