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

Avoid methods other than update! on OrderUpdater #1689

Merged
merged 14 commits into from
Mar 21, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Avoid Updater method in ensure_promotions_eligible
Instead of using a (soon to be) private method in OrderUpdater, this
method now does what is asked of it. Tests adjustments one-by-one to
ensure that they remain eligible.
  • Loading branch information
jhawthorn committed Mar 21, 2017
commit e706293879ff9165a7b0fa7ab31bcb6ea46f71de
14 changes: 13 additions & 1 deletion core/app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ def update!
self.amount = source.compute_amount(adjustable)

if promotion?
self.eligible = source.promotion.eligible?(adjustable, promotion_code: promotion_code)
self.eligible = calculate_eligibility
end

# Persist only if changed
@@ -116,6 +116,18 @@ def update!
amount
end

# Calculates based on attached promotion (if this is a promotion
# adjustment) whether this promotion is still eligible.
# @api private
# @return [true,false] Whether this adjustment is eligible
def calculate_eligibility
if !finalized? && source && promotion?
source.promotion.eligible?(adjustable, promotion_code: promotion_code)
else
eligible?
end
end

private

def require_promotion_code?
7 changes: 5 additions & 2 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
@@ -794,9 +794,12 @@ def ensure_inventory_units
end

def ensure_promotions_eligible
updater.update_adjustment_total
if promo_total_changed?
adjustment_changed = all_adjustments.eligible.promotion.any? do |adjustment|
!adjustment.calculate_eligibility
end
if adjustment_changed
restart_checkout_flow
update!
errors.add(:base, Spree.t(:promotion_total_changed_before_complete))
end
errors.empty?
4 changes: 4 additions & 0 deletions core/spec/models/spree/promotion_code_spec.rb
Original file line number Diff line number Diff line change
@@ -164,21 +164,25 @@
order.complete!
end
end

it "makes the promotion ineligible" do
expect{
order.complete
}.to change{ promo_adjustment.reload.eligible }.to(false)
end

it "adjusts the promo_total" do
expect{
order.complete
}.to change(order, :promo_total).by(10)
end

it "increases the total to remove the promo" do
expect{
order.complete
}.to change(order, :total).from(30).to(40)
end

it "resets the state of the order" do
expect{
order.complete