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
Checks if the record is not new and if mailings are all gone.
-
#deliver!(mailing) ⇒ Object
Actually deliver and process the mail.
-
#end(reason = ::Caffeinate.config.default_ended_reason) ⇒ Object
Updates
ended_atand runson_completecallbacks. -
#end!(reason = ::Caffeinate.config.default_ended_reason) ⇒ Object
Updates
ended_atand runson_completecallbacks. -
#ended? ⇒ Boolean
Checks if the
CampaignSubscriptionis ended by checking the presence ofended_at. -
#refuel!(offset: :created_at) ⇒ Object
Add (new) drips to a
CampaignSubscriber. -
#resubscribe!(force = false) ⇒ Object
Updates
unsubscribed_atto nil and runson_subscribecallbacks. -
#subscribed? ⇒ Boolean
Checks if the
CampaignSubscriptionis not ended and not unsubscribed. -
#unsubscribe(reason = ::Caffeinate.config.default_unsubscribe_reason) ⇒ Object
Updates
unsubscribed_atand runson_subscribecallbacks. -
#unsubscribe!(reason = ::Caffeinate.config.default_unsubscribe_reason) ⇒ 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
Checks if the record is not new and if mailings are all gone.
155 156 157 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 155 def completed? caffeinate_mailings.unsent.count.zero? end |
#deliver!(mailing) ⇒ Object
Actually deliver and process the mail
81 82 83 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 81 def deliver!(mailing) caffeinate_campaign.to_dripper.deliver!(mailing) end |
#end(reason = ::Caffeinate.config.default_ended_reason) ⇒ Object
Updates ended_at and runs on_complete callbacks
112 113 114 115 116 117 118 119 120 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 112 def end(reason = ::Caffeinate.config.default_ended_reason) return false if unsubscribed? return true if ended? result = update(ended_at: ::Caffeinate.config.time_now, ended_reason: reason) caffeinate_campaign.to_dripper.run_callbacks(:on_end, self) result end |
#end!(reason = ::Caffeinate.config.default_ended_reason) ⇒ Object
Updates ended_at and runs on_complete callbacks
101 102 103 104 105 106 107 108 109 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 101 def end!(reason = ::Caffeinate.config.default_ended_reason) raise ::Caffeinate::InvalidState, 'CampaignSubscription is already unsubscribed.' if unsubscribed? return true if ended? 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
96 97 98 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 96 def ended? ended_at.present? end |
#refuel!(offset: :created_at) ⇒ Object
Add (new) drips to a CampaignSubscriber.
Useful if you added new drips to a Campaign and have existing CampaignSubscription which you want to add them to.
Pass :created_at if you want to offset ‘Mailing#send_at` time from the time the CampaignSubscription was originally created. That is to say that if you add a new drip for 5 days from now, the mailing will be sent 5 days from when the CampaignSubscription was created.
Pass :current to offset from the current time (doesn’t offset anything, actually)
74 75 76 77 78 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 74 def refuel!(offset: :created_at) ::CampaignSubscriptions::RefuelService.new(self, offset: offset).call true 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.
144 145 146 147 148 149 150 151 152 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 144 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
86 87 88 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 86 def subscribed? !ended? && !unsubscribed? end |
#unsubscribe(reason = ::Caffeinate.config.default_unsubscribe_reason) ⇒ Object
Updates unsubscribed_at and runs on_subscribe callbacks
133 134 135 136 137 138 139 140 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 133 def unsubscribe(reason = ::Caffeinate.config.default_unsubscribe_reason) 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 = ::Caffeinate.config.default_unsubscribe_reason) ⇒ Object
Updates unsubscribed_at and runs on_subscribe callbacks
123 124 125 126 127 128 129 130 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 123 def unsubscribe!(reason = ::Caffeinate.config.default_unsubscribe_reason) 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
91 92 93 |
# File 'app/models/caffeinate/campaign_subscription.rb', line 91 def unsubscribed? unsubscribed_at.present? end |