Class: ActivityChainType

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/activity_chain_type.rb

Overview

== Schema Information

Table name: activity_chain_types
Database name: primary

id :integer not null, primary key
auto_close_rule :enum default("not_set"), not null
email_defer_days :integer
email_defer_tod :time
offset_calculation :enum default("static_offset")
offset_days :integer
prompt :boolean default(TRUE), not null
schedule_on_completion :boolean default(TRUE)
created_at :datetime
updated_at :datetime
activity_result_type_id :integer
activity_type_id :integer
campaign_id :integer
chain_activity_type_id :integer
email_template_id :integer

Indexes

activity_chain_types_activity_result_type_id_idx (activity_result_type_id)
activity_chain_types_campaign_id_idx (campaign_id)
activity_chain_types_email_template_id_idx (email_template_id)
idx_activity_chain_types (activity_type_id,activity_result_type_id)

Foreign Keys

activity_chain_types_activity_result_type_id_fkey (activity_result_type_id => activity_result_types.id)
activity_chain_types_activity_type_id_fkey (activity_type_id => activity_types.id)
activity_chain_types_email_template_id_fk (email_template_id => email_templates.id) ON DELETE => nullify
fk_rails_... (campaign_id => campaigns.id)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Class Method Summary collapse

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 Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#offset_daysObject (readonly)



56
# File 'app/models/activity_chain_type.rb', line 56

validates :offset_days, numericality: { only_integer: true, less_than_or_equal_to: 365, greater_than_or_equal_to: 0 }, if: -> { chain_activity_type.present? }

Class Method Details

.with_chainsActiveRecord::Relation<ActivityChainType>

A relation of ActivityChainTypes that are with chains. Active Record Scope

Returns:

See Also:



58
# File 'app/models/activity_chain_type.rb', line 58

scope :with_chains, -> { where.not(chain_activity_type_id: nil) }

Instance Method Details

#activity_result_typeActivityResultType



51
# File 'app/models/activity_chain_type.rb', line 51

belongs_to :activity_result_type, optional: true

#activity_typeActivityType



48
# File 'app/models/activity_chain_type.rb', line 48

belongs_to :activity_type, inverse_of: :activity_chain_types, optional: true

#campaignCampaign

Returns:

See Also:



49
# File 'app/models/activity_chain_type.rb', line 49

belongs_to :campaign, optional: true

#chain_activity_typeActivityType



50
# File 'app/models/activity_chain_type.rb', line 50

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

#email_templateEmailTemplate



52
# File 'app/models/activity_chain_type.rb', line 52

belongs_to :email_template, inverse_of: :activity_chain_types, optional: true

#email_template_descriptionObject



68
69
70
71
72
73
74
75
# File 'app/models/activity_chain_type.rb', line 68

def email_template_description
  s = []
  s << email_template.description
  s << "[#{email_template.category}]" if email_template.category.present?
  s << "+#{email_defer_days}d" if email_defer_days.present?
  s << "@#{email_defer_tod.strftime('%I:%M %p')}" if email_defer_tod.present?
  s.join(' ')
end

#email_transmit_at_timeObject

Calculates the date/time when the email for this activity
should be transmitted. It will transmit immediately by default. If the
activity type has configured email deferral rules, it will apply those
by adding a number of days or setting the time of day to transmit.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/activity_chain_type.rb', line 81

def email_transmit_at_time
  # Determine here if we send immediately or schedule the email
  return unless email_defer_days&.positive? || email_defer_tod.present?

  transmit_at = Time.current
  if email_defer_days&.positive?
    # Add this number of days to the current time
    transmit_at += email_defer_days.days
  end
  if email_defer_tod
    # If email has a deferal rule for a specific time of the day
    transmit_at = email_defer_tod.on(transmit_at)
    # But if the time of day has passed already we advance to the next day
    transmit_at += 1.day if transmit_at < Time.current
  end
  transmit_at
end

#last_usedObject



60
61
62
# File 'app/models/activity_chain_type.rb', line 60

def last_used
  Activity.where(activity_type_id: activity_type_id, activity_result_type_id: activity_result_type_id).pluck('max(completion_datetime)').try(:first)
end

#usage_countObject



64
65
66
# File 'app/models/activity_chain_type.rb', line 64

def usage_count
  Activity.where(activity_type_id: activity_type_id, activity_result_type_id: activity_result_type_id).count
end