Module: Notifi::Subscriber

Defined in:
lib/notifi/subscriber.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/notifi/subscriber.rb', line 3

def self.included(base)
  base.has_many :subscriptions, class_name: Subscription.name, dependent: :destroy, inverse_of: :subscriber
  base.has_many :triggered_notifications, class_name: Notification.name, dependent: :destroy, inverse_of: :notifier
  base.has_many :notifications, class_name: Notification.name, dependent: :destroy, inverse_of: :subscriber

  base.extend ClassMethods
end

Instance Method Details

#subscribe_to(subscribable) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/notifi/subscriber.rb', line 11

def subscribe_to(subscribable)
  reject_non_subscribable! subscribable

  sub = self.subscriptions.find_or_initialize_by(subscriber: self, subscribable: subscribable)

  if sub.new_record?
    sub.save
    sub
  end
end

#unsubscribe_from(subscribable) ⇒ Object



22
23
24
25
26
# File 'lib/notifi/subscriber.rb', line 22

def unsubscribe_from(subscribable)
  reject_non_subscribable! subscribable

  self.subscriptions.destroy_all(subscribable: subscribable)
end