Extract shared callback behaviour to `CustomFilterCache` concern (#29695)

pull/29863/head
Matt Jankowski 2024-04-05 05:17:58 -04:00 committed by GitHub
parent 285a87a77f
commit c0fe8a9f13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 24 deletions

View File

@ -0,0 +1,17 @@
# frozen_string_literal: true
module CustomFilterCache
extend ActiveSupport::Concern
included do
after_commit :invalidate_cache!
before_destroy :prepare_cache_invalidation!
before_save :prepare_cache_invalidation!
delegate(
:invalidate_cache!,
:prepare_cache_invalidation!,
to: :custom_filter
)
end
end

View File

@ -13,16 +13,14 @@
#
class CustomFilterKeyword < ApplicationRecord
include CustomFilterCache
belongs_to :custom_filter
validates :keyword, presence: true
alias_attribute :phrase, :keyword
before_save :prepare_cache_invalidation!
before_destroy :prepare_cache_invalidation!
after_commit :invalidate_cache!
def to_regex
if whole_word?
/(?mix:#{to_regex_sb}#{Regexp.escape(keyword)}#{to_regex_eb})/
@ -40,12 +38,4 @@ class CustomFilterKeyword < ApplicationRecord
def to_regex_eb
/[[:word:]]\z/.match?(keyword) ? '\b' : ''
end
def prepare_cache_invalidation!
custom_filter.prepare_cache_invalidation!
end
def invalidate_cache!
custom_filter.invalidate_cache!
end
end

View File

@ -12,27 +12,17 @@
#
class CustomFilterStatus < ApplicationRecord
include CustomFilterCache
belongs_to :custom_filter
belongs_to :status
validates :status, uniqueness: { scope: :custom_filter }
validate :validate_status_access
before_save :prepare_cache_invalidation!
before_destroy :prepare_cache_invalidation!
after_commit :invalidate_cache!
private
def validate_status_access
errors.add(:status_id, :invalid) unless StatusPolicy.new(custom_filter.account, status).show?
end
def prepare_cache_invalidation!
custom_filter.prepare_cache_invalidation!
end
def invalidate_cache!
custom_filter.invalidate_cache!
end
end