Class: MailManager::Message
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- MailManager::Message
- Includes:
- StatusHistory
- Defined in:
- app/models/mail_manager/message.rb
Instance Method Summary collapse
-
#active_or_deleted_contact ⇒ Object
return a contact whether its deleted or not.
-
#can_deliver? ⇒ Boolean
whether or not you can deliver a message.
-
#contactable ⇒ Object
returns the contact’s ‘contactable’ object tied to the contact.
-
#deliver ⇒ Object
sends the message through Mailer.
-
#email_address ⇒ Object
returns the contact’s email address.
-
#email_address_with_name ⇒ Object
returns a string in the form of “contact name” <[email protected]> if the contact’s full name returns anything …
-
#from_email_address ⇒ Object
the “From: ” email address for the email lazy sets the from email addres if not present from the mailing.
-
#full_name ⇒ Object
returns the contact’s full name.
-
#generate_guid ⇒ Object
generated the guid for which the message is identified by in transit.
-
#initialize(*args) ⇒ Message
constructor
A new instance of Message.
-
#parts ⇒ Object
returns the separate mime parts of the message’s Mailable.
-
#subject ⇒ Object
returns the mailings subject.
-
#substitutions ⇒ Object
returns a hash of substitutions to be used to modify the mailable’s html/plaing text.
-
#unsubscribe_url ⇒ Object
the full url to unsubscribe based on this message; including site url & guid.
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_contact ⇒ Object
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
90 91 92 |
# File 'app/models/mail_manager/message.rb', line 90 def can_deliver? ['ready','pending'].include?(status) end |
#contactable ⇒ Object
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 |
#deliver ⇒ Object
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.(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_address ⇒ Object
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_name ⇒ Object
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_address ⇒ Object
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_name ⇒ Object
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_guid ⇒ Object
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 |
#parts ⇒ Object
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 |
#subject ⇒ Object
returns the mailings subject
111 112 113 |
# File 'app/models/mail_manager/message.rb', line 111 def subject mailing.subject end |
#substitutions ⇒ Object
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_url ⇒ Object
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 |