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

Make more stock classes configurable #4395

Merged

Conversation

jarednorman
Copy link
Member

@jarednorman jarednorman commented May 30, 2022

Description

This makes three additional stock classes configurable, so that they can be overridden by extensions like solidus_product_assembly or any stores that need similar functionality:

  • Spree::Stock::InventoryUnitBuilder
  • Spree::Stock::InventoryValidator
  • Spree::Stock::AvailabilityValidator

I think there's probably more (breaking) work to be done to improve how this system works and better support the use case of having line items that map to inventory units with different quantities or variants, but this is a good first step.

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

Copy link
Member

@spaghetticode spaghetticode 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, thanks @jarednorman !

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 Jared! Approved and left some suggestions to try (if not done already) to avoid stubbing the preference manually. Let me know if those are valid or if I am missing something.

core/spec/models/spree/order_spec.rb Outdated Show resolved Hide resolved
@jarednorman jarednorman force-pushed the jared/likes-configurable-classes branch from 2483b46 to bcb06e3 Compare May 31, 2022 18:05
@jarednorman
Copy link
Member Author

Only change here is that I applied @kennyadsl's suggestion for stubbing the preferences.

Copy link
Contributor

@waiting-for-dev waiting-for-dev left a comment

Choose a reason for hiding this comment

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

Thanks, @jarednorman. It's always nice having more flexibility. I left some suggestions.

core/spec/models/spree/order_spec.rb Outdated Show resolved Hide resolved
@jarednorman jarednorman force-pushed the jared/likes-configurable-classes branch from bcb06e3 to 3300cf9 Compare June 2, 2022 21:36
@jarednorman jarednorman changed the title Make the inventory unit builder configurable Make more stock classes configurable Jun 2, 2022
@jarednorman jarednorman force-pushed the jared/likes-configurable-classes branch from 3300cf9 to 0b4eebb Compare June 2, 2022 21:43
@jarednorman
Copy link
Member Author

jarednorman commented Jun 2, 2022

I've updated the description and added two additional commits to hook into the other two spots where solidus_product_assembly (which I'm not actually using... but it's a good reference) overrides all the methods on an object.

@kennyadsl
Copy link
Member

@waiting-for-dev can we merge this?

@jarednorman
Copy link
Member Author

jarednorman commented Jul 6, 2022

Needs documentation update.

@jarednorman
Copy link
Member Author

I've opened up a change request in Git Book to explain the use of these endpoints.

@jarednorman jarednorman force-pushed the jared/likes-configurable-classes branch from 3ce8dc4 to b41f7ce Compare July 6, 2022 22:02
@jarednorman
Copy link
Member Author

In writing the documentation I discovered that the commit that made the inventory validator configurable actually hadn't; it had added the configuration option, but it had neither used it, nor even attempted to assert that it the configured value was used.

I've now fixed that and rebased this. Assuming tests pass I believe this should be ready to merge after it receives reviews.

This adds the inventory unit builder (Spree::Stock::InventoryUnitBuilder
by default) to the Spree::Config.stock configuration, so that it can be
overridden.

This is desirable for any store that doesn't have a one-to-one mapping
between purchased variants and the fulfilled inventory units. The
extension solidus_product_assembly heavily overrides this class to
accomplish this, so with this change it will be able to stop decorating
Spree::Stock::InventoryUnitBuilder and instead provide its own.

I also reworked the stock configuration specs a bit. I fixed the
repeated typo, simplified the structure slightly, and ensured they don't
leak constants anymore.
This makes the Spree::Stock::AvailabilityValidator swappable.

I need to justify using `send` in that test: this method is called by
the order state machine, itself a swappable component. I could imagine
making it a public method, but I don't want to increase the surface area
of Spree::Order's API. Instead, I think it's useful to test this method
in isolation (i.e. separately from the state machine) with the
assumption that it is part of the API that the configured state machine
will call.

I've not added tests for the change to the checkout controller in the
frontend... because it's too hard. The configured class already gets
called a bunch just in setting up these tests, so trying to write a test
that cares that it gets called with the right arguments at that
particular moment did not seem worth the effort given we're deprecated
frontend. Hopefully we can do something better in
solidus_starter_frontend, where we also need to care about whether we're
on a Solidus version that supports this configuration option.
Similar to the two parent commits, I'm making more of the stock-related
classes in Solidus configurable to better support functionality like
solidus_product_assembly provides.

Also like the previous commit, I've chosen to unit test a private method
on Spree::Order because I think it warrants being tested independently
of the order state machine (the only place that calls this method).
This ensures that even if the specs fail, they don't leak constants.
@jarednorman jarednorman force-pushed the jared/likes-configurable-classes branch from b41f7ce to 244c5c0 Compare July 7, 2022 19:38
@jarednorman
Copy link
Member Author

I've implemented @waiting-for-dev's remaining suggestion now.

Copy link
Contributor

@waiting-for-dev waiting-for-dev left a comment

Choose a reason for hiding this comment

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

Thanks, @jarednorman! ❤️

@waiting-for-dev waiting-for-dev merged commit df51df6 into solidusio:master Jul 8, 2022
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.

6 participants