forked from mirrors/catstodon
Extract shared callback behaviour to CustomFilterCache
concern (#29695)
This commit is contained in:
parent
285a87a77f
commit
c0fe8a9f13
3 changed files with 21 additions and 24 deletions
17
app/models/concerns/custom_filter_cache.rb
Normal file
17
app/models/concerns/custom_filter_cache.rb
Normal 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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue