module HasNotifications::HasNotifications

Public Instance Methods

all_notifications() click to toggle source
# File lib/has_notifications/has_notifications.rb, line 28
def all_notifications
        #
        # Get all notifications
        #
        self.notifications
end
has_notifications() click to toggle source
# File lib/has_notifications/has_notifications.rb, line 6
def has_notifications
        # storage for callback
        self.class_variable_set :@@notification_callbacks, Array.new

        # associations
        has_many :notifications, class_name: "::HasNotifications::HnNotification", as: :destination, dependent: :destroy

        # create instance methods
        class_eval do
                def notify(notification)
                        #
                        # Notify destination
                        #
                        hn_notification = ::HasNotifications::HnNotification.new
                        hn_notification.destination = self
                        hn_notification.notification = notification
                        hn_notification.save

                        # callbacks....
                        self.run_notification_callbacks(hn_notification)
                end

                def all_notifications
                        #
                        # Get all notifications
                        #
                        self.notifications
                end

                def unwatched_notifications
                        #
                        # Get all unwatched notifications
                        #
                        self.notifications.where(watched: false)
                end

                def watched_notifications
                        #
                        # Get all watched notifications
                        #
                        self.notifications.where(watched: true)
                end

                protected
                def run_notification_callbacks(notification)
                        # get callback list
                        callbacks = self.class.class_variable_get(:@@notification_callbacks)

                        callbacks.each do |callback|
                                self.send(callback, notification)
                        end
                end
        end
end
notify(notification) click to toggle source
# File lib/has_notifications/has_notifications.rb, line 15
def notify(notification)
        #
        # Notify destination
        #
        hn_notification = ::HasNotifications::HnNotification.new
        hn_notification.destination = self
        hn_notification.notification = notification
        hn_notification.save

        # callbacks....
        self.run_notification_callbacks(hn_notification)
end
on_notification(*methods) click to toggle source
# File lib/has_notifications/has_notifications.rb, line 61
def on_notification(*methods)
        #
        # Add callback
        #
        notification_callbacks = self.class_variable_get(:@@notification_callbacks)

        methods.each do |method|
                notification_callbacks << method
        end
end
run_notification_callbacks(notification) click to toggle source
# File lib/has_notifications/has_notifications.rb, line 50
def run_notification_callbacks(notification)
        # get callback list
        callbacks = self.class.class_variable_get(:@@notification_callbacks)

        callbacks.each do |callback|
                self.send(callback, notification)
        end
end
unwatched_notifications() click to toggle source
# File lib/has_notifications/has_notifications.rb, line 35
def unwatched_notifications
        #
        # Get all unwatched notifications
        #
        self.notifications.where(watched: false)
end
watched_notifications() click to toggle source
# File lib/has_notifications/has_notifications.rb, line 42
def watched_notifications
        #
        # Get all watched notifications
        #
        self.notifications.where(watched: true)
end