dawarich/app/models/notification.rb

30 lines
523 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).order(created_at: :desc) }
2024-09-08 10:52:35 -04:00
def read?
read_at.present?
end
private
def broadcast_notification
NotificationsChannel.broadcast_to(
user, {
title: title,
id: id,
kind: kind
}
)
end
2024-07-04 16:20:12 -04:00
end