Class: Caffeinate::CampaignSubscription
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Caffeinate::CampaignSubscription
- Defined in:
- app/models/caffeinate/campaign_subscription.rb
Overview
CampaignSubscription associates an object and its optional user to a Campaign and its relevant Mailings.
Instance Method Summary collapse
- #completed? ⇒ Boolean
-
#deliver!(mailing) ⇒ Object
Actually deliver and process the mail.
-
#end(reason = nil) ⇒ Object
Updates
ended_atand runson_completecallbacks. -
#end!(reason = nil) ⇒ Object
Updates
ended_atand runson_completecallbacks. -
#ended? ⇒ Boolean
Checks if the
CampaignSubscriptionis ended by checking the presence ofended_at. -
#resubscribe!(force = false) ⇒ Object
Updates
unsubscribed_atto nil and runson_subscribecallbacks. -
#subscribed? ⇒ Boolean
Checks if the
CampaignSubscriptionis not ended and not unsubscribed. -
#unsubscribe(reason = nil) ⇒ Object
Updates
unsubscribed_atand runson_subscribecallbacks. -
#unsubscribe!(reason = nil) ⇒ Object
Updates
unsubscribed_atand runson_subscribecallbacks. -
#unsubscribed? ⇒ Boolean
Checks if the
CampaignSubscriptionis not subscribed by checking the presence ofunsubscribed_at.
Instance Method Details
#completed? ⇒ Boolean
140 141 142 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 140 def completed? caffeinate_mailings.unsent.count.zero? end |
#deliver!(mailing) ⇒ Object
Actually deliver and process the mail
57 58 59 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 57 def deliver!(mailing) caffeinate_campaign.to_dripper.deliver!(mailing) end |
#end(reason = nil) ⇒ Object
Updates ended_at and runs on_complete callbacks
87 88 89 90 91 92 93 94 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 87 def end(reason = nil) return false if unsubscribed? result = update(ended_at: ::Caffeinate.config.time_now, ended_reason: reason) caffeinate_campaign.to_dripper.run_callbacks(:on_end, self) result end |
#end!(reason = nil) ⇒ Object
Updates ended_at and runs on_complete callbacks
77 78 79 80 81 82 83 84 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 77 def end!(reason = nil) raise ::Caffeinate::InvalidState, 'CampaignSubscription is already unsubscribed.' if unsubscribed? update!(ended_at: ::Caffeinate.config.time_now, ended_reason: reason) caffeinate_campaign.to_dripper.run_callbacks(:on_end, self) true end |
#ended? ⇒ Boolean
Checks if the CampaignSubscription is ended by checking the presence of ended_at
72 73 74 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 72 def ended? ended_at.present? end |
#resubscribe!(force = false) ⇒ Object
Updates unsubscribed_at to nil and runs on_subscribe callbacks. Use force to forcefully reset. Does not create the mailings.
118 119 120 121 122 123 124 125 126 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 118 def resubscribe!(force = false) raise ::Caffeinate::InvalidState, 'CampaignSubscription is already ended.' if ended? && !force raise ::Caffeinate::InvalidState, 'CampaignSubscription is already unsubscribed.' if unsubscribed? && !force update!(unsubscribed_at: nil, resubscribed_at: ::Caffeinate.config.time_now) caffeinate_campaign.to_dripper.run_callbacks(:on_resubscribe, self) true end |
#subscribed? ⇒ Boolean
Checks if the CampaignSubscription is not ended and not unsubscribed
62 63 64 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 62 def subscribed? !ended? && !unsubscribed? end |
#unsubscribe(reason = nil) ⇒ Object
Updates unsubscribed_at and runs on_subscribe callbacks
107 108 109 110 111 112 113 114 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 107 def unsubscribe(reason = nil) return false if ended? result = update(unsubscribed_at: ::Caffeinate.config.time_now, unsubscribe_reason: reason) caffeinate_campaign.to_dripper.run_callbacks(:on_unsubscribe, self) result end |
#unsubscribe!(reason = nil) ⇒ Object
Updates unsubscribed_at and runs on_subscribe callbacks
97 98 99 100 101 102 103 104 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 97 def unsubscribe!(reason = nil) raise ::Caffeinate::InvalidState, 'CampaignSubscription is already ended.' if ended? update!(unsubscribed_at: ::Caffeinate.config.time_now, unsubscribe_reason: reason) caffeinate_campaign.to_dripper.run_callbacks(:on_unsubscribe, self) true end |
#unsubscribed? ⇒ Boolean
Checks if the CampaignSubscription is not subscribed by checking the presence of unsubscribed_at
67 68 69 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 67 def unsubscribed? unsubscribed_at.present? end |