Skip to content

Commit 0abbd73

Browse files
Address pr feedback from pr 29
Addressed changes: - [EventsManager -> EventManager Rename](#29 (comment)) - [Using find_case callback](#29 (comment)) - [No-op loop rather than if block](#29 (comment)) - [Passing method reference instead of callback loop](#29 (comment)) - [Changing named parameters to normal parameters](#29 (comment)) - [Naming style change](#29 (comment))
1 parent fbae213 commit 0abbd73

16 files changed

Lines changed: 48 additions & 53 deletions

template/{{app_name}}/engines/flex/app/helpers/flex/events_manager.rb renamed to template/{{app_name}}/engines/flex/app/helpers/flex/event_manager.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Flex
2-
class EventsManager
2+
class EventManager
33
def self.subscribe(event_key, callback)
44
subscription = ActiveSupport::Notifications.subscribe(event_key) do |name, _started, _finished, _unique_id, payload|
55
callback.call({

template/{{app_name}}/engines/flex/app/models/concerns/flex/task_handler_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Flex
22
module TaskHandlerService
33
extend ActiveSupport::Concern
44

5-
def create_task(kase:)
5+
def create_task(kase)
66
raise NoMethodError, "#{self.class} must implement execute method"
77
end
88
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def prevent_changes_if_submitted
3232
end
3333

3434
def publish_event
35-
EventsManager.publish("application_submitted", self.event_payload)
35+
EventManager.publish("application_submitted", self.event_payload)
3636
end
3737
end
3838
end

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ class BusinessProcess
44

55
attr_accessor :name, :description, :steps, :start, :transitions
66

7-
def initialize(name:, type:, description: "", steps: {}, start: "", transitions: {})
7+
def initialize(name:, find_case_callback:, description: "", steps: {}, start: "", transitions: {})
88
@subscriptions = {}
99
@name = name
10-
@type = type # recognizing this is a code smell that we will want to address in the future
10+
@find_case_callback = find_case_callback
1111
@description = description
1212
define_start(start)
1313
define_steps(steps)
@@ -28,18 +28,15 @@ def define_steps(steps)
2828
end
2929

3030
def define_transitions(transitions)
31-
if @subscriptions.any?
32-
stop_listening_all_events
33-
end
34-
31+
stop_listening_for_events
3532
@transitions = transitions
3633
start_listening_for_events
3734
end
3835

3936
private
4037

4138
def handle_event(event)
42-
kase = @type.find(event[:payload][:case_id])
39+
kase = @find_case_callback.call(event[:payload][:case_id])
4340
current_step = kase.business_process_current_step
4441
next_step = @transitions[current_step][event[:name]]
4542
kase.business_process_current_step = next_step
@@ -57,16 +54,13 @@ def get_event_names_from_transitions
5754

5855
def start_listening_for_events
5956
get_event_names_from_transitions.each do |event_name|
60-
new_subscription = EventsManager.subscribe(event_name, ->(event) {
61-
handle_event(event)
62-
})
63-
@subscriptions[event_name] = new_subscription
57+
@subscriptions[event_name] = EventManager.subscribe(event_name, method(:handle_event))
6458
end
6559
end
6660

67-
def stop_listening_all_events
61+
def stop_listening_for_events
6862
@subscriptions.each do |event_name, subscription|
69-
EventsManager.unsubscribe(subscription)
63+
EventManager.unsubscribe(subscription)
7064
end
7165
@subscriptions.clear
7266
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class SystemProcess
55
attr_accessor :name
66
attr_accessor :callback
77

8-
def initialize(name:, callback:)
8+
def initialize(name, callback)
99
@name = name
1010
@callback = callback
1111
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class UserTask
44

55
attr_accessor :name, :task_management_service
66

7-
def initialize(name:, task_management_service:)
7+
def initialize(name, task_management_service)
88
@name = name
99
@task_management_service = task_management_service
1010
end

template/{{app_name}}/engines/flex/spec/acceptance/flex/passport_application_case_acceptance_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ module Flex
2424
expect(kase.business_process_current_step).to eq ("verify_identity")
2525

2626
# verify identity (simulate action that an adjudicator takes)
27-
EventsManager.publish("identity_verified", { case_id: kase.id })
27+
EventManager.publish("identity_verified", { case_id: kase.id })
2828
kase.reload
2929
expect(kase.business_process_current_step).to eq ("review_passport_photo")
3030

3131
# approve passport photo
32-
EventsManager.publish("passport_photo_approved", { case_id: kase.id })
32+
EventManager.publish("passport_photo_approved", { case_id: kase.id })
3333
kase.reload
3434
expect(kase.business_process_current_step).to eq ("notify_user_passport_approved")
3535

3636
# notify user
37-
EventsManager.publish("notification_completed", { case_id: kase.id })
37+
EventManager.publish("notification_completed", { case_id: kase.id })
3838
kase.reload
3939
expect(kase.business_process_current_step).to eq ("end")
4040

template/{{app_name}}/engines/flex/spec/dummy/app/business_processes/flex/passport_application_business_process_manager.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ def initialize
1313
def create_passport_application_business_process
1414
business_process = BusinessProcess.new(
1515
name: 'Passport Application Process',
16-
type: PassportCase,
16+
find_case_callback: ->(case_id) { PassportCase.find(case_id) },
1717
description: 'Process for applying for a passport'
1818
)
1919
business_process.define_steps(
2020
{
2121
"collect_application_info" => UserTask.new(
22-
name: "Collect App Info",
23-
task_management_service: UserTaskCreatorService
22+
"Collect App Info",
23+
UserTaskCreationService
2424
),
25-
"verify_identity" => SystemProcess.new(name: "Verify Identity", callback: ->(kase) {
26-
IdentityVerifierService.new(kase).verify_identity # IdentityVerifierService would publish an event when verify_identity completes
25+
"verify_identity" => SystemProcess.new("Verify Identity", ->(kase) {
26+
IdentityVerificationService.new(kase).verify_identity # IdentityVerificationService would publish an event when verify_identity completes
2727
}),
28-
"manual_adjudicator_review" => UserTask.new(name: "Manual Adjudicator Review", task_management_service: AdjudicatorTaskCreatorService), # create an adjudicator task for manual review
29-
"review_passport_photo" => SystemProcess.new(name: "Review Passport Photo", callback: ->(kase) {
30-
PhotoVerifierService.new(kase).verify_photo # PhotoVerifierService would publish an event when verify_photo completes
28+
"manual_adjudicator_review" => UserTask.new("Manual Adjudicator Review", AdjudicatorTaskCreationService), # create an adjudicator task for manual review
29+
"review_passport_photo" => SystemProcess.new("Review Passport Photo", ->(kase) {
30+
PhotoVerificationService.new(kase).verify_photo # PhotoVerificationService would publish an event when verify_photo completes
3131
}),
32-
"notify_user_passport_approved" => SystemProcess.new(name: "Notify Passport Approval", callback: ->(kase) {
33-
UserNotifierService.new(kase).send_notification("approval") # UserNotifierService would publish an event when send_notification completes
32+
"notify_user_passport_approved" => SystemProcess.new("Notify Passport Approval", ->(kase) {
33+
UserNotificationService.new(kase).send_notification("approval") # UserNotificationService would publish an event when send_notification completes
3434
}),
35-
"notify_user_passport_rejected" => SystemProcess.new(name: "Notify Passport Rejection", callback: ->(kase) {
36-
UserNotifierService.new(kase).send_notification("rejection") # UserNotifierService would publish an event when send_notification completes
35+
"notify_user_passport_rejected" => SystemProcess.new("Notify Passport Rejection", ->(kase) {
36+
UserNotificationService.new(kase).send_notification("rejection") # UserNotificationService would publish an event when send_notification completes
3737
})
3838
}
3939
)

template/{{app_name}}/engines/flex/spec/dummy/app/services/flex/adjudicator_task_creator_service.rb renamed to template/{{app_name}}/engines/flex/spec/dummy/app/services/flex/adjudicator_task_creation_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Flex
2-
class AdjudicatorTaskCreatorService
2+
class AdjudicatorTaskCreationService
33
include TaskHandlerService
44

55
def self.create_task(kase:)

template/{{app_name}}/engines/flex/spec/dummy/app/services/flex/identity_verifier_service.rb renamed to template/{{app_name}}/engines/flex/spec/dummy/app/services/flex/identity_verification_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Flex
2-
class IdentityVerifierService
2+
class IdentityVerificationService
33
def initialize(kase)
44
@kase = kase
55
end

0 commit comments

Comments
 (0)