diff --git a/app/models/concerns/custom_filter_cache.rb b/app/models/concerns/custom_filter_cache.rb new file mode 100644 index 000000000..79b22f11f --- /dev/null +++ b/app/models/concerns/custom_filter_cache.rb @@ -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 diff --git a/app/models/custom_filter_keyword.rb b/app/models/custom_filter_keyword.rb index 979d0b822..112798b10 100644 --- a/app/models/custom_filter_keyword.rb +++ b/app/models/custom_filter_keyword.rb @@ -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 diff --git a/app/models/custom_filter_status.rb b/app/models/custom_filter_status.rb index 0a5650204..58b61cd79 100644 --- a/app/models/custom_filter_status.rb +++ b/app/models/custom_filter_status.rb @@ -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