2024-07-04 16:20:12 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class Notification < ApplicationRecord
|
2024-11-03 08:37:01 -05:00
|
|
|
after_create_commit :broadcast_notification
|
|
|
|
|
|
2024-07-04 16:20:12 -04:00
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
|
|
validates :title, :content, :kind, presence: true
|
|
|
|
|
|
2024-09-05 15:01:59 -04:00
|
|
|
enum :kind, { info: 0, warning: 1, error: 2 }
|
2024-07-04 16:20:12 -04:00
|
|
|
|
2024-11-07 13:00:11 -05:00
|
|
|
scope :unread, -> { where(read_at: nil).order(created_at: :desc) }
|
2024-09-08 10:52:35 -04:00
|
|
|
|
|
|
|
|
def read?
|
|
|
|
|
read_at.present?
|
|
|
|
|
end
|
2024-11-03 08:37:01 -05:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def broadcast_notification
|
|
|
|
|
NotificationsChannel.broadcast_to(
|
2024-11-07 13:00:11 -05:00
|
|
|
user, {
|
|
|
|
|
title: title,
|
|
|
|
|
id: id,
|
|
|
|
|
kind: kind
|
2024-11-03 08:37:01 -05:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
end
|
2024-07-04 16:20:12 -04:00
|
|
|
end
|