Class: MailManager::Contact
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Deleteable
#delete, included, #is_deleted?, #undelete
#active_subscriptions, #change_subscription_status, #contactable_method, #contactable_value, #destroy, #get_subscription_atttributes_for_subscription, included, #reload, #save, #set_contactable_data, #subscribe, #subscription_status_for, #subscriptions, #unsubscribe, #update_contactable_data, #update_subscription_data
Class Method Details
115
116
117
118
119
120
121
122
123
|
# File 'app/models/mail_manager/contact.rb', line 115
def self.(token)
token = token.split('')
id_string = ""
0.upto(token.length - 41) do |index|
token.shift
id_string << token.shift
end
id_string.to_i
end
|
.find_by_token(token) ⇒ Object
125
126
127
|
# File 'app/models/mail_manager/contact.rb', line 125
def self.find_by_token(token)
Contact.find_by_id(Contact::(token))
end
|
104
105
106
107
108
109
110
111
112
113
|
# File 'app/models/mail_manager/contact.rb', line 104
def self.inject_contact_id(token,id)
token = token.split('')
id_string = id.to_s.split('')
new_token = ""
0.upto(39) do |index|
new_token << token.pop
new_token << id_string.shift unless id_string.blank?
end
new_token
end
|
.signup(params) ⇒ Object
76
77
78
79
80
81
82
|
# File 'app/models/mail_manager/contact.rb', line 76
def self.signup(params)
contact = Contact.active.find_by_email_address(params['email_address'])
contact ||= Contact.new
Rails.logger.debug "Updating contact(#{contact.new_record? ? "New" : contact.id}) params: #{params.inspect}"
contact.update_attributes(params)
contact
end
|
Instance Method Details
#authorized?(token) ⇒ Boolean
133
134
135
|
# File 'app/models/mail_manager/contact.rb', line 133
def authorized?(token)
login_token.eql?(token) and login_token_created_at > 2.days.ago
end
|
22
23
24
|
# File 'app/models/mail_manager/contact.rb', line 22
def contact
self
end
|
#deleted? ⇒ Boolean
72
73
74
|
# File 'app/models/mail_manager/contact.rb', line 72
def deleted?
!deleted_at.nil?
end
|
#deliver_double_opt_in ⇒ Object
96
97
98
|
# File 'app/models/mail_manager/contact.rb', line 96
def deliver_double_opt_in
Mailer.double_opt_in(self).deliver
end
|
#double_opt_in(mailing_list_ids) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
|
# File 'app/models/mail_manager/contact.rb', line 84
def double_opt_in(mailing_list_ids)
previously_active_subscriptions = subscriptions.select(&:"active?")
new_subscriptions = subscriptions.select do |s|
mailing_list_ids.include?(s.mailing_list_id.to_s)
end
if new_subscriptions.present?
new_subscriptions.each{|subscription| subscription.change_status(:pending)}
self.delay.deliver_double_opt_in
end
nil
end
|
#double_opt_in_url ⇒ Object
100
101
102
|
# File 'app/models/mail_manager/contact.rb', line 100
def double_opt_in_url
"#{MailManager.site_url}#{MailManager.double_opt_in_path}/#{login_token}"
end
|
#email_address_with_name ⇒ Object
63
64
65
66
|
# File 'app/models/mail_manager/contact.rb', line 63
def email_address_with_name
return %Q|"#{full_name}" <#{email_address}>|.gsub(/\s+/,' ') unless full_name.eql?('')
email_address
end
|
#full_name ⇒ Object
68
69
70
|
# File 'app/models/mail_manager/contact.rb', line 68
def full_name
"#{first_name} #{last_name}".strip
end
|
#generate_login_token ⇒ Object
generated the token for which an opt-in is emailed
153
154
155
156
157
158
159
160
|
# File 'app/models/mail_manager/contact.rb', line 153
def generate_login_token
time = Time.now
token = Contact::inject_contact_id("#{Digest::SHA1.hexdigest(
"#{self.id}#{::MailManager.secret}#{time}")}", self.id)
self.update_attribute(:login_token, token)
self.update_attribute(:login_token_created_at, time)
token
end
|
#initialize_subscriptions ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'app/models/mail_manager/contact.rb', line 137
def initialize_subscriptions
@subscriptions = new_record? ? [] : Subscription.find_all_by_contact_id(self.id)
MailingList.active.each do |list|
next if @subscriptions.detect{|subscription| subscription.mailing_list_id.eql?(list.id) }
Rails.logger.warn "Building Subscription for Mailing List #{list.name}"
subscription = Subscription.new(:contact => self)
subscription.mailing_list_id = list.id
subscription.change_status((self.new_record? and list.defaults_to_active?) ? :active : :pending,false)
@subscriptions << subscription
end
@subscriptions = subscriptions.reject{|subscription| subscription.mailing_list.try(:inactive?) or
subscription.mailing_list.nil?}.sort_by{|subscription|
subscription.mailing_list.name.downcase}
end
|
#login_token ⇒ Object
129
130
131
|
# File 'app/models/mail_manager/contact.rb', line 129
def login_token
self[:login_token] ||= generate_login_token
end
|
#trim_fields ⇒ Object
26
27
28
29
30
|
# File 'app/models/mail_manager/contact.rb', line 26
def trim_fields
[:first_name, :last_name, :email_address].each do |field|
self[field].gsub!(/\A\s*|\s*\Z/,'') if self[field].present?
end
end
|