Add `CustomFilterKeyword#to_regex` method (#28893)

th-new
Matt Jankowski 2024-01-25 08:00:34 -05:00 committed by GitHub
parent a69506a434
commit 6b6586f5d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 10 deletions

View File

@ -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 = 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| scope.to_a.group_by(&:custom_filter).each do |filter, keywords|
keywords.map! do |keyword| keywords.map!(&:to_regex)
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
filters_hash[filter.id] = { keywords: Regexp.union(keywords), filter: filter } filters_hash[filter.id] = { keywords: Regexp.union(keywords), filter: filter }
end.to_h end.to_h

View File

@ -23,8 +23,24 @@ class CustomFilterKeyword < ApplicationRecord
before_destroy :prepare_cache_invalidation! before_destroy :prepare_cache_invalidation!
after_commit :invalidate_cache! 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 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! def prepare_cache_invalidation!
custom_filter.prepare_cache_invalidation! custom_filter.prepare_cache_invalidation!
end end

View File

@ -0,0 +1,35 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe CustomFilterKeyword do
describe '#to_regex' do
context 'when whole_word is true' do
it 'builds a regex with boundaries and the keyword' do
keyword = described_class.new(whole_word: true, keyword: 'test')
expect(keyword.to_regex).to eq(/(?mix:\b#{Regexp.escape(keyword.keyword)}\b)/)
end
it 'builds a regex with starting boundary and the keyword when end with non-word' do
keyword = described_class.new(whole_word: true, keyword: 'test#')
expect(keyword.to_regex).to eq(/(?mix:\btest\#)/)
end
it 'builds a regex with end boundary and the keyword when start with non-word' do
keyword = described_class.new(whole_word: true, keyword: '#test')
expect(keyword.to_regex).to eq(/(?mix:\#test\b)/)
end
end
context 'when whole_word is false' do
it 'builds a regex with the keyword' do
keyword = described_class.new(whole_word: false, keyword: 'test')
expect(keyword.to_regex).to eq(/test/i)
end
end
end
end