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

Several small refactors to promotions code #3416

Merged
merged 4 commits into from
Nov 20, 2019

Conversation

kennyadsl
Copy link
Member

Description
I've worked on these refactors while working on #3355, which is closed now but I think these small preliminary changes could still be beneficial for the codebase.

Checklist:

The previous specs file was actually testing the main
Spree::Calculator::PriceSack instead.

The allow_any_instance hack is required since while building
the package content, no matter what is the price of the variant,
the line item amount is always 10. This is due to how the factory
is defined:

https://github.com/solidusio/solidus/blob/b76c1dd67642245778c019b15b3a10ef22b1f50c/core/lib/spree/testing_support/factories/line_item_factory.rb#L9
The right method to be overridden is `compute` and
not `compute_shipment`.

Take advantage of the change to use described_class here.
We had some issues on the PriceSack specs that was testing
the wrong class. Using described_class more should avoid this
kind of issues in the future.
We don't need to check and raise if that method is not defined
since it's not used in our codebase for that kind of calculator.

This is the reason why also specs are useless.

We are able to remove this method without a deprecation warning
because it's an abstract method and, if users are overriding
it into their own calculators that inherit from this one their
application will be perfectly safe.
@kennyadsl kennyadsl added the changelog:solidus_core Changes to the solidus_core gem label Oct 30, 2019
@kennyadsl kennyadsl self-assigned this Oct 30, 2019
@kennyadsl kennyadsl changed the title Several small fixes to promotions code Several small refactors to promotions code Oct 30, 2019
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.

Looks good to me, I left a question about the stubbing, I think it would be nicer if we could remove it, but not blocking 👍

# This hack is due to our factories not being so smart to understand
# that they should create line items with the price of the associated
# variant by default.
allow_any_instance_of(Spree::Stock::ContentItem).to receive(:price) { amount }
Copy link
Member

Choose a reason for hiding this comment

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

Do you think it would make sense (be possible, simple, and useful for other scenarios) to change the factory in order to avoid this stubbing?

Copy link
Member Author

Choose a reason for hiding this comment

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

I had the same thought but I think we have to change the line item price having a dynamic price, based on the variant price. I think that this would break a lot of specs in stores that are relying on the static price of line items in our core factories, and that's why I think this is the best trade-off.

I'm open to making an attempt to create an alternative line_item factory with a dynamic price and trying to deprecate the current one. Do you think it's worth it?

Copy link
Member

@spaghetticode spaghetticode Oct 31, 2019

Choose a reason for hiding this comment

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

I think it is the normal scenario for line items to have prices bound to the ones of their variants... I would consider a price difference to be more of an edge case, so I think that factory could be later changed to reflect the more regular use case.
But of course I would not consider this an urgent matter at all. We may just open an issue and see how it goes from there.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agree, let me do that!

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.

@kennyadsl thank you 👍

@kennyadsl kennyadsl merged commit 9b6368b into solidusio:master Nov 20, 2019
@kennyadsl kennyadsl deleted the kennyadsl/promo-fixes branch November 20, 2019 21:06
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