Sha256: f66658487b6db8c3a64063a3e31df32646ea69f587d59934baaebae1ceb7451d

Contents?: true

Size: 984 Bytes

Versions: 3

Compression:

Stored size: 984 Bytes

Contents

require 'active_support'

module NotificationSettings
    module Subscriber

        extend ActiveSupport::Concern

        included do
            has_many :notification_subscriptions, as: :subscriber, class_name: 'NotificationSettings::Subscription', dependent: :destroy
            has_many :notification_subscribables, through: :notification_subscriptions, source: :subscribable

            include NotificationSettings::Subscriber::InstanceMethods
        end

        module InstanceMethods

            def subscribe subscribable, options = {}
                options[:subscribable] = subscribable
                self.notification_subscriptions.create options
            end

            def unsubscribe subscribable
                subscription = self.notification_subscriptions.find_by subscribable_id: subscribable.id, subscribable_type: subscribable.class.name
                subscription.destroy
            end

        end

    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
notification-settings-1.2.5 lib/notification_settings/subscriber.rb
notification-settings-1.2.4 lib/notification_settings/subscriber.rb
notification-settings-1.2.3 lib/notification_settings/subscriber.rb