Class: EmailPreference
Overview
== Schema Information
Table name: email_preferences
Database name: primary
id :integer not null, primary key
disable_announcements :boolean default(FALSE), not null
disable_events :boolean default(FALSE), not null
disable_newsletters :boolean default(FALSE), not null
disable_promotions :boolean default(FALSE), not null
disable_reviews :boolean default(FALSE), not null
disable_webinars :boolean default(FALSE), not null
email :string not null
last_delivery_status :string
last_delivery_status_at :datetime
last_delivery_status_notes :string
created_at :datetime
updated_at :datetime
creator_id :integer
updater_id :integer
Indexes
index_email_preferences_on_email (email) UNIQUE
Constant Summary
collapse
- ALL_STATES =
%w[suppressed dropped deferred bounced delivered spammed opened clicked unsubscribed].freeze
Models::Auditable::ALWAYS_IGNORED
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#all_skipped_columns, #audit_reference_data, #creator, #should_not_save_version, #stamp_record, #updater
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
#publish_event
Instance Attribute Details
#disable_all ⇒ Object
This is to allow the initial state on the form to be selected properly
61
62
63
|
# File 'app/models/email_preference.rb', line 61
def disable_all
@disable_all
end
|
#email ⇒ Object
30
|
# File 'app/models/email_preference.rb', line 30
validates :email, email_format: true, presence: true, uniqueness: true
|
Class Method Details
.can_receive_email_of_category(email, category) ⇒ Object
79
80
81
82
83
84
85
86
87
|
# File 'app/models/email_preference.rb', line 79
def self.can_receive_email_of_category(email, category)
ep = EmailPreference.find_by(email: email)
return true if ep.nil?
category_method = "disable_#{category}"
return true unless ep.respond_to?(category_method)
!ep.send(category_method)
end
|
.completely_unsubscribed ⇒ ActiveRecord::Relation<EmailPreference>
A relation of EmailPreferences that are completely unsubscribed. Active Record Scope
38
39
40
41
42
43
44
45
|
# File 'app/models/email_preference.rb', line 38
scope :completely_unsubscribed, -> {
where(disable_promotions: true,
disable_newsletters: true,
disable_announcements: true,
disable_events: true,
disable_webinars: true,
disable_reviews: true)
}
|
.decrypt_token(token) ⇒ Object
120
121
122
123
|
# File 'app/models/email_preference.rb', line 120
def self.decrypt_token(token)
res = Encryption.decrypt_string(token)
res.presence || ActiveSupport::MessageEncryptor.new(Heatwave::Configuration.fetch(:secret_key_base)).decrypt_and_verify(token)
end
|
.encrypt_email(email) ⇒ Object
125
126
127
|
# File 'app/models/email_preference.rb', line 125
def self.encrypt_email(email)
Encryption.encrypt_string(email)
end
|
89
90
91
92
|
# File 'app/models/email_preference.rb', line 89
def self.generate_email_preferences_form_url(email)
UrlHelper.instance.email_preferences_my_account_url(token: EmailPreference.encrypt_email(email), host: "https://#{WEB_HOSTNAME}", locale: false)
end
|
.import_suppressions(path) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
|
# File 'app/models/email_preference.rb', line 94
def self.import_suppressions(path)
CSV.foreach(path) do |row|
ep = find_or_create_by(email: row[0])
ep.update(disable_promotions: true,
disable_newsletters: true,
disable_announcements: true,
disable_events: true,
disable_webinars: true,
disable_reviews: true)
end
end
|
.unsubscribe(emails, disable: true) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'app/models/email_preference.rb', line 106
def self.unsubscribe(emails, disable: true)
email_preferences = EmailPreference.where(email: emails)
email_preferences.each do |ep|
ep.update(
disable_promotions: disable,
disable_newsletters: disable,
disable_announcements: disable,
disable_events: disable,
disable_webinars: disable,
disable_reviews: disable
)
end
end
|
Instance Method Details
#accounts ⇒ ActiveRecord::Relation<Account>
33
|
# File 'app/models/email_preference.rb', line 33
has_many :accounts, foreign_key: :email, primary_key: :email
|
#any? ⇒ Boolean
70
71
72
73
74
75
76
77
|
# File 'app/models/email_preference.rb', line 70
def any?
disable_promotions ||
disable_newsletters ||
disable_announcements ||
disable_events ||
disable_webinars ||
disable_reviews
end
|
#broadcast_updates ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'app/models/email_preference.rb', line 143
def broadcast_updates
event_hash = { email: email }
event_hash[:categories] = unsubscribed_categories_changes
if event_hash[:categories].present?
AfterCommitEverywhere.after_commit do
Rails.configuration.event_store.publish(
Events::EmailUnsubscribed.new(data: {
email: event_hash[:email],
categories: event_hash[:categories]
}),
stream_name: "EmailPreference-#{id}"
)
end
end
end
|
#communication_recipients ⇒ ActiveRecord::Relation<CommunicationRecipient>
34
|
# File 'app/models/email_preference.rb', line 34
has_many :communication_recipients, foreign_key: :detail, primary_key: :email
|
32
|
# File 'app/models/email_preference.rb', line 32
has_many :contact_points, foreign_key: :detail, primary_key: :email
|
#downcase_email ⇒ Object
133
134
135
|
# File 'app/models/email_preference.rb', line 133
def downcase_email
self.email = email&.downcase
end
|
#to_s ⇒ Object
129
130
131
|
# File 'app/models/email_preference.rb', line 129
def to_s
email
end
|
#unsubscribed_categories_changes ⇒ Object
137
138
139
140
141
|
# File 'app/models/email_preference.rb', line 137
def unsubscribed_categories_changes
saved_changes.select do |k, v|
k.starts_with?('disable_') && v.last == true
end.keys.map { |k| k.scan(/disable_(.*)/)[0][0].to_sym }
end
|