Class: ActivityTypeRule
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ActivityTypeRule
- Includes:
- Models::Auditable
- Defined in:
- app/models/activity_type_rule.rb
Overview
== Schema Information
Table name: activity_type_rules
Database name: primary
id :bigint not null, primary key
source_event :enum not null
tagged :string
target_event :enum not null
target_scope :enum not null
activity_type_id :integer not null
target_activity_type_id :integer
Indexes
index_unique_activity_type_rules (activity_type_id,target_activity_type_id,tagged,source_event,target_event,target_scope) UNIQUE
Constant Summary
Constants included from Models::Auditable
Models::Auditable::ALWAYS_IGNORED
Constants included from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
- #source_event ⇒ Object readonly
- #target_event ⇒ Object readonly
- #target_scope ⇒ Object readonly
Belongs to collapse
Methods included from Models::Auditable
Instance Method Summary collapse
- #description ⇒ Object
- #determine_target_object(activity) ⇒ Object
- #execute_target_event_for_activity(activity) ⇒ Object
- #find_open_activities(target_object) ⇒ Object
Methods included from Models::Auditable
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#source_event ⇒ Object (readonly)
33 |
# File 'app/models/activity_type_rule.rb', line 33 validates :source_event, presence: true |
#target_event ⇒ Object (readonly)
34 |
# File 'app/models/activity_type_rule.rb', line 34 validates :target_event, presence: true |
#target_scope ⇒ Object (readonly)
35 |
# File 'app/models/activity_type_rule.rb', line 35 validates :target_scope, presence: true |
Instance Method Details
#activity_type ⇒ ActivityType
28 |
# File 'app/models/activity_type_rule.rb', line 28 belongs_to :activity_type |
#description ⇒ Object
38 39 40 |
# File 'app/models/activity_type_rule.rb', line 38 def description "On #{source_event} of #{activity_type.task_type}, find activities of type #{tagged.presence || target_activity_type.task_type} in #{target_scope} and #{target_event}" end |
#determine_target_object(activity) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/models/activity_type_rule.rb', line 42 def determine_target_object(activity) # First determine the scope if target_scope_resource? activity.resource elsif target_scope_party? activity.party elsif target_scope_customer? activity.customer elsif target_scope_opportunity? && (resource = activity.resource) if resource.is_a?(Opportunity) resource elsif resource.respond_to?(:opportunity) activity.resource.opportunity end end end |
#execute_target_event_for_activity(activity) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/activity_type_rule.rb', line 72 def execute_target_event_for_activity(activity) return [] unless (target_object = determine_target_object(activity)) activity.updated_activities ||= [] find_open_activities(target_object).excluding(activity).find_each do |target_activity| new_note = "Triggered by #{activity.display_type} - #{description}" if target_event_complete? target_activity.complete(new_note:) activity.updated_activities << target_activity elsif target_event_cancel? target_activity.cancel(new_note:) activity.updated_activities << target_activity end end activity.updated_activities end |
#find_open_activities(target_object) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/models/activity_type_rule.rb', line 59 def find_open_activities(target_object) activities = target_object.all_activities if target_object.respond_to?(:all_activities) activities ||= target_object.activities activities = activities.open_activities # Apply filter if target_activity_type activities = activities.where(activity_type_id: target_activity_type.id) elsif tagged.present? activities = activities.tagged_with(tagged) end activities end |
#target_activity_type ⇒ ActivityType
29 |
# File 'app/models/activity_type_rule.rb', line 29 belongs_to :target_activity_type, class_name: 'ActivityType', optional: true |