Files
fizzy/app/models/notification.rb
T
Mike Dalessio 822ec5b5bb Broadcast notification readings
so that the notification tray will be more consistent across open
browser tabs.
2025-05-08 10:40:10 -04:00

36 lines
762 B
Ruby

class Notification < ApplicationRecord
belongs_to :user
belongs_to :creator, class_name: "User"
belongs_to :source, polymorphic: true
scope :unread, -> { where(read_at: nil) }
scope :read, -> { where.not(read_at: nil) }
scope :ordered, -> { order(read_at: :desc, created_at: :desc) }
after_create_commit :broadcast_unread
delegate :notifiable_target, to: :source
def self.read_all
update!(read_at: Time.current)
end
def read
update!(read_at: Time.current)
broadcast_read
end
def read?
read_at.present?
end
private
def broadcast_unread
broadcast_prepend_later_to user, :notifications, target: "notifications"
end
def broadcast_read
broadcast_remove_to user, :notifications
end
end