Skip to content

Commit 1ea7927

Browse files
adammathysjarednormanNoah-SilveraAlistairNorman
committed
Use Shipment#recalculate_state
We don't want our in-memory order updater to have to persist shipment state. Because we no longer rely on `Shipment#update_state` which uses `update_column`, we are now logging any additional `Shipment#state` changes when the order is saved. The changes to the specs reflect this change in behaviour. Co-authored-by: Jared Norman <jared@super.gd> Co-authored-by: Noah Silvera <noah@super.gd> Co-authored-by: Alistair Norman <alistair@super.gd>
1 parent daae6dd commit 1ea7927

File tree

3 files changed

+11
-24
lines changed

3 files changed

+11
-24
lines changed

core/app/models/spree/in_memory_order_updater.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def recalculate(persist: true)
4848
monitor.call do
4949
if order.completed?
5050
recalculate_payment_state
51-
update_shipments
5251
recalculate_shipment_state
5352
end
5453
end
@@ -61,7 +60,8 @@ def recalculate(persist: true)
6160
alias_method :update, :recalculate
6261
deprecate update: :recalculate, deprecator: Spree.deprecator
6362

64-
# Recalculates the +shipment_state+ attribute according to the following logic:
63+
# Recalculates the state on all of them shipments, then recalculates the
64+
# +shipment_state+ attribute according to the following logic:
6565
#
6666
# shipped when all Shipments are in the "shipped" state
6767
# partial when at least one Shipment has a state of "shipped" and there is another Shipment with a state other than "shipped"
@@ -72,6 +72,8 @@ def recalculate(persist: true)
7272
#
7373
# The +shipment_state+ value helps with reporting, etc. since it provides a quick and easy way to locate Orders needing attention.
7474
def recalculate_shipment_state
75+
shipments.each(&:recalculate_state)
76+
7577
log_state_change('shipment') do
7678
order.shipment_state = determine_shipment_state
7779
end
@@ -217,11 +219,6 @@ def persist_item_totals
217219
end
218220
end
219221

220-
# give each of the shipments a chance to update themselves
221-
def update_shipments
222-
shipments.each(&:update_state)
223-
end
224-
225222
def recalculate_payment_total
226223
order.payment_total = payments.completed.includes(:refunds).sum { |payment| payment.amount - payment.refunds.sum(:amount) }
227224
end

core/spec/models/spree/in_memory_order_updater_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ module Spree
544544
order.shipments = [shipment]
545545
allow(order.shipments).to receive_messages(states: [], ready: [], pending: [], shipped: [])
546546
allow(updater).to receive(:update_totals) # Otherwise this gets called and causes a scene
547-
expect(updater).not_to receive(:update_shipments)
547+
expect(updater).not_to receive(:recalculate_shipment_state)
548548
updater.recalculate
549549
end
550550
end

core/spec/models/spree/shipment_spec.rb

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -983,14 +983,18 @@
983983
it_behaves_like "customer and admin metadata fields: storage and validation", :shipment
984984

985985
describe "state change tracking" do
986+
# Ensure that we're only testing tracking jobs created by the change in
987+
# state.
988+
before { shipment }
989+
986990
it "enqueues a StateChangeTrackingJob when state changes" do
987991
expect {
988992
shipment.update!(state: 'shipped')
989993
}.to have_enqueued_job(Spree::StateChangeTrackingJob).with(
990994
shipment,
991995
'pending',
992996
'shipped',
993-
kind_of(Time)
997+
be_within(1.second).of(Time.current)
994998
)
995999
end
9961000

@@ -1000,20 +1004,6 @@
10001004
}.not_to have_enqueued_job(Spree::StateChangeTrackingJob)
10011005
end
10021006

1003-
it "captures the transition timestamp accurately" do
1004-
before_time = Time.current
1005-
1006-
shipment.update!(state: 'shipped')
1007-
1008-
# Check that a job was enqueued with a timestamp close to when we made the change
1009-
expect(Spree::StateChangeTrackingJob).to have_been_enqueued.with do |shipment_id, prev_state, next_state, timestamp|
1010-
expect(shipment_id).to eq(shipment.id)
1011-
expect(prev_state).to eq('pending')
1012-
expect(next_state).to eq('shipped')
1013-
expect(timestamp).to be_within(1.second).of(before_time)
1014-
end
1015-
end
1016-
10171007
it "creates multiple state transitions" do
10181008
clear_enqueued_jobs
10191009

@@ -1027,7 +1017,7 @@
10271017
perform_enqueued_jobs do
10281018
expect {
10291019
shipment.update!(state: 'shipped')
1030-
}.to change(Spree::StateChange, :count).by(1)
1020+
}.to change(shipment.state_changes, :count).by(1)
10311021
end
10321022

10331023
state_change = Spree::StateChange.last

0 commit comments

Comments
 (0)