module Notifi::Subscriber

Public Class Methods

included(base) click to toggle source
# 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

Public Instance Methods

subscribe_to(subscribable) click to toggle source
# 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) click to toggle source
# File lib/notifi/subscriber.rb, line 22
def unsubscribe_from(subscribable)
  reject_non_subscribable! subscribable

  self.subscriptions.destroy_all(subscribable: subscribable)
end

Private Instance Methods

reject_non_subscribable!(target) click to toggle source
# File lib/notifi/subscriber.rb, line 35
def reject_non_subscribable!(target)
  unless target.kind_of? Subscribable
    raise ArgumentError, "#{target.class} does not include Notifi::Subscribable"
  end
end