Skip to content

Commit 765d4fd

Browse files
authored
Merge pull request #6393 from SuperGoodSoft/dont-persist-in-legacy-promotions
[legacy_promotions] Don't persist line item actions in compute_amount
2 parents 3c50325 + dc32b3b commit 765d4fd

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

legacy_promotions/app/models/spree/promotion/actions/create_quantity_adjustments.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,19 @@ def compute_amount(line_item)
7575
line_item.quantity
7676
].min
7777

78-
persist_quantity(usable_quantity, line_item)
78+
set_line_item_action_quantity(usable_quantity, line_item)
7979

8080
amount = adjustment_amount * usable_quantity
8181
[line_item.amount, amount].min * -1
8282
end
8383

8484
private
8585

86+
def create_adjustment(adjustable, order, promotion_code)
87+
return unless super
88+
save!
89+
end
90+
8691
def actionable_line_items(order)
8792
order.line_items.select do |item|
8893
promotion.line_item_actionable? order, item
@@ -97,17 +102,16 @@ def total_applicable_quantity(line_items)
97102
end
98103

99104
def total_used_quantity(line_items)
100-
line_item_actions.where(
101-
line_item_id: line_items.map(&:id)
102-
).sum(:quantity)
105+
line_item_actions.select { |line_item_action|
106+
line_items.map(&:id).include? line_item_action.line_item_id
107+
}.sum(&:quantity)
103108
end
104109

105-
def persist_quantity(quantity, line_item)
110+
def set_line_item_action_quantity(quantity, line_item)
106111
line_item_action = line_item_actions.where(
107112
line_item_id: line_item.id
108113
).first_or_initialize
109114
line_item_action.quantity = quantity
110-
line_item_action.save!
111115
end
112116

113117
##

legacy_promotions/spec/models/spree/promotion/actions/create_quantity_adjustments_spec.rb

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,48 @@ module Spree::Promotion::Actions
2222
let(:quantity) { 1 }
2323
let(:promotion) { FactoryBot.create :promotion }
2424

25+
describe "#perform" do
26+
subject { action.perform({ order:, promotion: }) }
27+
28+
let(:calculator) { FactoryBot.create :flat_rate_calculator, preferred_amount: }
29+
30+
context "when calculator computes 0" do
31+
let(:preferred_amount) { 0 }
32+
33+
it "does not create an adjustment" do
34+
expect { subject }
35+
.not_to change { action.adjustments.count }
36+
end
37+
end
38+
39+
context "when calculator returns a non-zero value" do
40+
let(:preferred_amount) { 10 }
41+
let(:line_item) { order.line_items.first }
42+
43+
it "creates an adjustment" do
44+
expect { subject }
45+
.to change { action.adjustments.count }
46+
.from(0).to(1)
47+
.and change { line_item.adjustments.count }
48+
.from(0).to(1)
49+
50+
expect(line_item.adjustments).to eq(action.adjustments)
51+
end
52+
53+
it "associates the line item with the action", :aggregate_failures do
54+
expect { subject }
55+
.to change { line_item.line_item_actions.count }
56+
.from(0).to(1)
57+
58+
expect(action.line_item_actions.first).to have_attributes(
59+
line_item_id: line_item.id,
60+
action_id: action.id,
61+
quantity: 1
62+
)
63+
end
64+
end
65+
end
66+
2567
describe "#compute_amount" do
2668
subject { action.compute_amount(line_item) }
2769

@@ -46,6 +88,15 @@ module Spree::Promotion::Actions
4688
context "and an item with a quantity of 2" do
4789
let(:quantity) { 2 }
4890
it { is_expected.to eq(-10) }
91+
92+
it "doesn't save anything to the database" do
93+
action
94+
line_item
95+
96+
expect {
97+
subject
98+
}.not_to make_database_queries(manipulative: true)
99+
end
49100
end
50101

51102
context "and an item with a quantity of 3" do
@@ -94,7 +145,7 @@ module Spree::Promotion::Actions
94145
:order_with_line_items,
95146
line_items_attributes: [
96147
{ quantity: 3 }
97-
]
148+
]
98149
)
99150
end
100151

0 commit comments

Comments
 (0)