Skip to content

Commit 340032c

Browse files
benjaminwiladammathysstewartHarmony Evangelinajarednorman
committed
Add persist flag to #recalculate
We want our new in-memory order updater to be able to persist or not persist changes to the order record. WORK IN PROGRESS This is a first step in ensuring we don't need to write to the database using the order updater. Clearly we have more work to do to ensure this functions like the existing updater. Co-authored-by: Adam Mueller <[email protected]> Co-authored-by: Andrew Stewart <[email protected]> Co-authored-by: Harmony Evangelina <[email protected]> Co-authored-by: Jared Norman <[email protected]> Co-authored-by: Nick Van Doorn <[email protected]> Co-authored-by: Noah Silvera <[email protected]> Co-authored-by: Senem Soy <[email protected]> Co-authored-by: Sofia Besenski <[email protected]> Co-authored-by: Tom Van Manen <[email protected]>
1 parent faa5c81 commit 340032c

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

core/app/models/spree/in_memory_order_updater.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def initialize(order)
1616
# This method should never do anything to the Order that results in a save call on the
1717
# object with callbacks (otherwise you will end up in an infinite recursion as the
1818
# associations try to save and then in turn try to call +update!+ again.)
19-
def recalculate
19+
def recalculate(persist: true)
2020
order.transaction do
2121
update_item_count
2222
update_shipment_amounts
@@ -27,7 +27,8 @@ def recalculate
2727
update_shipment_state
2828
end
2929
Spree::Bus.publish :order_recalculated, order: order
30-
persist_totals
30+
31+
persist_totals if persist
3132
end
3233
end
3334
alias_method :update, :recalculate

core/spec/models/spree/in_memory_order_updater_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,52 @@ module Spree
88
let(:order) { Spree::Order.create }
99
let(:updater) { described_class.new(order) }
1010

11+
describe "#recalculate" do
12+
subject { updater.recalculate(persist:) }
13+
14+
let(:new_store) { create(:store) }
15+
16+
context "when the persist flag is set to 'false'" do
17+
let(:persist) { false }
18+
19+
it "does not persist changes to order" do
20+
order.store = new_store
21+
22+
expect {
23+
subject
24+
}.not_to make_database_queries(manipulative: true)
25+
26+
expect(order.store).to eq new_store
27+
expect(order.reload.store).not_to eq new_store
28+
end
29+
30+
it "does not persist changes to the item count" do
31+
order.line_items << build(:line_item)
32+
33+
expect {
34+
subject
35+
}.not_to make_database_queries(manipulative: true)
36+
37+
expect(order.item_count).to eq 1
38+
expect(order.reload.item_count).to eq 0
39+
end
40+
end
41+
42+
context "when the persist flag is set to 'true'" do
43+
let(:persist) { true }
44+
45+
it "persists any changes to order" do
46+
order.store = new_store
47+
48+
expect {
49+
subject
50+
}.to make_database_queries(manipulative: true)
51+
52+
expect(order.reload.store).to eq new_store
53+
end
54+
end
55+
end
56+
1157
context "order totals" do
1258
before do
1359
2.times do

0 commit comments

Comments
 (0)