Files
fizzy/app/models/notification.rb
T
Mike Dalessio 53ebb509d8 Broadcast when all notifications are cleared
Then make the turbo response to clearing all notifications a no-op.

This has the bonus side effect of properly clearing the notifications
try on all open pages.

ref: https://fizzy.37signals.com/5986089/collections/2/cards/796
2025-07-03 12:05:41 -04:00

37 lines
807 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
delegate :card, to: :source
def self.read_all
all.each { |notification| notification.read }
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