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

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

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#offset_daysInteger (readonly)

Returns:

  • (Integer)


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

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:



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

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

Instance Method Details

#activity_result_typeActivityResultType?

Returns:



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

belongs_to :activity_result_type, optional: true

#activity_typeActivityType?

Returns:



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

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

#campaignCampaign?

Returns:



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

belongs_to :campaign, optional: true

#chain_activity_typeActivityType?

Returns:



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

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

#email_templateEmailTemplate?

Returns:



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

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

#email_template_descriptionString

Returns:

  • (String)


77
78
79
80
81
82
83
84
# File 'app/models/activity_chain_type.rb', line 77

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_timeActiveSupport::TimeWithZone?

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.

Returns:

  • (ActiveSupport::TimeWithZone, nil)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/activity_chain_type.rb', line 91

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_usedActiveSupport::TimeWithZone?

Returns:

  • (ActiveSupport::TimeWithZone, nil)


67
68
69
# File 'app/models/activity_chain_type.rb', line 67

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_countInteger

Returns:

  • (Integer)


72
73
74
# File 'app/models/activity_chain_type.rb', line 72

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