Move remove_each notifications method in own job (#2946)

This commit is contained in:
cyrillefr 2019-05-29 22:26:18 +02:00 committed by Mac Siri
parent a99c072525
commit 8c52d5c43f
6 changed files with 105 additions and 10 deletions

View file

@ -0,0 +1,11 @@
module Notifications
class RemoveEachJob < ApplicationJob
queue_as :remove_each_notifications
def perform(notifiable_collection_ids, service = Notifications::RemoveEach)
return unless notifiable_collection_ids.any?
service.call(notifiable_collection_ids)
end
end
end

View file

@ -128,17 +128,13 @@ class Notification < ApplicationRecord
Notifications::RemoveAllJob.perform_now(notifiable_id, notifiable_type, action)
end
def remove_each(notifiable_collection, action = nil)
# only used for mentions since it's an array
notifiable_collection.each do |notifiable|
Notification.where(
notifiable_id: notifiable.id,
notifiable_type: notifiable.class.name,
action: action,
).destroy_all
end
def remove_each(notifiable_collection)
Notifications::RemoveEachJob.perform_later(notifiable_collection.pluck(:id))
end
def remove_each_without_delay(notifiable_collection)
Notifications::RemoveEachJob.perform_now(notifiable_collection.pluck(:id))
end
handle_asynchronously :remove_each
def update_notifications(notifiable, action = nil)
Notifications::UpdateJob.perform_later(notifiable.id, notifiable.class.name, action)

View file

@ -0,0 +1,24 @@
module Notifications
class RemoveEach
# notifiable_collection_ids: an array of
# notifiable objects ids (eg mention)
def initialize(notifiable_collection_ids)
@notifiable_collection_ids = notifiable_collection_ids
end
def self.call(*args)
new(*args).call
end
def call
Notification.where(
notifiable_id: notifiable_collection_ids,
notifiable_type: "Mention",
).destroy_all
end
private
attr_reader :notifiable_collection_ids
end
end

View file

@ -0,0 +1,25 @@
require "rails_helper"
RSpec.describe Notifications::RemoveEachJob, type: :job do
include_examples "#enqueues_job", "remove_each_notifications", []
describe "#perform_now" do
let(:remove_each_service) { double }
before do
allow(remove_each_service).to receive(:call)
end
context "when array is empty" do
it "does not call the service" do
described_class.perform_now([], remove_each_service)
expect(remove_each_service).not_to have_received(:call)
end
end
it "calls the service" do
described_class.perform_now([935, 936], remove_each_service)
expect(remove_each_service).to have_received(:call).with([935, 936])
end
end
end

View file

@ -379,4 +379,22 @@ RSpec.describe Notification, type: :model do
end.to change(Notification, :count).by(1)
end
end
describe "#remove_each" do
let(:mention) { create(:mention, user_id: user.id, mentionable_id: comment.id, mentionable_type: "Comment") }
let(:comment) { create(:comment, user_id: user.id, commentable_id: article.id) }
let(:notifiable_collection) { [mention] }
before do
create(:notification, user_id: mention.user_id, notifiable_id: mention.id, notifiable_type: "Mention")
end
it "removes each mention related notifiable" do
perform_enqueued_jobs do
expect do
Notification.remove_each(notifiable_collection)
end.to change(Notification, :count).by(-1)
end
end
end
end

View file

@ -0,0 +1,21 @@
require "rails_helper"
RSpec.describe Notifications::RemoveEach do
let(:user) { create(:user) }
let(:user2) { create(:user) }
let(:article) { create(:article, user_id: user.id) }
let(:comment) { create(:comment, user_id: user.id, commentable_id: article.id) }
let(:comment2) { create(:comment, user_id: user2.id, commentable_id: article.id) }
let(:mention) { create(:mention, user_id: user.id, mentionable_id: comment.id, mentionable_type: "Comment") }
let(:mention2) { create(:mention, user_id: user2.id, mentionable_id: comment2.id, mentionable_type: "Comment") }
let(:notifiable_collection_ids) { [mention.id, mention2.id] }
before do
create(:notification, user_id: mention.user.id, notifiable_id: mention.id, notifiable_type: "Mention")
create(:notification, user_id: mention2.user.id, notifiable_id: mention2.id, notifiable_type: "Mention")
end
it "checks all notifiables are deleted" do
expect { described_class.call(notifiable_collection_ids) }.to change(Notification, :count).by(-2)
end
end