Add CustomFilterKeyword#to_regex
method (#28893)
This commit is contained in:
parent
a69506a434
commit
6b6586f5d0
3 changed files with 52 additions and 10 deletions
|
@ -68,16 +68,7 @@ class CustomFilter < ApplicationRecord
|
|||
|
||||
scope = CustomFilterKeyword.includes(:custom_filter).where(custom_filter: { account_id: account_id }).where(Arel.sql('expires_at IS NULL OR expires_at > NOW()'))
|
||||
scope.to_a.group_by(&:custom_filter).each do |filter, keywords|
|
||||
keywords.map! do |keyword|
|
||||
if keyword.whole_word
|
||||
sb = /\A[[:word:]]/.match?(keyword.keyword) ? '\b' : ''
|
||||
eb = /[[:word:]]\z/.match?(keyword.keyword) ? '\b' : ''
|
||||
|
||||
/(?mix:#{sb}#{Regexp.escape(keyword.keyword)}#{eb})/
|
||||
else
|
||||
/#{Regexp.escape(keyword.keyword)}/i
|
||||
end
|
||||
end
|
||||
keywords.map!(&:to_regex)
|
||||
|
||||
filters_hash[filter.id] = { keywords: Regexp.union(keywords), filter: filter }
|
||||
end.to_h
|
||||
|
|
|
@ -23,8 +23,24 @@ class CustomFilterKeyword < ApplicationRecord
|
|||
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})/
|
||||
else
|
||||
/#{Regexp.escape(keyword)}/i
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def to_regex_sb
|
||||
/\A[[:word:]]/.match?(keyword) ? '\b' : ''
|
||||
end
|
||||
|
||||
def to_regex_eb
|
||||
/[[:word:]]\z/.match?(keyword) ? '\b' : ''
|
||||
end
|
||||
|
||||
def prepare_cache_invalidation!
|
||||
custom_filter.prepare_cache_invalidation!
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue