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

Stock location sorters #2783

Merged
merged 2 commits into from
Oct 17, 2018

Conversation

aldesantis
Copy link
Member

@aldesantis aldesantis commented Jun 23, 2018

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.

@kennyadsl kennyadsl added the WIP label Jul 9, 2018
@aldesantis aldesantis force-pushed the simple-coordinator-extension branch from 1c3688e to b38e58e Compare July 10, 2018 14:05
@aldesantis
Copy link
Member Author

aldesantis commented Jul 10, 2018

Improved this with a better, configurable design that doesn't require opening and modifying SimpleCoordinator.

@aldesantis aldesantis force-pushed the simple-coordinator-extension branch 3 times, most recently from abdeaf8 to 622e2cf Compare July 10, 2018 14:13
@aldesantis aldesantis changed the title Make it easier to extend @stock_locations in SimpleCoordinator Stock location sorters Jul 10, 2018
@aldesantis aldesantis force-pushed the simple-coordinator-extension branch from 622e2cf to 788c2c8 Compare July 10, 2018 14:16
Copy link
Member

@kennyadsl kennyadsl left a 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
Copy link
Member

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?

Copy link
Member Author

@aldesantis aldesantis Jul 11, 2018

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.

Copy link
Member

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.

Copy link
Member

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.

@kennyadsl kennyadsl removed the WIP label Jul 11, 2018
@aldesantis aldesantis force-pushed the simple-coordinator-extension branch from 788c2c8 to 4943026 Compare July 12, 2018 10:56
@aldesantis
Copy link
Member Author

@kennyadsl this was documented in the guides. Please let me know how they look.

@aldesantis aldesantis force-pushed the simple-coordinator-extension branch from 4943026 to 5d555ef Compare July 12, 2018 11:18
class Application < Rails::Application
# ...

initializer 'spree.register.calculators' do |app|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why calculators?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kennyadsl fixed.

@aldesantis aldesantis force-pushed the simple-coordinator-extension branch from 5d555ef to a383c08 Compare July 12, 2018 13:14

### Switching the sorter

Once you have created the logic for the new shipping calculator, you need to
Copy link
Member

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?

@aldesantis aldesantis force-pushed the simple-coordinator-extension branch 2 times, most recently from a4192c1 to 193c89e Compare July 12, 2018 13:23
kennyadsl
kennyadsl previously approved these changes Jul 12, 2018
@aldesantis aldesantis force-pushed the simple-coordinator-extension branch 2 times, most recently from 9b53177 to 39cdde4 Compare July 12, 2018 14:37
@kennyadsl kennyadsl dismissed their stale review July 12, 2018 16:30

Ops, there are failing specs, can you take a look please?

@kennyadsl
Copy link
Member

@aldesantis can you please rebase against master here?

@aldesantis
Copy link
Member Author

@kennyadsl done.

Copy link
Member

@gmacdougall gmacdougall left a 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.

@gmacdougall
Copy link
Member

There's just a failing spec that needs to be addressed.

@aldesantis
Copy link
Member Author

@gmacdougall addressed.

Copy link
Member

@tvdeyen tvdeyen left a 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
Copy link
Member

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
Copy link
Member

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

Copy link
Contributor

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
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

location_sorter_class

Copy link
Contributor

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
Copy link
Member

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
Copy link
Member

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
Copy link
Contributor

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

Copy link
Contributor

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?

Copy link
Member Author

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.

@aldesantis
Copy link
Member Author

aldesantis commented Sep 28, 2018

@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.

@aldesantis aldesantis force-pushed the simple-coordinator-extension branch 3 times, most recently from 22cf6e0 to f2a476f Compare September 28, 2018 08:01
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

CHANGELOG.md Show resolved Hide resolved
@aldesantis aldesantis force-pushed the simple-coordinator-extension branch 2 times, most recently from df533a4 to 76db15d Compare October 12, 2018 18:21
@aldesantis
Copy link
Member Author

@tvdeyen @BenMorganIO I think this might be ready for merging?

kennyadsl
kennyadsl previously approved these changes Oct 12, 2018
@kennyadsl
Copy link
Member

@aldesantis specs failures seem related, can you please take a look?

@kennyadsl kennyadsl dismissed their stale review October 12, 2018 19:11

Specs are failing

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.
@aldesantis
Copy link
Member Author

@kennyadsl sorry about that, it should be okay now.

@kennyadsl kennyadsl merged commit 5ecf43f into solidusio:master Oct 17, 2018
@aldesantis aldesantis deleted the simple-coordinator-extension branch October 17, 2018 08:12
@aldesantis
Copy link
Member Author

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants