Class: Subscriber
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
Models::Auditable::ALWAYS_IGNORED
Instance Attribute Summary collapse
#creator, #updater
Class Method Summary
collapse
Instance Method Summary
collapse
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
#publish_event
Instance Attribute Details
#customer_id ⇒ Object
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_address ⇒ Object
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_customer ⇒ Object
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
.active ⇒ ActiveRecord::Relation<Subscriber>
A relation of Subscribers that are active. Active Record Scope
44
|
# File 'app/models/subscriber.rb', line 44
scope :active, -> { where(active: true) }
|
.inactive ⇒ ActiveRecord::Relation<Subscriber>
A relation of Subscribers that are inactive. Active Record Scope
45
|
# File 'app/models/subscriber.rb', line 45
scope :inactive, -> { where(active: false) }
|
.with_customer ⇒ ActiveRecord::Relation<Subscriber>
A relation of Subscribers that are with customer. Active Record Scope
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
#activate ⇒ Object
61
62
63
|
# File 'app/models/subscriber.rb', line 61
def activate
update(active: true)
end
|
#campaign_deliveries ⇒ ActiveRecord::Relation<CampaignDelivery>
37
|
# File 'app/models/subscriber.rb', line 37
has_many :campaign_deliveries
|
#campaigns ⇒ ActiveRecord::Relation<Campaign>
39
|
# File 'app/models/subscriber.rb', line 39
has_many :campaigns, through: :subscriber_list
|
38
|
# File 'app/models/subscriber.rb', line 38
has_many :contact_points, foreign_key: :detail, primary_key: :email_address
|
34
|
# File 'app/models/subscriber.rb', line 34
belongs_to :customer, optional: true
|
#deactivate ⇒ Object
65
66
67
|
# File 'app/models/subscriber.rb', line 65
def deactivate
update(active: false)
end
|
#determined_customer ⇒ Object
85
86
87
|
# File 'app/models/subscriber.rb', line 85
def determined_customer
customer || party_customer
end
|
35
|
# File 'app/models/subscriber.rb', line 35
belongs_to :email_preference, optional: true, primary_key: :email, foreign_key: :email_address
|
#name ⇒ Object
69
70
71
|
# File 'app/models/subscriber.rb', line 69
def name
party&.full_name
end
|
#party ⇒ Object
73
74
75
|
# File 'app/models/subscriber.rb', line 73
def party
contact_points.where.not(party_id: nil).first&.party
end
|
#party_customer ⇒ Object
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_missing ⇒ Object
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
|
33
|
# File 'app/models/subscriber.rb', line 33
belongs_to :subscriber_list, inverse_of: :subscribers, optional: true
|
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
|