Skip to content

Commit 4279c1d

Browse files
AlistairNormanadammathysstewartHarmony Evangelinajarednorman
committed
Support conditional persist in promotion chooser
Previously this would update the eligible column. We now only assign the value and then save if persist is true. Co-Authored-By: Adam Mueller <adam@super.gd> Co-Authored-By: Andrew Stewart <andrew@super.gd> Co-Authored-By: Harmony Evangelina <harmony@super.gd> Co-Authored-By: Jared Norman <jared@super.gd> Co-Authored-By: Kendra Riga <kendra@super.gd> Co-Authored-By: benjamin wil <benjamin@super.gd> Co-Authored-By: Chris Todorov <chris@super.gd> Co-Authored-By: Nick Van Doorn <nick@super.gd> Co-Authored-By: Sofia Besenski <sofia@super.gd>
1 parent 2c886b3 commit 4279c1d

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

legacy_promotions/app/models/spree/promotion/order_adjustments_recalculator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def call(persist: true)
3636
select(&:eligible?).
3737
select(&:promotion?).
3838
sum(&:amount)
39+
40+
order.save! if persist
3941
order
4042
end
4143

legacy_promotions/app/models/spree/promotion_chooser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def update
1414
if best_promotion_adjustment
1515
@adjustments.select(&:eligible?).each do |adjustment|
1616
next if adjustment == best_promotion_adjustment
17-
adjustment.update_columns(eligible: false, updated_at: Time.current)
17+
adjustment.eligible = false
1818
end
1919
best_promotion_adjustment.amount
2020
else

legacy_promotions/spec/models/spree/promotion/order_adjustments_recalculator_spec.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# frozen_string_literal: true
22

33
require 'rails_helper'
4+
require 'db-query-matchers'
45

56
RSpec.describe Spree::Promotion::OrderAdjustmentsRecalculator do
6-
subject { described_class.new(order).call }
7+
subject { described_class.new(order).call(persist:) }
8+
9+
let(:persist) { true }
710

811
describe '#call ' do
912
describe 'promotion recalculation' do
@@ -44,6 +47,27 @@
4447
order.promo_total
4548
}.from(-1).to(-2)
4649
end
50+
51+
context "when persist is false" do
52+
let(:persist) { false }
53+
54+
it "does not persist changes to order" do
55+
order.promotions << create(:promotion, :with_line_item_adjustment, adjustment_rate: 2)
56+
Spree::PromotionHandler::Cart.new(order).activate
57+
58+
expect(order.promo_total).to eq -1
59+
60+
expect {
61+
subject
62+
}.not_to make_database_queries(manipulative: true)
63+
64+
expect(order.promo_total).to eq -2
65+
66+
expect { order.reload }
67+
.to change { order.promo_total }
68+
.from(-2).to(-1)
69+
end
70+
end
4771
end
4872

4973
context 'promotion chooser customization' do

0 commit comments

Comments
 (0)