dawarich/app/models/notification.rb

33 lines
578 B
Ruby
Raw Normal View History

2024-07-04 16:20:12 -04:00
# frozen_string_literal: true
class Notification < ApplicationRecord
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
scope :unread, -> { where(read_at: nil) }
2024-09-08 10:52:35 -04:00
def read?
read_at.present?
end
private
def broadcast_notification
NotificationsChannel.broadcast_to(
user,
{
# id: id,
# title: title,
# content: content,
# kind: kind,
# timestamp: Time.current.to_i
}
)
end
2024-07-04 16:20:12 -04:00
end