DEV: Forces custom search filter matchers to be case insensitive (#28785)

This commit is contained in:
Sérgio Saquetim 2024-09-06 12:30:51 -03:00 committed by GitHub
parent 02f8396cfc
commit f8e3a90ab9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

View File

@ -903,8 +903,11 @@ class Search
found = false found = false
Search.advanced_filters.each do |matcher, block| Search.advanced_filters.each do |matcher, block|
case_insensitive_matcher =
Regexp.new(matcher.source, matcher.options | Regexp::IGNORECASE)
cleaned = word.gsub(/["']/, "") cleaned = word.gsub(/["']/, "")
if cleaned =~ matcher if cleaned =~ case_insensitive_matcher
(@filters ||= []) << [block, $1] (@filters ||= []) << [block, $1]
found = true found = true
end end

View File

@ -2832,6 +2832,18 @@ RSpec.describe Search do
Search.advanced_filters.delete(/^min_chars:(\d+)$/) Search.advanced_filters.delete(/^min_chars:(\d+)$/)
end end
it "forces custom filters matchers to be case insensitive" do
expect(Search.new("advanced").execute.posts).to eq([post1, post0])
Search.advanced_filter(/^MIN_CHARS:(\d+)$/) do |posts, match|
posts.where("(SELECT LENGTH(p2.raw) FROM posts p2 WHERE p2.id = posts.id) >= ?", match.to_i)
end
expect(Search.new("advanced Min_Chars:50").execute.posts).to eq([post0])
ensure
Search.advanced_filters.delete(/^MIN_CHARS:(\d+)$/)
end
it "allows to define custom order" do it "allows to define custom order" do
expect(Search.new("advanced").execute.posts).to eq([post1, post0]) expect(Search.new("advanced").execute.posts).to eq([post1, post0])