Class: MailManager::Contact

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Deleteable, MailManager::ContactableRegistry::Contactable
Defined in:
app/models/mail_manager/contact.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deleteable

#delete, included, #is_deleted?, #undelete

Methods included from MailManager::ContactableRegistry::Contactable

#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

.extract_contact_id(token) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'app/models/mail_manager/contact.rb', line 115

def self.extract_contact_id(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::extract_contact_id(token))
end

.inject_contact_id(token, id) ⇒ Object



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.(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

Returns:

  • (Boolean)


133
134
135
# File 'app/models/mail_manager/contact.rb', line 133

def authorized?(token)
  .eql?(token) and  > 2.days.ago
end

#contactObject



22
23
24
# File 'app/models/mail_manager/contact.rb', line 22

def contact
  self
end

#deleted?Boolean

Returns:

  • (Boolean)


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

def deleted?
  !deleted_at.nil?
end

#deliver_double_opt_inObject



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_urlObject



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}/#{}"
end

#email_address_with_nameObject



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_nameObject



68
69
70
# File 'app/models/mail_manager/contact.rb', line 68

def full_name
  "#{first_name} #{last_name}".strip
end

#generate_login_tokenObject

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 
  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_subscriptionsObject



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_tokenObject



129
130
131
# File 'app/models/mail_manager/contact.rb', line 129

def 
  self[:login_token] ||= 
end

#trim_fieldsObject



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