Skip to content

Commit 578b60e

Browse files
committed
feat: Make state change tracker a configurable class
Currently we always enqueues a ActiveJob, but some stores maybe want to inline the state changes or add more information to the state change record. This allows to set your own state change tracker class. It defaults to the current behavior.
1 parent f54d7ff commit 578b60e

File tree

8 files changed

+83
-9
lines changed

8 files changed

+83
-9
lines changed

core/app/models/concerns/spree/state_change_tracking.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ def enqueue_state_change_tracking
1515
previous_state, current_state = saved_changes['state']
1616

1717
# Enqueue the job to track this state change
18-
StateChangeTrackingJob.perform_later(
19-
self,
20-
previous_state,
21-
current_state,
22-
Time.current
18+
Spree::Config.state_change_tracking_class.call(
19+
stateful: self,
20+
previous_state:,
21+
current_state:,
22+
transition_timestamp: Time.current
2323
)
2424
end
2525
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
module Spree
4+
# Configurable class to enqueue state change tracking jobs
5+
# Configure your custom logic by setting Spree::Config.state_change_tracking_class
6+
# @example Spree::Config.state_change_tracking_class = MyCustomTracker
7+
class StateChangeTracker
8+
# @param stateful [Object] The stateful object to track changes for
9+
# @param previous_state [String] The previous state of the order
10+
# @param current_state [String] The current state of the order
11+
# @param transition_timestamp [Time] When the state transition occurred
12+
# @param stateful_name [String] The element name of the state transition being
13+
# tracked. It defaults to the `stateful` model element name.
14+
def self.call(
15+
stateful:,
16+
previous_state:,
17+
current_state:,
18+
transition_timestamp:,
19+
stateful_name: stateful.class.model_name.element
20+
)
21+
# Enqueue a background job to track this state change
22+
StateChangeTrackingJob.perform_later(
23+
stateful,
24+
previous_state,
25+
current_state,
26+
transition_timestamp,
27+
stateful_name
28+
)
29+
end
30+
end
31+
end

core/lib/spree/app_configuration.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,13 @@ def payment_canceller
549549
product: '680x680>',
550550
large: '1200x1200>' }
551551

552+
# Allows to provide your own class for tracking state changes of stateful models
553+
#
554+
# @!attribute [rw] state_change_tracking_class
555+
# @return [Class] a class with the same public interfaces as
556+
# Spree::StateChangeTracker.
557+
class_name_attribute :state_change_tracking_class, default: "Spree::StateChangeTracker"
558+
552559
# Allows providing your own class for prioritizing store credit application
553560
# to an order.
554561
#

core/spec/lib/spree/app_configuration_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
expect(prefs.variant_price_selector_class).to eq Spree::Variant::PriceSelector
4545
end
4646

47+
it "uses state change tracker class by default" do
48+
expect(prefs.state_change_tracking_class).to eq Spree::StateChangeTracker
49+
end
50+
4751
it "uses core's promotion configuration class by default" do
4852
expect(prefs.promotions).to be_a Spree::Core::NullPromotionConfiguration
4953
end

core/spec/models/spree/order_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,8 @@ def validate(line_item)
21752175
order,
21762176
'cart',
21772177
'address',
2178-
kind_of(Time)
2178+
kind_of(Time),
2179+
'order'
21792180
)
21802181
end
21812182

@@ -2195,7 +2196,8 @@ def validate(line_item)
21952196
order,
21962197
'cart',
21972198
'address',
2198-
kind_of(Time)
2199+
kind_of(Time),
2200+
'order'
21992201
)
22002202
end
22012203
end

core/spec/models/spree/payment_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,8 @@
13441344
payment,
13451345
'checkout',
13461346
'completed',
1347-
kind_of(Time)
1347+
kind_of(Time),
1348+
'payment'
13481349
)
13491350
end
13501351

core/spec/models/spree/shipment_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,8 @@
959959
shipment,
960960
'pending',
961961
'shipped',
962-
kind_of(Time)
962+
kind_of(Time),
963+
'shipment'
963964
)
964965
end
965966

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
5+
RSpec.describe Spree::StateChangeTracker, type: :model do
6+
let(:order) { create(:order) }
7+
let(:transition_timestamp) { Time.current }
8+
9+
describe "#call" do
10+
it "enqueues a StateChangeTrackingJob with correct arguments" do
11+
expect {
12+
described_class.call(
13+
stateful: order,
14+
previous_state: "cart",
15+
current_state: "address",
16+
transition_timestamp: transition_timestamp,
17+
stateful_name: "order"
18+
)
19+
}.to have_enqueued_job(Spree::StateChangeTrackingJob).with(
20+
order,
21+
"cart",
22+
"address",
23+
transition_timestamp,
24+
"order"
25+
)
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)