class NotificationMessage < ApplicationRecord
  belongs_to :user
  belongs_to :associated, polymorphic: true

  after_create :send_message

  after_update :clear_message

  after_destroy :clear_message

  scope :ordered, -> { order(created_at: :desc) }

  private
    def send_message
      Notification::Job.perform_async(user_id)
    end

    def clear_message
      Notification::Job.perform_async(user_id, false)
    end
end
