Class: Caffeinate::Campaign
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Caffeinate::Campaign
- Defined in:
- app/models/caffeinate/campaign.rb
Overview
Campaign ties together subscribers and mailings, and provides one core model for handling your Drippers.
Defined Under Namespace
Classes: NoSubscription
Class Method Summary collapse
-
.[](val) ⇒ Object
Convenience method for find_by!(slug: value).
Instance Method Summary collapse
-
#subscribe(subscriber, **args) ⇒ Object
Creates a ‘CampaignSubscription` object for the present Campaign.
-
#subscribe!(subscriber, **args) ⇒ Object
Subscribes an object to a campaign.
-
#subscriber(record, **args) ⇒ Object
Checks to see if the subscriber exists.
-
#subscribes?(record, **args) ⇒ Boolean
Check if the subscriber exists.
-
#to_dripper ⇒ Object
Poorly-named Campaign class resolver.
-
#unsubscribe(subscriber, **args) ⇒ Object
Unsubscribes an object from a campaign.
- #unsubscribe!(subscriber, **args) ⇒ Object
Class Method Details
.[](val) ⇒ Object
Convenience method for find_by!(slug: value)
::Caffeinate::Campaign[:onboarding]
# is the same as
::Caffeinate::Campaign.find_by(slug: :onboarding)
36 37 38 |
# File 'app/models/caffeinate/campaign.rb', line 36 def self.[](val) find_by!(slug: val) end |
Instance Method Details
#subscribe(subscriber, **args) ⇒ Object
Creates a ‘CampaignSubscription` object for the present Campaign. Allows passing `**args` to delegate additional arguments to the record. Uses `find_or_create_by`.
If a subscription hasn’t ended, any existing subscription will be returned.
79 80 81 |
# File 'app/models/caffeinate/campaign.rb', line 79 def subscribe(subscriber, **args) caffeinate_campaign_subscriptions.active.find_or_create_by(subscriber: subscriber, ended_at: nil, **args) end |
#subscribe!(subscriber, **args) ⇒ Object
Subscribes an object to a campaign. Raises ‘ActiveRecord::RecordInvalid` if the record was invalid. If a subscription hasn’t ended, any existing subscription will be returned.
85 86 87 |
# File 'app/models/caffeinate/campaign.rb', line 85 def subscribe!(subscriber, **args) caffeinate_campaign_subscriptions.find_or_create_by!(subscriber: subscriber, ended_at: nil, **args) end |
#subscriber(record, **args) ⇒ Object
Checks to see if the subscriber exists.
41 42 43 |
# File 'app/models/caffeinate/campaign.rb', line 41 def subscriber(record, **args) caffeinate_campaign_subscriptions.find_by(subscriber: record, **args) end |
#subscribes?(record, **args) ⇒ Boolean
Check if the subscriber exists
46 47 48 |
# File 'app/models/caffeinate/campaign.rb', line 46 def subscribes?(record, **args) subscriber(record, **args).present? end |
#to_dripper ⇒ Object
Poorly-named Campaign class resolver
27 28 29 |
# File 'app/models/caffeinate/campaign.rb', line 27 def to_dripper ::Caffeinate.dripper_collection.resolve(self) end |
#unsubscribe(subscriber, **args) ⇒ Object
Unsubscribes an object from a campaign.
Campaign[:onboarding].unsubscribe(Company.first, user: Company.first.admin, reason: "Because I said so")
is the same as
Campaign.find_by(slug: "onboarding").caffeinate_campaign_subscriptions.find_by(subscriber: Company.first, user: Company.first.admin).unsubscribe!("Because I said so")
Just… mintier.
59 60 61 62 63 64 65 |
# File 'app/models/caffeinate/campaign.rb', line 59 def unsubscribe(subscriber, **args) reason = args.delete(:reason) subscription = subscriber(subscriber, **args) return false if subscription.nil? subscription.unsubscribe(reason) end |
#unsubscribe!(subscriber, **args) ⇒ Object
67 68 69 70 71 72 73 |
# File 'app/models/caffeinate/campaign.rb', line 67 def unsubscribe!(subscriber, **args) reason = args.delete(:reason) subscription = subscriber(subscriber, **args) raise NoSubscription, subscription if subscription.nil? subscription.unsubscribe!(reason) end |