Files
fizzy/app/models/notification.rb
T
Jorge Manrubia 8aa99e34b4 Persist notification's creator
Instead of delegating. With a polymorphic relationship, relying on a certain
attribute implies doing things like aliasing "mentioner" to "creator" or similar.
2025-04-22 13:48:43 +02:00

30 lines
662 B
Ruby

class Notification < ApplicationRecord
belongs_to :user
belongs_to :creator, class_name: "User"
belongs_to :source, polymorphic: true
belongs_to :resource, 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
def self.read_all
update!(read_at: Time.current)
end
def read
update!(read_at: Time.current)
end
def read?
read_at.present?
end
private
def broadcast_unread
broadcast_prepend_later_to user, :notifications, target: "notifications"
end
end