Skip to content

Commit

Permalink
Add persist flag to #recalculate
Browse files Browse the repository at this point in the history
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: Kendra Riga <[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]>
  • Loading branch information
11 people committed Oct 11, 2024
1 parent 9190b94 commit 27f19a8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
5 changes: 3 additions & 2 deletions core/app/models/spree/in_memory_order_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(order)
# This method should never do anything to the Order that results in a save call on the
# object with callbacks (otherwise you will end up in an infinite recursion as the
# associations try to save and then in turn try to call +update!+ again.)
def recalculate
def recalculate(persist: true)
order.transaction do
update_item_count
update_shipment_amounts
Expand All @@ -27,7 +27,8 @@ def recalculate
update_shipment_state
end
Spree::Bus.publish :order_recalculated, order: order
persist_totals

persist_totals if persist
end
end
alias_method :update, :recalculate
Expand Down
46 changes: 46 additions & 0 deletions core/spec/models/spree/in_memory_order_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,52 @@ module Spree
let(:order) { Spree::Order.create }
let(:updater) { described_class.new(order) }

describe "#recalculate" do
subject { updater.recalculate(persist:) }

let(:new_store) { create(:store) }

context "when the persist flag is set to 'false'" do
let(:persist) { false }

it "does not persist changes to order" do
order.store = new_store

expect {
subject
}.not_to make_database_queries(manipulative: true)

expect(order.store).to eq new_store
expect(order.reload.store).not_to eq new_store
end

it "does not persist changes to the item count" do
order.line_items << build(:line_item)

expect {
subject
}.not_to make_database_queries(manipulative: true)

expect(order.item_count).to eq 1
expect(order.reload.item_count).to eq 0
end
end

context "when the persist flag is set to 'true'" do
let(:persist) { true }

it "persists any changes to order" do
order.store = new_store

expect {
subject
}.to make_database_queries(manipulative: true)

expect(order.reload.store).to eq new_store
end
end
end

context "order totals" do
before do
2.times do
Expand Down

0 comments on commit 27f19a8

Please sign in to comment.