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 :integer 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

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

#publish_event

Instance Attribute Details

#offset_daysObject (readonly)



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

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:



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

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

Instance Method Details

#activity_result_typeActivityResultType



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

belongs_to :activity_result_type, required: true, optional: true

#activity_typeActivityType



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

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

#campaignCampaign

Returns:

See Also:



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

belongs_to :campaign, required: false, optional: true

#chain_activity_typeActivityType



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

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

#email_templateEmailTemplate



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

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

#email_template_descriptionObject



65
66
67
68
69
70
71
72
# File 'app/models/activity_chain_type.rb', line 65

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.



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

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



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

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



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

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