Moved send_mention_notification to ActiveJob (#3566)

This commit is contained in:
Anna Buianova 2019-08-01 23:35:25 +03:00 committed by Mac Siri
parent 1c2ff71de8
commit f9da88b006
6 changed files with 96 additions and 14 deletions

View file

@ -0,0 +1,10 @@
module Notifications
class MentionJob < ApplicationJob
queue_as :send_new_mention_notification
def perform(mention_id, service = NewMention::Send)
mention = Mention.find_by(id: mention_id)
service.call(mention) if mention
end
end
end

View file

@ -73,20 +73,12 @@ class Notification < ApplicationRecord
end
def send_mention_notification(mention)
mentioner = mention.mentionable.user
json_data = {
user: user_data(mentioner)
}
json_data[:comment] = comment_data(mention.mentionable) if mention.mentionable_type == "Comment"
Notification.create(
user_id: mention.user_id,
notifiable_id: mention.id,
notifiable_type: "Mention",
action: nil,
json_data: json_data,
)
Notifications::MentionJob.perform_later(mention.id)
end
def send_mention_notification_without_delay(mention)
Notifications::MentionJob.perform_now(mention.id)
end
handle_asynchronously :send_mention_notification
def send_welcome_notification(receiver_id)
Notifications::WelcomeNotificationJob.perform_later(receiver_id)

View file

@ -19,7 +19,7 @@ module Notifications
json_data = { user: user_data(User.dev_account) }
json_data[notifiable.class.name.downcase] = public_send "#{notifiable.class.name.downcase}_data", notifiable
new_notification = Notification.create(
new_notification = Notification.create!(
user_id: moderator.id,
notifiable_id: notifiable.id,
notifiable_type: notifiable.class.name,

View file

@ -0,0 +1,36 @@
module Notifications
module NewMention
class Send
delegate :user_data, to: Notifications
delegate :comment_data, to: Notifications
def initialize(mention)
@mention = mention
end
def self.call(*args)
new(*args).call
end
def call
Notification.create(
user_id: mention.user_id,
notifiable_id: mention.id,
notifiable_type: "Mention",
action: nil,
json_data: json_data,
)
end
private
attr_reader :mention
def json_data
data = { user: user_data(mention.user) }
data[:comment] = comment_data(mention.mentionable) if mention.mentionable_type == "Comment"
data
end
end
end
end

View file

@ -0,0 +1,24 @@
require "rails_helper"
RSpec.describe Notifications::MentionJob, type: :job do
include_examples "#enqueues_job", "send_new_mention_notification", 1
describe "#perform_now" do
let(:new_mention_service) { double }
let(:mention) { create(:mention, mentionable: create(:comment, commentable: create(:article))) }
before do
allow(new_mention_service).to receive(:call)
end
it "calls the service" do
described_class.perform_now(mention.id, new_mention_service)
expect(new_mention_service).to have_received(:call)
end
it "doesn't call the service when there's no mention" do
described_class.perform_now(mention.id + 1, new_mention_service)
expect(new_mention_service).not_to have_received(:call)
end
end
end

View file

@ -0,0 +1,20 @@
require "rails_helper"
RSpec.describe Notifications::NewMention::Send, type: :service do
let(:user) { create(:user) }
let(:comment) { create(:comment, commentable: create(:article)) }
let(:mention) { create(:mention, mentionable: comment, user: user) }
it "creates a mention notification" do
expect do
described_class.call(mention)
end.to change(Notification, :count).by(1)
end
it "creates a correct mention notification" do
notification = described_class.call(mention)
expect(notification.user_id).to eq(user.id)
expect(notification.notifiable).to eq(mention)
expect(notification.json_data["comment"]["path"]).to eq(comment.path)
end
end