Class: Activity::TypeRulesCreatedHandler

Inherits:
Object
  • Object
show all
Defined in:
app/subscribers/activity/type_rules_created_handler.rb

Overview

Executes ActivityTypeRules with source_event_creation scope after a new
activity is persisted and the transaction commits.

Subscribes to: Events::ActivityCreated

WHY after-commit: Rules create new Activity records. Running after the parent
activity has fully committed prevents foreign-key issues and ensures that any
rule-triggered child activity can itself trigger the observer chain cleanly.

NOTE: a no-op when the activity has no activity_type (notes-only activities).

Instance Method Summary collapse

Instance Method Details

#call(event) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/subscribers/activity/type_rules_created_handler.rb', line 14

def call(event)
  activity = Activity.find_by(id: event.data[:activity_id])
  return unless activity
  return unless activity.activity_type

  creation_rules = activity.activity_type.activity_type_rules
                           .includes(:target_activity_type)
                           .source_event_creation
  return if creation_rules.blank?

  creation_rules.each { |r| r.execute_target_event_for_activity(activity) }
end