-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Stock location sorters #2783
Stock location sorters #2783
Conversation
1c3688e
to
b38e58e
Compare
Improved this with a better, configurable design that doesn't require opening and modifying SimpleCoordinator. |
abdeaf8
to
622e2cf
Compare
622e2cf
to
788c2c8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great change, just left some thoughts. Thanks!
@@ -23,7 +23,7 @@ def initialize(order, inventory_units = nil) | |||
@order = order | |||
@inventory_units = inventory_units || InventoryUnitBuilder.new(order).units | |||
@splitters = Spree::Config.environment.stock_splitters | |||
@stock_locations = Spree::StockLocation.active | |||
@stock_locations = Spree::Config.stock_location_sorter_class.new(Spree::StockLocation.active).sort |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use another variable or method @sorted_stock_lcoations
for that. It should also help backward compatibility if someone extended this class and was using the @stock_locations
ivar. what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kennyadsl the problem with that is if someone overrides @stock_locations
and we sort in a different variable (let's say @sorted_stock_locations
), they also have to override @sorted_stock_locations
, otherwise it will contain the result of the sorting of the original @stock_locations
variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right. One option could be creating another @active_stock_locations
ivar, use that in the whole class instead of @stock_locations
which could be deprecated with DeprecatedInstanceVariableProxy. I feel like it's too complex though, maybe a changelog line is enough here. I'd say, let's see what others think before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Talked with @gmacdougall about this. Since the default sorted is returning the same thing as before maybe no one will have unexpected behavior with this, so I'm ok leaving it as is.
788c2c8
to
4943026
Compare
@kennyadsl this was documented in the guides. Please let me know how they look. |
4943026
to
5d555ef
Compare
class Application < Rails::Application | ||
# ... | ||
|
||
initializer 'spree.register.calculators' do |app| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why calculators
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kennyadsl fixed.
5d555ef
to
a383c08
Compare
|
||
### Switching the sorter | ||
|
||
Once you have created the logic for the new shipping calculator, you need to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this text as well?
a4192c1
to
193c89e
Compare
9b53177
to
39cdde4
Compare
Ops, there are failing specs, can you take a look please?
@aldesantis can you please rebase against master here? |
39cdde4
to
93e30af
Compare
@kennyadsl done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! Thanks for the hard work on this one.
There's just a failing spec that needs to be addressed. |
93e30af
to
0ddbd8d
Compare
@gmacdougall addressed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very great work. I like the concept 👍
IMO the configuration and the module namespace should be reflecting that we are sorting stock locations, like your PR says.
@@ -23,7 +23,7 @@ def initialize(order, inventory_units = nil) | |||
@order = order | |||
@inventory_units = inventory_units || InventoryUnitBuilder.new(order).units | |||
@splitters = Spree::Config.environment.stock_splitters | |||
@stock_locations = Spree::StockLocation.active | |||
@stock_locations = Spree::Config.stock.sorter_class.new(Spree::StockLocation.active).sort |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should name this
Spree::Config.stock.location_sorter_class
|
||
module Spree | ||
module Stock | ||
module Sorter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Sorter
module should be renamed
Spree::Stock::LocationSorter::Base
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would also be really good to add some top-level docs as well.
|
||
module Spree | ||
module Stock | ||
module Sorter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
@@ -5,6 +5,7 @@ module Core | |||
class StockConfiguration | |||
attr_writer :coordinator_class | |||
attr_writer :estimator_class | |||
attr_writer :sorter_class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
location_sorter_class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can also do:
attr_writer :coordinator_class, :estimator_class, :sorter_class
@@ -15,6 +16,11 @@ def estimator_class | |||
@estimator_class ||= '::Spree::Stock::Estimator' | |||
@estimator_class.constantize | |||
end | |||
|
|||
def sorter_class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
@@ -37,4 +37,21 @@ | |||
end | |||
end | |||
end | |||
describe '#sorter_class' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing newline
module Spree | ||
module Stock | ||
module Sorter | ||
RSpec.describe DefaultFirst, type: :model do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks weird. Why not just do:
describe Spree::Stock::Sorter::DefaultFirst do
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the need for type: :model
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BenMorganIO I agree that it's weird, but this is how specs are written for the other models too (including type: :model
), so I chose to stick with the convention here.
@BenMorganIO @tvdeyen @kennyadsl I addressed the requested changes (sorry for the delay!) in separate commits so you can look at them. Let me know if it all looks good and I will squash the commits together. EDIT: Nevermind, squashed now per @kennyadsl's request. |
22cf6e0
to
f2a476f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
f2a476f
to
60ec029
Compare
df533a4
to
76db15d
Compare
@tvdeyen @BenMorganIO I think this might be ready for merging? |
@aldesantis specs failures seem related, can you please take a look? |
This makes it easier to change the order of @stock_locations (e.g. by shipping from the default location first) in SimpleCoordinator by introducing the concept of stock location sorter and ensuring the order of stock locations is preserved when allocating inventory.
76db15d
to
c0bdf54
Compare
@kennyadsl sorry about that, it should be okay now. |
🎉 |
This introduces the concept of a stock location sorter, which takes a collection of stock locations and returns them in the right order for inventory allocation.
It also updates
SimpleCoordinator#allocate_inventory
to respect the order of stock locations returned by the sorter.