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`

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_initialize_from_drip(campaign_subscription, drip) ⇒ Object



35
36
37
38
39
40
41
# File 'app/models/caffeinate/mailing.rb', line 35

def self.find_or_initialize_from_drip(campaign_subscription, drip)
  find_or_initialize_by(
    caffeinate_campaign_subscription: campaign_subscription,
    mailer_class: drip.options[:mailer_class],
    mailer_action: drip.action
  )
end

Instance Method Details

#deliver!Object

Delivers the Mailing in the foreground



117
118
119
# File 'app/models/caffeinate/mailing.rb', line 117

def deliver!
  caffeinate_campaign_subscription.deliver!(self)
end

#deliver_later!Object

Delivers the Mailing in the background



122
123
124
125
126
127
128
129
130
131
# File 'app/models/caffeinate/mailing.rb', line 122

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

#dripObject

The associated drip



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

def drip
  @drip ||= caffeinate_campaign.to_dripper.drip_collection.for(mailer_action)
end

#end_if_no_mailings!Object



133
134
135
# File 'app/models/caffeinate/mailing.rb', line 133

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



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

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

#initialize_dup(args) ⇒ Object



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

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

Returns:

  • (Boolean)


51
52
53
# File 'app/models/caffeinate/mailing.rb', line 51

def pending?
  unskipped? && unsent?
end

#process!Object

Handles the logic for delivery and delivers



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

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

Returns:

  • (Boolean)


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

def sent?
  sent_at.present?
end

#skip!Object

Updates ‘skipped_at and runs `on_skip` callbacks



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

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

Returns:

  • (Boolean)


56
57
58
# File 'app/models/caffeinate/mailing.rb', line 56

def skipped?
  skipped_at.present?
end

#subscriberObject

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



88
89
90
# File 'app/models/caffeinate/mailing.rb', line 88

def subscriber
  caffeinate_campaign_subscription.subscriber
end

#unsent?Boolean

Checks if the Mailing is not sent

Returns:

  • (Boolean)


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

def unsent?
  !sent?
end

#unskipped?Boolean

Checks if the Mailing is not skipped

Returns:

  • (Boolean)


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

def unskipped?
  !skipped?
end

#userObject

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



93
94
95
# File 'app/models/caffeinate/mailing.rb', line 93

def user
  caffeinate_campaign_subscription.user
end