Skip to content

Commit 6890f58

Browse files
authored
Merge pull request #6375 from blish/config-state-change-job-class
feat: Make state change tracker a configurable class
2 parents d3ef74b + 3cf18bb commit 6890f58

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
@@ -19,11 +19,11 @@ def enqueue_state_change_tracking
1919
previous_state, current_state = saved_changes['state']
2020

2121
# Enqueue the job to track this state change
22-
StateChangeTrackingJob.perform_later(
23-
self,
24-
previous_state,
25-
current_state,
26-
Time.current
22+
Spree::Config.state_change_tracking_class.call(
23+
stateful: self,
24+
previous_state:,
25+
current_state:,
26+
transition_timestamp: Time.current
2727
)
2828
end
2929
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
@@ -2181,7 +2181,8 @@ def validate(line_item)
21812181
order,
21822182
'cart',
21832183
'address',
2184-
kind_of(Time)
2184+
kind_of(Time),
2185+
'order'
21852186
)
21862187
end
21872188

@@ -2201,7 +2202,8 @@ def validate(line_item)
22012202
order,
22022203
'cart',
22032204
'address',
2204-
kind_of(Time)
2205+
kind_of(Time),
2206+
'order'
22052207
)
22062208
end
22072209
end

core/spec/models/spree/payment_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,8 @@
13501350
payment,
13511351
'checkout',
13521352
'completed',
1353-
kind_of(Time)
1353+
kind_of(Time),
1354+
'payment'
13541355
)
13551356
end
13561357

core/spec/models/spree/shipment_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,8 @@
974974
shipment,
975975
'pending',
976976
'shipped',
977-
kind_of(Time)
977+
kind_of(Time),
978+
'shipment'
978979
)
979980
end
980981

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)