Class: Subscriber

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

Overview

== Schema Information

Table name: subscribers
Database name: primary

id :integer not null, primary key
active :boolean default(TRUE), not null
email_address :string
reconciled :boolean default(FALSE), not null
created_at :datetime not null
updated_at :datetime not null
creator_id :integer
customer_id :integer
subscriber_list_id :integer
updater_id :integer

Indexes

idx_unique_subscribers (subscriber_list_id,customer_id) UNIQUE
index_subscribers_on_active (active)
index_subscribers_on_customer_id (customer_id)
index_subscribers_on_email_address (email_address)

Foreign Keys

fk_rails_... (customer_id => parties.id) ON DELETE => cascade
fk_rails_... (subscriber_list_id => subscriber_lists.id) ON DELETE => cascade

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

Has many collapse

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

#customer_idObject (readonly)



42
# File 'app/models/subscriber.rb', line 42

validates :customer_id, presence: true, uniqueness: { scope: :subscriber_list_id, message: 'can only be present once on each subscriber list' }, if: proc { |s| s.subscriber_list.customer? }

#email_addressObject (readonly)



41
# File 'app/models/subscriber.rb', line 41

validates :email_address, email_format: true, presence: true, uniqueness: { scope: :subscriber_list_id, message: 'can only be present once on each subscriber list' }, if: proc { |s| s.subscriber_list.email? }

#update_customerObject

Returns the value of attribute update_customer.



53
54
55
# File 'app/models/subscriber.rb', line 53

def update_customer
  @update_customer
end

Class Method Details

.activeActiveRecord::Relation<Subscriber>

A relation of Subscribers that are active. Active Record Scope

Returns:

See Also:



44
# File 'app/models/subscriber.rb', line 44

scope :active, -> { where(active: true) }

.inactiveActiveRecord::Relation<Subscriber>

A relation of Subscribers that are inactive. Active Record Scope

Returns:

See Also:



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

scope :inactive, -> { where(active: false) }

.with_customerActiveRecord::Relation<Subscriber>

A relation of Subscribers that are with customer. Active Record Scope

Returns:

See Also:



47
48
49
50
51
# File 'app/models/subscriber.rb', line 47

scope :with_customer, -> {
  joins("left outer JOIN contact_points on contact_points.id = (select max(id) from contact_points where contact_points.detail = subscribers.email_address)
		left outer JOIN parties contacts on contacts.id = contact_points.party_id and contacts.type = 'Contact'
		left outer JOIN parties customers on customers.id = coalesce(subscribers.customer_id, contacts.customer_id, contact_points.party_id)")
}

Instance Method Details

#activateObject



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

def activate
  update(active: true)
end

#campaign_deliveriesActiveRecord::Relation<CampaignDelivery>

Returns:

See Also:



37
# File 'app/models/subscriber.rb', line 37

has_many :campaign_deliveries

#campaignsActiveRecord::Relation<Campaign>

Returns:

See Also:



39
# File 'app/models/subscriber.rb', line 39

has_many :campaigns, through: :subscriber_list

#contact_pointsActiveRecord::Relation<ContactPoint>

Returns:

See Also:



38
# File 'app/models/subscriber.rb', line 38

has_many :contact_points, foreign_key: :detail, primary_key: :email_address

#customerCustomer

Returns:

See Also:



34
# File 'app/models/subscriber.rb', line 34

belongs_to :customer, optional: true

#deactivateObject



65
66
67
# File 'app/models/subscriber.rb', line 65

def deactivate
  update(active: false)
end

#determined_customerObject



85
86
87
# File 'app/models/subscriber.rb', line 85

def determined_customer
  customer || party_customer
end

#email_preferenceEmailPreference



35
# File 'app/models/subscriber.rb', line 35

belongs_to :email_preference, optional: true, primary_key: :email, foreign_key: :email_address

#nameObject



69
70
71
# File 'app/models/subscriber.rb', line 69

def name
  party&.full_name
end

#partyObject



73
74
75
# File 'app/models/subscriber.rb', line 73

def party
  contact_points.where.not(party_id: nil).first&.party
end

#party_customerObject



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

def party_customer
  if party.respond_to?(:customer)
    party.customer
  else
    party
  end
end

#populate_email_if_missingObject



93
94
95
96
97
98
99
# File 'app/models/subscriber.rb', line 93

def populate_email_if_missing
  return if email_address.present?
  return unless subscriber_list.email?
  return unless customer

  self.email_address = customer.email
end

#subscriber_listSubscriberList



33
# File 'app/models/subscriber.rb', line 33

belongs_to :subscriber_list, inverse_of: :subscribers, optional: true

#update_linked_contact_pointsObject



89
90
91
# File 'app/models/subscriber.rb', line 89

def update_linked_contact_points
  ContactPoint.where(detail: email_address_was).update_all(detail: email_address)
end