2eae58725a
Is confusing since "source" already captures the origin of the notifications. It's needed to interpret things at rendering time, so we can query things as needed there.
29 lines
620 B
Ruby
29 lines
620 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
|
|
|
|
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
|