Skip to content

Commit

Permalink
Conditionally persist Shipment#update_amounts changes
Browse files Browse the repository at this point in the history
This is in service of supporting the InMemoryOrderUpdater's goal to not do database writes.
  • Loading branch information
Noah-Silvera committed Oct 25, 2024
1 parent 5ecd078 commit 84c1ce4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions core/app/models/spree/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ def tracking_url
@tracking_url ||= shipping_method.build_tracking_url(tracking)
end

def update_amounts
def update_amounts(persist: true)
if selected_shipping_rate
self.cost = selected_shipping_rate.cost
if changed?
if changed? && persist
update_columns(
cost: cost,
updated_at: Time.current
Expand Down
33 changes: 26 additions & 7 deletions core/spec/models/spree/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -508,22 +508,41 @@
end

describe "#update_amounts" do
let(:shipment) { create(:shipment, cost: 3) }
subject { shipment.update_amounts(persist: persist) }

let(:persist) { true }
let(:shipment) { create(:shipment, cost: 1) }

context 'when the selected shipping rate cost is different than the current shipment cost' do
before { shipment.selected_shipping_rate.update!(cost: 5) }
before { shipment.selected_shipping_rate.update!(cost: 999) }

it "updates the shipments cost" do
it "changes and persists the shipments cost" do
expect {
shipment.update_amounts
}.to change { shipment.reload.cost }.to(5)
subject
}.to change { shipment.reload.cost }.to(999)
end

it 'changes the updated_at column' do
it 'changes and persists the updated_at column' do
expect {
shipment.update_amounts
subject
}.to change { shipment.reload.updated_at }
end

context 'when `persist: false` is passed' do
let(:persist) { false }

it 'does not perform any database writes' do
expect {
subject
}.not_to make_database_queries(manipulative: true)
end

it "changes but does not persist the shipments cost" do
subject
expect(shipment.cost).to eq 999
expect(shipment.reload.cost).to eq 1
end
end
end
end

Expand Down

0 comments on commit 84c1ce4

Please sign in to comment.