diff --git a/app/jobs/chat_channels/index_job.rb b/app/jobs/chat_channels/index_job.rb new file mode 100644 index 000000000..6f4740bb5 --- /dev/null +++ b/app/jobs/chat_channels/index_job.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module ChatChannels + class IndexJob < ApplicationJob + queue_as :chat_channels_index + + def perform(chat_channel_id:) + chat_channel = ChatChannel.find_by(id: chat_channel_id) + return unless chat_channel + + chat_channel.index! + end + end +end diff --git a/app/models/message.rb b/app/models/message.rb index 7045a3135..8bfe22b59 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -37,7 +37,8 @@ class Message < ApplicationRecord chat_channel.touch(:last_message_at) chat_channel.index! chat_channel.chat_channel_memberships.reindex! - chat_channel.delay.index! + + ChatChannels::IndexJob.perform_later(chat_channel_id: chat_channel.id) end def update_all_has_unopened_messages_statuses diff --git a/spec/jobs/chat_channels/index_job_spec.rb b/spec/jobs/chat_channels/index_job_spec.rb new file mode 100644 index 000000000..786c73e83 --- /dev/null +++ b/spec/jobs/chat_channels/index_job_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe ChatChannels::IndexJob, type: :job do + include_examples "#enqueues_job", "chat_channels_index", chat_channel_id: 1 + + describe "#perform_now" do + let(:chat_channel) { double } + let(:chat_channel_id) { 1 } + + context "when chat_channel is found" do + before do + allow(ChatChannel).to receive(:find_by).with(id: chat_channel_id).and_return(chat_channel) + allow(chat_channel).to receive(:index!) + end + + it "calls index" do + described_class.perform_now(chat_channel_id: chat_channel_id) + expect(chat_channel).to have_received(:index!) + end + end + + context "when chat_channel is not found" do + before do + allow(ChatChannel).to receive(:find_by).with(id: chat_channel_id).and_return(nil) + end + + it "doesn't fail" do + described_class.perform_now(chat_channel_id: chat_channel_id) + end + end + end +end