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

Use configurable promo adjuster in callback #5498

Merged

Conversation

mamhoff
Copy link
Contributor

@mamhoff mamhoff commented Nov 2, 2023

Summary

This deprecates Spree::Adjustment#calculate_eligibility by refactoring Spree::Order#ensure_promotions_eligible to use the configurable promotion adjuster rather than Spree::Adjustment#calculate_eligibility.

Checklist

The following are not always needed:

  • ✅ I have added automated tests to cover my changes. (Really I didn't add any because exisiting specs would catch misbehavior).

`Spree::Order#ensure_promotions_eligible` uses the
`Spree::Adjustment#calculate_eligibility` in order to determine whether
the order's promo total might change. `Spree::Adjustment` should
probably not know too much about promotion eligibility, and the error
message we add to the order also does only talks about the promo total
changing.
The default order promotion adjuster duplicates this logic, and
`calculate_eligibility` should now not be called from anywhere in the
codebase during normal operation.
@mamhoff mamhoff requested a review from a team as a code owner November 2, 2023 13:48
@github-actions github-actions bot added the changelog:solidus_core Changes to the solidus_core gem label Nov 2, 2023
!adjustment.calculate_eligibility
end
if adjustment_changed
Spree::Config.promotion_adjuster_class.new(self).call
Copy link
Member

Choose a reason for hiding this comment

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

What this class does seems pretty different from the calculate_eligibility method's content. Can you help me understand in which way they are equivalent in this context?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does check the eligibility of each adjustment in recalculate(adjustment) and especially calculate_eligibility(adjustment). The class does a little bit more - it calculates not only eligibility, but also checks whether the adjustment's sum of amount is still correct. So it actually checks the promo total that the error message indicates it should have checked.

Copy link
Member

Choose a reason for hiding this comment

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

I got it; I am a bit worried about degrading the performance of this method with the call to the promotion_adjuster_class because I guess (I didn't check) it will hit the database many more times. Considering that it only happens on the order completion, it might be a good trade-off. If you or others are not concerned about that, I'm okay with merging this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a slight performance degradation for sure. From my experience with the promotion system though, the expensive part is checking the rules for eligibility, and not (re)calculating the discounts.
Also: I fully agree that it's a tradeoff, and that it's okay because it only happens on order completion.

Copy link

codecov bot commented Nov 3, 2023

Codecov Report

Merging #5498 (36ae7f4) into main (00031a2) will not change coverage.
Report is 2 commits behind head on main.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #5498   +/-   ##
=======================================
  Coverage   88.93%   88.93%           
=======================================
  Files         622      622           
  Lines       14909    14909           
=======================================
  Hits        13259    13259           
  Misses       1650     1650           
Files Coverage Δ
core/app/models/spree/adjustment.rb 93.93% <100.00%> (+0.09%) ⬆️
core/app/models/spree/order.rb 94.63% <100.00%> (-0.02%) ⬇️

📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

@elia elia merged commit 594f479 into solidusio:main Dec 22, 2023
10 checks passed
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.

5 participants