Class: Caffeinate::Mailing

Inherits:
ApplicationRecord show all
Defined in:
app/models/caffeinate/mailing.rb

Overview

Records of the mails sent and to be sent for a given ‘::Caffeinate::CampaignSubscriber`

Constant Summary collapse

CURRENT_THREAD_KEY =
:current_caffeinate_mailing.freeze

Instance Method Summary collapse

Instance Method Details

#deliver!Object

Delivers the Mailing in the foreground



99
100
101
# File 'app/models/caffeinate/mailing.rb', line 99

def deliver!
  caffeinate_campaign_subscription.deliver!(self)
end

#deliver_later!Object

Delivers the Mailing in the background



104
105
106
107
108
109
110
111
112
113
# File 'app/models/caffeinate/mailing.rb', line 104

def deliver_later!
  klass = ::Caffeinate.config.mailing_job_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

#dripObject

TODO:

This can be optimized with a better cache

The associated drip



67
68
69
# File 'app/models/caffeinate/mailing.rb', line 67

def drip
  @drip ||= caffeinate_campaign.to_dripper.drip_collection[mailer_action]
end

#from_drip(drip) ⇒ Object

Assigns attributes to the Mailing from the Drip



82
83
84
85
86
87
# File 'app/models/caffeinate/mailing.rb', line 82

def from_drip(drip)
  self.send_at = drip.send_at
  self.mailer_class = drip.options[:mailer_class]
  self.mailer_action = drip.action
  self
end

#pending?Boolean

Checks if the Mailing is not skipped and not sent

Returns:

  • (Boolean)


34
35
36
# File 'app/models/caffeinate/mailing.rb', line 34

def pending?
  unskipped? && unsent?
end

#process!Object

Handles the logic for delivery and delivers



90
91
92
93
94
95
96
# File 'app/models/caffeinate/mailing.rb', line 90

def process!
  if ::Caffeinate.config.async_delivery?
    deliver_later!
  else
    deliver!
  end
end

#sent?Boolean

Checks if the Mailing is sent

Returns:

  • (Boolean)


49
50
51
# File 'app/models/caffeinate/mailing.rb', line 49

def sent?
  sent_at.present?
end

#skip!Object

Updates ‘skipped_at and runs `on_skip` callbacks



59
60
61
62
63
# File 'app/models/caffeinate/mailing.rb', line 59

def skip!
  update!(skipped_at: Caffeinate.config.time_now)

  caffeinate_campaign.to_dripper.run_callbacks(:on_skip, caffeinate_campaign_subscription, self)
end

#skipped?Boolean

Checks if the Mailing is skipped

Returns:

  • (Boolean)


39
40
41
# File 'app/models/caffeinate/mailing.rb', line 39

def skipped?
  skipped_at.present?
end

#subscriberObject

The associated Subscriber from ‘::Caffeinate::CampaignSubscription`



72
73
74
# File 'app/models/caffeinate/mailing.rb', line 72

def subscriber
  caffeinate_campaign_subscription.subscriber
end

#unsent?Boolean

Checks if the Mailing is not sent

Returns:

  • (Boolean)


54
55
56
# File 'app/models/caffeinate/mailing.rb', line 54

def unsent?
  !sent?
end

#unskipped?Boolean

Checks if the Mailing is not skipped

Returns:

  • (Boolean)


44
45
46
# File 'app/models/caffeinate/mailing.rb', line 44

def unskipped?
  !skipped?
end

#userObject

The associated Subscriber from ‘::Caffeinate::CampaignSubscription`



77
78
79
# File 'app/models/caffeinate/mailing.rb', line 77

def user
  caffeinate_campaign_subscription.user
end