docbrown/app/models/mention.rb
Alain Mauri 1217f1f08d Add mentions send email notifications worker (#5549)
* Added email notification worker for mentions
  * Modified the email generation to after_create_commit
  * Modified Mention model to use the new worker
  * Added tests for the worker
2020-01-20 08:24:42 -05:00

30 lines
865 B
Ruby

class Mention < ApplicationRecord
belongs_to :user
belongs_to :mentionable, polymorphic: true
validates :user_id, presence: true,
uniqueness: { scope: %i[mentionable_id
mentionable_type] }
validates :mentionable_id, presence: true
validates :mentionable_type, presence: true
validate :permission
after_create_commit :send_email_notification
def self.create_all(notifiable)
Mentions::CreateAllWorker.perform_async(notifiable.id, notifiable.class.name)
end
private
def send_email_notification
user = User.find(user_id)
return unless user.email.present? && user.email_mention_notifications
Mentions::SendEmailNotificationWorker.perform_async(id)
end
def permission
errors.add(:mentionable_id, "is not valid.") unless mentionable&.valid?
end
end