Sha256: a8234b294782f646dd2f49419881b2396a67737a10fae6e74f87307241b6c1c9

Contents?: true

Size: 1.86 KB

Versions: 8

Compression:

Stored size: 1.86 KB

Contents

require 'active_support'

module NotificationHandler
    module NotificationLibrary

        extend ActiveSupport::Concern

        included do
            before_validation :create_for_group
            after_commit :cache

            serialize :metadata, Hash
            attr_accessor :group

            belongs_to :target, polymorphic: true
            belongs_to :object, polymorphic: true, optional: true

            include NotificationHandler::NotificationLibrary::InstanceMethods

            include NotificationRenderer::NotificationLibrary if defined?(NotificationRenderer)
            include NotificationPusher::NotificationLibrary if defined?(NotificationPusher)
            include NotificationSettings::NotificationLibrary if defined?(NotificationSettings)
        end

        module InstanceMethods

            def read?
                self.read
            end
            def unread?
                !self.read
            end

            private

            def create_for_group
                unless self.group.nil?
                    target_scope = NotificationHandler::Group.find_by_name(self.group).last.target_scope
                    target_scope&.each do |target|
                        notification = self.dup
                        notification.target = target
                        notification.group = nil
                        notification.save
                    end
                    return false
                end
            end

            def cache
                if self.read_changed?
                    self.target.read_notification_count = self.target.notifications.read.count
                    self.target.unread_notification_count = self.target.notifications.unread.count
                    self.target.save!
                end
            end

        end

    end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
notification-handler-1.2.2 lib/notification_handler/notification_library.rb
notification-handler-1.2.1 lib/notification_handler/notification_library.rb
notification-handler-1.2.0 lib/notification_handler/notification_library.rb
notification-handler-1.1.2 lib/notification_handler/notification_library.rb
notification-handler-1.1.1 lib/notification_handler/notification_library.rb
notification-handler-1.1.0 lib/notification_handler/notification_library.rb
notification-handler-1.0.0 lib/notification_handler/notification_library.rb
notification-handler-1.0.0.beta11 lib/notification_handler/notification_library.rb