Skip to content

Commit e5733e6

Browse files
Add clear_process_configuration method
Added a `clear_process_configuration` method to BusinessProcess, which can allow someone to clear the steps, transitions, start, and subscriptions of a BusinessProcess. Additionally: - Added an `attr_accessor` for `find_case_callback` so that a user of BusinessProcess can change it if needed - Added debug logging around subscribing and unsubscribing, which can make local development (or other debugging) easier
1 parent 6c87e05 commit e5733e6

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

template/{{app_name}}/engines/flex/app/models/flex/business_process.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Flex
22
class BusinessProcess
33
include Step
44

5-
attr_accessor :name, :description, :steps, :start, :transitions
5+
attr_accessor :name, :description, :steps, :start, :transitions, :find_case_callback
66

77
def initialize(name:, find_case_callback:, description: "", steps: {}, start: "", transitions: {})
88
@subscriptions = {}
@@ -33,6 +33,15 @@ def define_transitions(transitions)
3333
start_listening_for_events
3434
end
3535

36+
# @description This method will clear subscriptions and set steps, transitions, and start to their default values.
37+
# Only use this method if you are finished with the instance or plan to manually reset these values.
38+
def clear_process_configuration
39+
stop_listening_for_events
40+
@steps = {}
41+
@transitions = {}
42+
@start = ""
43+
end
44+
3645
private
3746

3847
def handle_event(event)
@@ -54,12 +63,14 @@ def get_event_names_from_transitions
5463

5564
def start_listening_for_events
5665
get_event_names_from_transitions.each do |event_name|
66+
Rails.logger.debug "Flex::BusinessProcess with name #{name} subscribing to event: #{event_name}"
5767
@subscriptions[event_name] = EventManager.subscribe(event_name, method(:handle_event))
5868
end
5969
end
6070

6171
def stop_listening_for_events
6272
@subscriptions.each do |event_name, subscription|
73+
Rails.logger.debug "Flex::BusinessProcess with name #{name} unsubscribing from event: #{event_name}"
6374
EventManager.unsubscribe(subscription)
6475
end
6576
@subscriptions.clear

template/{{app_name}}/engines/flex/spec/models/flex/business_process_spec.rb

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
stub_const("Flex::EventManager", mock_event_manager)
1818
end
1919

20-
describe 'executing a business process' do
20+
describe '#execute' do
2121
before do
2222
business_process.define_steps(mock_steps)
2323
allow(mock_steps["user_task"]).to receive(:execute)
@@ -43,7 +43,7 @@
4343
end
4444
end
4545

46-
describe 'when defining transitions' do
46+
describe '#define_transitions' do
4747
before do
4848
allow(mock_event_manager).to receive(:subscribe)
4949
allow(mock_event_manager).to receive(:unsubscribe)
@@ -74,4 +74,31 @@
7474
expect(mock_event_manager).to have_received(:subscribe).with("event4", anything)
7575
end
7676
end
77+
78+
describe '#clear_process_configuration' do
79+
before do
80+
allow(mock_event_manager).to receive(:unsubscribe)
81+
allow(mock_event_manager).to receive(:subscribe)
82+
business_process.define_steps(mock_steps)
83+
business_process.define_start("event1")
84+
business_process.define_transitions({
85+
"step1" => { "event1" => "step2" },
86+
"step2" => { "event2" => "end" }
87+
})
88+
end
89+
90+
it 'clears all steps, transitions, and the start step' do
91+
business_process.clear_process_configuration
92+
93+
expect(business_process.steps).to eq({})
94+
expect(business_process.transitions).to eq({})
95+
expect(business_process.start).to eq("")
96+
end
97+
98+
it 'stops listening for events' do
99+
business_process.clear_process_configuration
100+
101+
expect(mock_event_manager).to have_received(:unsubscribe).exactly(2).times
102+
end
103+
end
77104
end

0 commit comments

Comments
 (0)