Class: ActivityTypeRule

Inherits:
ApplicationRecord show all
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 :string not null
tagged :string
target_event :string not null
target_scope :string 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

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Instance Method Summary collapse

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 Models::EventPublishable

#publish_event

Instance Attribute Details

#source_eventObject (readonly)



30
# File 'app/models/activity_type_rule.rb', line 30

validates :source_event, presence: true

#target_eventObject (readonly)



31
# File 'app/models/activity_type_rule.rb', line 31

validates :target_event, presence: true

#target_scopeObject (readonly)



32
# File 'app/models/activity_type_rule.rb', line 32

validates :target_scope, presence: true

Instance Method Details

#activity_typeActivityType



25
# File 'app/models/activity_type_rule.rb', line 25

belongs_to :activity_type

#descriptionObject



35
36
37
# File 'app/models/activity_type_rule.rb', line 35

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/activity_type_rule.rb', line 39

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/activity_type_rule.rb', line 69

def execute_target_event_for_activity(activity)
  return [] unless (target_object = determine_target_object(activity))

  activity.updated_activities ||= []

  find_open_activities(target_object).where.not(id: activity.id).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



56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/activity_type_rule.rb', line 56

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_typeActivityType



26
# File 'app/models/activity_type_rule.rb', line 26

belongs_to :target_activity_type, class_name: 'ActivityType', optional: true