notification.rb 573 B

12345678910111213141516171819202122
  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. def self.post(to:, from:, action:, notifiable:)
  6. recipients = Array.wrap(to)
  7. notifications = []
  8. Notification.transaction do
  9. notifications = recipients.uniq.each do |recipient|
  10. Notification.create(
  11. notifiable: notifiable,
  12. action: action,
  13. recipient: recipient,
  14. actor: from
  15. )
  16. end
  17. end
  18. notifications
  19. end
  20. end