Class: Caffeinate::Mailing
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Caffeinate::Mailing
- Defined in:
- app/models/caffeinate/mailing.rb
Overview
Records of the mails sent and to be sent for a given ::Caffeinate::CampaignSubscriber
Class Method Summary collapse
Instance Method Summary collapse
-
#deliver! ⇒ Object
Delivers the Mailing in the foreground.
-
#deliver_later! ⇒ Object
Delivers the Mailing in the background.
-
#drip ⇒ Object
The associated drip.
- #end_if_no_mailings! ⇒ Object
-
#from_drip(drip) ⇒ Object
Assigns attributes to the Mailing from the Drip.
- #initialize_dup(args) ⇒ Object
-
#pending? ⇒ Boolean
Checks if the Mailing is not skipped and not sent.
-
#process! ⇒ Object
Handles the logic for delivery and delivers.
-
#sent? ⇒ Boolean
Checks if the Mailing is sent.
-
#skip! ⇒ Object
Updates ‘skipped_at and runs
on_skipcallbacks. -
#skipped? ⇒ Boolean
Checks if the Mailing is skipped.
-
#subscriber ⇒ Object
The associated Subscriber from
::Caffeinate::CampaignSubscription. -
#unsent? ⇒ Boolean
Checks if the Mailing is not sent.
-
#unskipped? ⇒ Boolean
Checks if the Mailing is not skipped.
-
#user ⇒ Object
The associated Subscriber from
::Caffeinate::CampaignSubscription.
Class Method Details
.find_or_initialize_from_drip(campaign_subscription, drip) ⇒ Object
36 37 38 39 40 41 42 |
# File 'app/models/caffeinate/mailing.rb', line 36 def self.find_or_initialize_from_drip(campaign_subscription, drip) find_or_initialize_by( caffeinate_campaign_subscription: campaign_subscription, mailer_class: drip.[:mailer_class], mailer_action: drip.action ) end |
Instance Method Details
#deliver! ⇒ Object
Delivers the Mailing in the foreground
118 119 120 |
# File 'app/models/caffeinate/mailing.rb', line 118 def deliver! caffeinate_campaign_subscription.deliver!(self) end |
#deliver_later! ⇒ Object
Delivers the Mailing in the background
123 124 125 126 127 128 129 130 131 132 |
# File 'app/models/caffeinate/mailing.rb', line 123 def deliver_later! klass = ::Caffeinate.config.async_delivery_class if klass.respond_to?(:perform_later) klass.perform_later(id) elsif klass.respond_to?(:perform_async) klass.perform_async(id) else raise NoMethodError, "Neither perform_later or perform_async are defined on #{klass}." end end |
#drip ⇒ Object
The associated drip
84 85 86 |
# File 'app/models/caffeinate/mailing.rb', line 84 def drip @drip ||= caffeinate_campaign.to_dripper.drip_collection.for(mailer_action) end |
#end_if_no_mailings! ⇒ Object
134 135 136 |
# File 'app/models/caffeinate/mailing.rb', line 134 def end_if_no_mailings! caffeinate_campaign_subscription.end! if caffeinate_campaign_subscription.future_mailings.empty? end |
#from_drip(drip) ⇒ Object
Assigns attributes to the Mailing from the Drip
99 100 101 102 103 104 |
# File 'app/models/caffeinate/mailing.rb', line 99 def from_drip(drip) self.send_at = drip.send_at(self) self.mailer_class = drip.[:mailer_class] || drip.[:action_class] self.mailer_action = drip.action self end |
#initialize_dup(args) ⇒ Object
44 45 46 47 48 49 |
# File 'app/models/caffeinate/mailing.rb', line 44 def initialize_dup(args) super self.send_at = nil self.sent_at = nil self.skipped_at = nil end |
#pending? ⇒ Boolean
Checks if the Mailing is not skipped and not sent
52 53 54 |
# File 'app/models/caffeinate/mailing.rb', line 52 def pending? unskipped? && unsent? end |
#process! ⇒ Object
Handles the logic for delivery and delivers
107 108 109 110 111 112 113 114 115 |
# File 'app/models/caffeinate/mailing.rb', line 107 def process! if ::Caffeinate.config.async_delivery? deliver_later! else deliver! end caffeinate_campaign_subscription.touch end |
#sent? ⇒ Boolean
Checks if the Mailing is sent
67 68 69 |
# File 'app/models/caffeinate/mailing.rb', line 67 def sent? sent_at.present? end |
#skip! ⇒ Object
Updates ‘skipped_at and runs on_skip callbacks
77 78 79 80 81 |
# File 'app/models/caffeinate/mailing.rb', line 77 def skip! update!(skipped_at: Caffeinate.config.time_now) caffeinate_campaign.to_dripper.run_callbacks(:on_skip, self) end |
#skipped? ⇒ Boolean
Checks if the Mailing is skipped
57 58 59 |
# File 'app/models/caffeinate/mailing.rb', line 57 def skipped? skipped_at.present? end |
#subscriber ⇒ Object
The associated Subscriber from ::Caffeinate::CampaignSubscription
89 90 91 |
# File 'app/models/caffeinate/mailing.rb', line 89 def subscriber caffeinate_campaign_subscription.subscriber end |
#unsent? ⇒ Boolean
Checks if the Mailing is not sent
72 73 74 |
# File 'app/models/caffeinate/mailing.rb', line 72 def unsent? !sent? end |
#unskipped? ⇒ Boolean
Checks if the Mailing is not skipped
62 63 64 |
# File 'app/models/caffeinate/mailing.rb', line 62 def unskipped? !skipped? end |
#user ⇒ Object
The associated Subscriber from ::Caffeinate::CampaignSubscription
94 95 96 |
# File 'app/models/caffeinate/mailing.rb', line 94 def user caffeinate_campaign_subscription.user end |