Sha256: fdb860657165d02e017fe931602dcaf650c70f64a68085cf7eded6aeeb470771

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

require 'active_support'

module NotificationHandler
    module NotificationLibrary

        extend ActiveSupport::Concern

        included do
            self.inheritance_column = :_type_disabled
            
            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

3 entries across 3 versions & 1 rubygems

Version Path
notification-handler-1.2.5 lib/notification_handler/notification_library.rb
notification-handler-1.2.4 lib/notification_handler/notification_library.rb
notification-handler-1.2.3 lib/notification_handler/notification_library.rb