Module: Caffeinate::ActiveRecord::Extension

Defined in:
lib/caffeinate/active_record/extension.rb

Overview

Includes the ActiveRecord association and relevant scopes for an ActiveRecord-backed model

Instance Method Summary collapse

Instance Method Details

#acts_as_caffeinate_subscriberObject Also known as: caffeinate_subscriber

Adds the associations for a subscriber



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/caffeinate/active_record/extension.rb', line 8

def acts_as_caffeinate_subscriber
  has_many :caffeinate_campaign_subscriptions, as: :subscriber, class_name: '::Caffeinate::CampaignSubscription', dependent: :destroy
  has_many :caffeinate_campaigns, through: :caffeinate_campaign_subscriptions, class_name: '::Caffeinate::Campaign'
  has_many :caffeinate_mailings, through: :caffeinate_campaign_subscriptions, class_name: '::Caffeinate::Mailing'

  scope :not_subscribed_to_campaign, lambda { |list|
    where.not(id: ::Caffeinate::CampaignSubscription
                      .select(:subscriber_id)
                      .joins(:caffeinate_campaign)
                      .where(subscriber_type: name, caffeinate_campaigns: { slug: list }))
  }

  scope :unsubscribed_from_campaign, lambda { |list|
    where(id: ::Caffeinate::CampaignSubscription
                  .unsubscribed
                  .select(:subscriber_id)
                  .joins(:caffeinate_campaign)
                  .where(subscriber_type: name, caffeinate_campaigns: { slug: list }))
  }
end

#acts_as_caffeinate_userObject Also known as: caffeinate_user

Adds the associations for a user



31
32
33
34
35
# File 'lib/caffeinate/active_record/extension.rb', line 31

def acts_as_caffeinate_user
  has_many :caffeinate_campaign_subscriptions_as_user, as: :user, class_name: '::Caffeinate::CampaignSubscription'
  has_many :caffeinate_campaigns_as_user, through: :caffeinate_campaign_subscriptions_as_user, class_name: '::Caffeinate::Campaign'
  has_many :caffeinate_mailings_as_user, through: :caffeinate_campaign_subscriptions_as_user, class_name: '::Caffeinate::Campaign'
end