Class: Caffeinate::CampaignSubscription

Inherits:
ApplicationRecord show all
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

Instance Method Details

#completed?Boolean

Checks if the record is not new and if mailings are all gone.

Returns:

  • (Boolean)


154
155
156
# File 'app/models/caffeinate/campaign_subscription.rb', line 154

def completed?
  caffeinate_mailings.unsent.count.zero?
end

#deliver!(mailing) ⇒ Object

Actually deliver and process the mail



80
81
82
# File 'app/models/caffeinate/campaign_subscription.rb', line 80

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



111
112
113
114
115
116
117
118
119
# File 'app/models/caffeinate/campaign_subscription.rb', line 111

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



100
101
102
103
104
105
106
107
108
# File 'app/models/caffeinate/campaign_subscription.rb', line 100

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`

Returns:

  • (Boolean)


95
96
97
# File 'app/models/caffeinate/campaign_subscription.rb', line 95

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)



73
74
75
76
77
# File 'app/models/caffeinate/campaign_subscription.rb', line 73

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.



143
144
145
146
147
148
149
150
151
# File 'app/models/caffeinate/campaign_subscription.rb', line 143

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

Returns:

  • (Boolean)


85
86
87
# File 'app/models/caffeinate/campaign_subscription.rb', line 85

def subscribed?
  !ended? && !unsubscribed?
end

#unsubscribe(reason = ::Caffeinate.config.default_unsubscribe_reason) ⇒ Object

Updates ‘unsubscribed_at` and runs `on_subscribe` callbacks



132
133
134
135
136
137
138
139
# File 'app/models/caffeinate/campaign_subscription.rb', line 132

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



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

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`

Returns:

  • (Boolean)


90
91
92
# File 'app/models/caffeinate/campaign_subscription.rb', line 90

def unsubscribed?
  unsubscribed_at.present?
end