diff --git a/app/models/concerns/account_cache_revalidator.rb b/app/models/concerns/account_cache_revalidator.rb index 53ffda28a..21dcf0d63 100644 --- a/app/models/concerns/account_cache_revalidator.rb +++ b/app/models/concerns/account_cache_revalidator.rb @@ -2,9 +2,7 @@ module AccountCacheRevalidator extend ActiveSupport::Concern included do - after_save :update_account_cache - after_destroy :update_account_cache - after_create :update_account_cache + after_commit :update_account_cache end def update_account_cache diff --git a/app/models/concerns/channelable.rb b/app/models/concerns/channelable.rb index e54d5f3a6..d88aae90a 100644 --- a/app/models/concerns/channelable.rb +++ b/app/models/concerns/channelable.rb @@ -3,7 +3,7 @@ module Channelable included do validates :account_id, presence: true belongs_to :account - has_one :inbox, as: :channel, dependent: :destroy_async + has_one :inbox, as: :channel, dependent: :destroy_async, touch: true end def messaging_window_enabled? diff --git a/spec/models/inbox_spec.rb b/spec/models/inbox_spec.rb index ced8eef2a..bc1abd501 100644 --- a/spec/models/inbox_spec.rb +++ b/spec/models/inbox_spec.rb @@ -194,4 +194,25 @@ RSpec.describe Inbox do end end end + + describe '#update' do + let!(:inbox) { FactoryBot.create(:inbox) } + + before do + allow(Rails.configuration.dispatcher).to receive(:dispatch) + end + + it 'resets cache key if there is an update in the channel' do + channel = inbox.channel + channel.update(widget_color: '#fff') + + expect(Rails.configuration.dispatcher).to have_received(:dispatch) + .with( + 'account.cache_invalidated', + kind_of(Time), + account: inbox.account, + cache_keys: inbox.account.cache_keys + ) + end + end end