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
This commit is contained in:
Alain Mauri 2020-01-20 13:24:42 +00:00 committed by Molly Struve
parent 18b2b2cb4c
commit 1217f1f08d
3 changed files with 45 additions and 2 deletions

View file

@ -9,7 +9,7 @@ class Mention < ApplicationRecord
validates :mentionable_type, presence: true
validate :permission
after_create :send_email_notification
after_create_commit :send_email_notification
def self.create_all(notifiable)
Mentions::CreateAllWorker.perform_async(notifiable.id, notifiable.class.name)
@ -21,7 +21,7 @@ class Mention < ApplicationRecord
user = User.find(user_id)
return unless user.email.present? && user.email_mention_notifications
Mentions::SendEmailNotificationJob.perform_later(id)
Mentions::SendEmailNotificationWorker.perform_async(id)
end
def permission

View file

@ -0,0 +1,11 @@
module Mentions
class SendEmailNotificationWorker
include Sidekiq::Worker
sidekiq_options queue: :default, retry: 10
def perform(mention_id)
mention = Mention.find_by(id: mention_id)
NotifyMailer.new_mention_email(mention) if mention
end
end
end

View file

@ -0,0 +1,32 @@
require "rails_helper"
RSpec.describe Mentions::SendEmailNotificationWorker, type: :worker do
include_examples "#enqueues_on_correct_queue", "default", 1
describe "#perform" do
let(:worker) { subject }
let(:user) { create(:user) }
let(:mention) { create(:mention, user_id: user.id, mentionable_id: comment.id, mentionable_type: "Comment") }
let(:comment) { create(:comment, user_id: user.id, commentable: create(:article)) }
context "with mention" do
it "calls on NotifyMailer" do
worker.perform(mention.id) do
expect(NotifyMailer).to have_received(:new_mention_email).with(mention)
end
end
end
context "without a mention" do
it "does not error" do
expect { worker.perform(nil) }.not_to raise_error
end
it "does not call NotifyMailer" do
worker.perform(nil) do
expect(NotifyMailer).not_to have_received(:new_mention_email)
end
end
end
end
end