notification.rb 678 B

12345678910111213141516171819202122232425
  1. class Notification < ApplicationRecord
  2. belongs_to :recipient, class_name: "User"
  3. belongs_to :actor, class_name: "User"
  4. belongs_to :notifiable, polymorphic: true
  5. scope :unread, -> { where(read_at: nil) }
  6. scope :recent, -> { order(created_at: :desc).limit(5) }
  7. def self.post(to:, from:, action:, notifiable:)
  8. recipients = Array.wrap(to)
  9. notifications = []
  10. Notification.transaction do
  11. notifications = recipients.uniq.each do |recipient|
  12. Notification.create(
  13. notifiable: notifiable,
  14. action: action,
  15. recipient: recipient,
  16. actor: from
  17. )
  18. end
  19. end
  20. notifications
  21. end
  22. end