Files
fizzy/app/models/notification.rb
T
Jorge Manrubia f0851988a2 Revert tentatively
Kevin got a bunch of dups, need to look into this more calmly https://3.basecamp.com/2914079/buckets/37331921/chats/9301300227
2025-11-23 10:18:39 +01:00

53 lines
1.2 KiB
Ruby

class Notification < ApplicationRecord
include PushNotifiable
belongs_to :account, default: -> { user.account }
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
after_destroy_commit :broadcast_read
after_create :bundle
scope :preloaded, -> { preload(:creator, :account, source: [ :board, :creator ]) }
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 unread
update!(read_at: nil)
broadcast_unread
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
def bundle
user.bundle(self) if user.settings.bundling_emails?
end
end