Class: MailManager::Message

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
StatusHistory
Defined in:
app/models/mail_manager/message.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StatusHistory

#change_status, included, #set_default_status, #status, #status=, #status_changed_at=

Constructor Details

#initialize(*args) ⇒ Message

Returns a new instance of Message.



48
49
50
51
# File 'app/models/mail_manager/message.rb', line 48

def initialize(*args)
  super
  set_type
end

Class Method Details

.email_address_hash_for_mailing_id(mailing_id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'app/models/mail_manager/message.rb', line 37

def self.email_address_hash_for_mailing_id(mailing_id)
  results = MailManager::Message.connection.execute(
    %Q|select distinct c.email_address 
    from #{MailManager.table_prefix}contacts c 
    inner join #{MailManager.table_prefix}messages m 
    on c.id=m.contact_id where m.mailing_id=#{mailing_id.to_i}|
  )
  results = results.map(&:values) if results.first.is_a?(Hash) 
  results.inject(Hash.new){|h,r|h.merge!(r[0].to_s.strip.downcase => true)}
end

Instance Method Details

#active_or_deleted_contactObject

return a contact whether its deleted or not



111
112
113
114
# File 'app/models/mail_manager/message.rb', line 111

def active_or_deleted_contact
  @active_or_deleted_contact ||= self.contact || MailManager::Contact.unscoped.
    where(id: self.contact_id).first
end

#can_deliver?Boolean

whether or not you can deliver a message

Returns:

  • (Boolean)


106
107
108
# File 'app/models/mail_manager/message.rb', line 106

def can_deliver?
  ['ready','pending'].include?(status) 
end

#contactableObject

returns the contact’s ‘contactable’ object tied to the contact



145
146
147
# File 'app/models/mail_manager/message.rb', line 145

def contactable
  active_or_deleted_contact.try(:contactable)
end

#deliver(cached_parts = nil) ⇒ Object

sends the message through Mailer



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/mail_manager/message.rb', line 83

def deliver(cached_parts=nil)
  # lock only needed until status is updated
  Lock.with_lock("deliver_message_#{self.id}") do
    reload
    if can_deliver?
      change_status(:processing)
    else
      Rails.logger.warn "Message(#{self.id})'s is no longer suitable to deliver.. staus: #{status}"
    end
  end
  cached_parts = if cached_parts.present? 
    mailing.parts(substitutions, cached_parts.dup)
  else
    parts
  end
  MailManager::Mailer.deliver_message(self,cached_parts)
  change_status(:sent)
# allow other errors to bubble up
rescue MailManager::LockException => e
  Rails.logger.warn "Locking error while trying to send MailManager::Message(#{id}) leaving in #{status} status"
end

#email_addressObject

returns the contact’s email address



122
123
124
# File 'app/models/mail_manager/message.rb', line 122

def email_address
  active_or_deleted_contact.try(:email_address)
end

#email_address_with_nameObject

returns a string in the form of “contact name” <[email protected]> if the contact’s full name returns anything … or simply [email protected] if there is no name at all



77
78
79
80
# File 'app/models/mail_manager/message.rb', line 77

def email_address_with_name
  return %Q|"#{full_name}" <#{email_address}>|.gsub(/\s+/,' ') unless full_name.eql?('')
  email_address
end

#from_email_addressObject

the “From: ” email address for the email lazy sets the from email addres if not present from the mailing



133
134
135
136
137
# File 'app/models/mail_manager/message.rb', line 133

def from_email_address
  return self[:from_email_address] if self[:from_email_address].present?
  self.update_attribute(:from_email_address,mailing.from_email_address)
  self[:from_email_address]
end

#full_nameObject

returns the contact’s full name



117
118
119
# File 'app/models/mail_manager/message.rb', line 117

def full_name
  active_or_deleted_contact.try(:full_name)
end

#generate_guidObject

generated the guid for which the message is identified by in transit



171
172
173
174
# File 'app/models/mail_manager/message.rb', line 171

def generate_guid
  update_attribute(:guid,       
    "#{active_or_deleted_contact.try(:id)}-#{subscription.try(:id)}-#{self.id}-#{Digest::SHA1.hexdigest("#{active_or_deleted_contact.try(:id)}-#{subscription.try(:id)}-#{self.id}-#{MailManager.secret}")}")
end

#partsObject

returns the separate mime parts of the message’s Mailable



140
141
142
# File 'app/models/mail_manager/message.rb', line 140

def parts
  @parts ||= mailing.parts(substitutions)
end

#subjectObject

returns the mailings subject



127
128
129
# File 'app/models/mail_manager/message.rb', line 127

def subject
  mailing.subject
end

#substitutionsObject

returns a hash of substitutions to be used to modify the mailable’s html/plaing text



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/mail_manager/message.rb', line 150

def substitutions
  substitutions_hash = {}
  MailManager::ContactableRegistry.registered_methods.each do |method|
    method_key = method.to_s.upcase
    if active_or_deleted_contact.respond_to?(method)
      substitutions_hash[method_key] = active_or_deleted_contact.send(method)
    elsif contactable.respond_to?(method)
      substitutions_hash[method_key] = contactable.send(method)
    else
      substitutions_hash[method_key] = ''
    end
  end
  substitutions_hash.merge('UNSUBSCRIBE_URL' => unsubscribe_url)
end

#unsubscribe_urlObject

the full url to unsubscribe based on this message; including site url & guid



166
167
168
# File 'app/models/mail_manager/message.rb', line 166

def unsubscribe_url
  "#{MailManager.site_url}#{MailManager.unsubscribe_path}/#{guid}"
end