diff --git a/core/app/models/spree/in_memory_order_updater.rb b/core/app/models/spree/in_memory_order_updater.rb index 41ac249a3a..5f5255a169 100644 --- a/core/app/models/spree/in_memory_order_updater.rb +++ b/core/app/models/spree/in_memory_order_updater.rb @@ -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 @@ -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 diff --git a/core/spec/models/spree/in_memory_order_updater_spec.rb b/core/spec/models/spree/in_memory_order_updater_spec.rb index 98cfb111ee..2e9020825d 100644 --- a/core/spec/models/spree/in_memory_order_updater_spec.rb +++ b/core/spec/models/spree/in_memory_order_updater_spec.rb @@ -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