-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change Notifications::UpdateJob to Notifications::UpdateWorker and mo…
…ve to sidekiq (#5689) [deploy]
- Loading branch information
Showing
7 changed files
with
72 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Notifications | ||
class UpdateWorker | ||
include Sidekiq::Worker | ||
|
||
sidekiq_options queue: :medium_priority, retry: 10 | ||
|
||
def perform(notifiable_id, notifiable_class, action = nil) | ||
raise InvalidNotifiableForUpdate, notifiable_class unless %w[Article Comment].include?(notifiable_class) | ||
|
||
notifiable = notifiable_class.constantize.find_by(id: notifiable_id) | ||
|
||
return unless notifiable | ||
|
||
Notifications::Update.call(notifiable, action) | ||
end | ||
end | ||
|
||
class InvalidNotifiableForUpdate < StandardError; end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Notifications::UpdateWorker do | ||
describe "#perform_now" do | ||
let(:id) { rand(1000) } | ||
let(:worker) { subject } | ||
|
||
before do | ||
allow(Notifications::Update).to receive(:call) | ||
end | ||
|
||
describe "when wrong class is passed" do | ||
it "raises an exception" do | ||
allow(User).to receive(:find_by).with(id).and_return(double) | ||
expect do | ||
worker.perform(id, "User") | ||
end.to raise_error(Notifications::InvalidNotifiableForUpdate, "User") | ||
end | ||
end | ||
|
||
describe "when notifiable is not found" do | ||
it "does not call the service" do | ||
allow(Comment).to receive(:find_by).with(id: id).and_return(nil) | ||
worker.perform(id, "Comment", nil) | ||
expect(Notifications::Update).not_to have_received(:call) | ||
end | ||
end | ||
|
||
describe "when notifiable is found" do | ||
it "calls the service" do | ||
article = double | ||
allow(Article).to receive(:find_by).with(id: id).and_return(article) | ||
worker.perform(id, "Article", "Published") | ||
expect(Notifications::Update).to have_received(:call).with(article, "Published") | ||
end | ||
end | ||
end | ||
end |