Sha256: f59d9b5f9f58a6745e2d0219d1a35c830583c81a0d34d7809a9069abf181a331

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

module NotificationHandler
    module NotificationLibrary

        attr_accessor :group

        before_validation :create_for_group
        after_commit :cache

        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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
notification-handler-1.0.0.beta8 lib/notification_handler/notification_library.rb
notification-handler-1.0.0.beta7 lib/notification_handler/notification_library.rb