Class: MailManager::Message

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

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.



37
38
39
40
# File 'app/models/mail_manager/message.rb', line 37

def initialize(*args)
  super
  set_type
end

Instance Method Details

#active_or_deleted_contactObject

return a contact whether its deleted or not



95
96
97
98
# File 'app/models/mail_manager/message.rb', line 95

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)


90
91
92
# File 'app/models/mail_manager/message.rb', line 90

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

#contactableObject

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



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

def contactable
  active_or_deleted_contact.try(:contactable)
end

#deliverObject

sends the message through Mailer



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/mail_manager/message.rb', line 72

def deliver
  # 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
  MailManager::Mailer.deliver_message(self)
  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



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

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



66
67
68
69
# File 'app/models/mail_manager/message.rb', line 66

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



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

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



101
102
103
# File 'app/models/mail_manager/message.rb', line 101

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



155
156
157
158
# File 'app/models/mail_manager/message.rb', line 155

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



124
125
126
# File 'app/models/mail_manager/message.rb', line 124

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

#subjectObject

returns the mailings subject



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

def subject
  mailing.subject
end

#substitutionsObject

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/mail_manager/message.rb', line 134

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



150
151
152
# File 'app/models/mail_manager/message.rb', line 150

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