Class: Moist::SubscriptionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/moist/subscription_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subscriber, user:, action:) ⇒ SubscriptionManager

Returns a new instance of SubscriptionManager.



4
5
6
7
8
# File 'lib/moist/subscription_manager.rb', line 4

def initialize(subscriber, user:, action:)
  @subscriber = subscriber
  @user = user
  @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



3
4
5
# File 'lib/moist/subscription_manager.rb', line 3

def action
  @action
end

#subscriberObject (readonly)

Returns the value of attribute subscriber.



3
4
5
# File 'lib/moist/subscription_manager.rb', line 3

def subscriber
  @subscriber
end

#userObject (readonly)

Returns the value of attribute user.



3
4
5
# File 'lib/moist/subscription_manager.rb', line 3

def user
  @user
end

Instance Method Details

#call(*campaign_slugs) ⇒ Object Also known as: to, from



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/moist/subscription_manager.rb', line 10

def call(*campaign_slugs)
  campaign_slugs.map do |campaign_slug|
    campaign = ::Moist::Campaign[campaign_slug]
    if @action == :subscribe
      ::Moist::CampaignSubscriber.first_or_create!(subscriber: subscriber, user: user, moist_campaign: campaign)
    elsif @action == :unsubscribe
      sub = ::Moist::CampaignSubscriber.find_by!(subscriber: subscriber, user: user, moist_campaign: campaign)
      sub.destroy!
      sub
    end
  end
end