Class: MailManager::Subscription
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- MailManager::Subscription
- Includes:
- StatusHistory
- Defined in:
- app/models/mail_manager/subscription.rb
Class Method Summary collapse
-
.change_subscription_status(contact, mailing_list, status) ⇒ Object
changes or creates a subscription for the given contact and list and assigns the given status.
- .fail_by_email_address(email_address) ⇒ Object
-
.subscribe(contact, mailing_list, status = 'active') ⇒ Object
subscribes the contact to the list.
-
.unsubscribe(contact, mailing_list) ⇒ Object
unsubscribes the contact from the list.
- .unsubscribe_by_email_address(email_address, message = nil) ⇒ Object
-
.unsubscribe_by_message_guid(guid) ⇒ Object
unsubscribes a contact from all lists by looking them up through a messages GUID FIXME: when we add more lists and the ability to have multiple subscriptions, this should remove only the list that is tied in the GUID and they should be linked to their options.
-
.unsubscribed_emails_hash ⇒ Object
acts_as_audited rescue Rails.logger.warn “Audit Table not defined!”.
Instance Method Summary collapse
Methods included from StatusHistory
#change_status, included, #set_default_status, #status, #status=, #status_changed_at=
Class Method Details
.change_subscription_status(contact, mailing_list, status) ⇒ Object
changes or creates a subscription for the given contact and list and assigns the given status
70 71 72 73 74 75 76 77 78 |
# File 'app/models/mail_manager/subscription.rb', line 70 def self.change_subscription_status(contact, mailing_list, status) subscription = self.find_by_contact_id_and_mailing_list_id(contact.id, mailing_list.id) return subscription.change_status(status) if subscription subscription = Subscription.new subscription.contact = contact subscription.mailing_list = mailing_list subscription.change_status(status) subscription end |
.fail_by_email_address(email_address) ⇒ Object
122 123 124 125 126 127 128 |
# File 'app/models/mail_manager/subscription.rb', line 122 def self.fail_by_email_address(email_address) Contact.find_all_by_email_address(email_address).each do |contact| contact.active_subscriptions.each do |subscription| subscription.change_status(:failed_address) end end end |
.subscribe(contact, mailing_list, status = 'active') ⇒ Object
subscribes the contact to the list
81 82 83 |
# File 'app/models/mail_manager/subscription.rb', line 81 def self.subscribe(contact, mailing_list, status='active') change_subscription_status(contact, mailing_list, status) end |
.unsubscribe(contact, mailing_list) ⇒ Object
unsubscribes the contact from the list
86 87 88 |
# File 'app/models/mail_manager/subscription.rb', line 86 def self.unsubscribe(contact, mailing_list) change_subscription_status(contact, mailing_list, 'unsubscribed') end |
.unsubscribe_by_email_address(email_address, message = nil) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/models/mail_manager/subscription.rb', line 130 def self.unsubscribe_by_email_address(email_address,=nil) subscriptions = [] Contact.find_all_by_email_address(email_address).each do |contact| subscriptions += contact.active_subscriptions.each do |subscription| subscription.change_status(:unsubscribed) end end Mailer.delay.unsubscribed(subscriptions,email_address, subscriptions.first.contact, ) if \ subscriptions.present? subscriptions end |
.unsubscribe_by_message_guid(guid) ⇒ Object
unsubscribes a contact from all lists by looking them up through a messages GUID FIXME: when we add more lists and the ability to have multiple subscriptions, this should remove only the list that is tied in the GUID and they should be linked to their options
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'app/models/mail_manager/subscription.rb', line 105 def self.(guid) = Message.find_by_guid(guid) contact = .contact if begin unsubscribed_subscriptions = self.unsubscribe_by_email_address(.contact.email_address, ) return unsubscribed_subscriptions rescue => e Rails.logger.warn "Error Unsubscribing email: #{.contact.email_address}\n#{e.}\n #{e.backtrace.join("\n ")}" raise "An error occured." end else raise "Could not find your subscription!" end nil end |
.unsubscribed_emails_hash ⇒ Object
acts_as_audited rescue Rails.logger.warn “Audit Table not defined!”
55 56 57 58 59 60 61 62 63 |
# File 'app/models/mail_manager/subscription.rb', line 55 def self.unsubscribed_emails_hash results = self.connection.execute(%Q|select distinct c.email_address from #{MailManager.table_prefix}contacts c inner join #{MailManager.table_prefix}subscriptions s on c.id=s.contact_id where s.status in ('unsubscribed')| ) 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? ⇒ Boolean
94 95 96 |
# File 'app/models/mail_manager/subscription.rb', line 94 def active? status.eql?('active') end |
#contact_full_name ⇒ Object
65 66 67 |
# File 'app/models/mail_manager/subscription.rb', line 65 def contact_full_name contact.full_name end |
#mailing_list_name ⇒ Object
90 91 92 |
# File 'app/models/mail_manager/subscription.rb', line 90 def mailing_list_name mailing_list.try(:name) end |
#pending? ⇒ Boolean
98 99 100 |
# File 'app/models/mail_manager/subscription.rb', line 98 def pending? status.eql?('pending') end |