mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-03 19:24:48 +08:00
Merge pull request #8270 from artoscinote/ma_SCI_11597
Fix search for special characters and add specs [SCI-11597]
This commit is contained in:
commit
fd88a5c2d3
2 changed files with 69 additions and 3 deletions
|
@ -206,11 +206,10 @@ module SearchableModel
|
|||
new_phrase.map! { |t| "#{t}:*" } unless exact_match
|
||||
new_phrase = sanitize_sql_like(new_phrase.join('&').tr('\'', '"'))
|
||||
else
|
||||
new_phrase = sanitize_sql_like(Regexp.escape(new_phrase))
|
||||
new_phrase = exact_match ? "(^|\\s)#{new_phrase}(\\s|$)" : "%#{new_phrase}%"
|
||||
new_phrase = exact_match ? "(^|\\s)#{Regexp.escape(new_phrase)}(\\s|$)" : "%#{sanitize_sql_like(new_phrase)}%"
|
||||
end
|
||||
|
||||
["t#{i}".to_sym, new_phrase]
|
||||
[:"t#{i}", new_phrase]
|
||||
end).to_h
|
||||
)
|
||||
end
|
||||
|
|
67
spec/models/concerns/searchable_model_spec.rb
Normal file
67
spec/models/concerns/searchable_model_spec.rb
Normal file
|
@ -0,0 +1,67 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
SEARCH_SPECIAL_CHARACTER_TEST_NAMES = %w(
|
||||
NameWithNoSpecialCharacters
|
||||
NameWith_Underscore
|
||||
NameWith@At
|
||||
NameWith.Dot
|
||||
NameWith-Minus
|
||||
NameWith+Plus
|
||||
NameWith&Ampersand
|
||||
NameWith;Semicolon
|
||||
NameWith:Colon
|
||||
NameWith,Comma
|
||||
NameWith$Dollar
|
||||
NameWith/Slash
|
||||
NameWith=Equals
|
||||
NameWith~Tilde
|
||||
NameWith^Caret
|
||||
NameWith°Degree
|
||||
NameWith`Backtick
|
||||
NameWith"DoubleQuote
|
||||
NameWith'SingleQuote
|
||||
NameWith*Asterisk
|
||||
NameWith?QuestionMark
|
||||
NameWith%Percent
|
||||
NameWith\Backslash
|
||||
NameWith[SquareBracket]
|
||||
NameWith{CurlyBracket}
|
||||
NameWith(Parenthesis)
|
||||
NameWith|Pipe
|
||||
NameWith!Exclamation
|
||||
).freeze
|
||||
|
||||
describe SearchableModel, type: :concern do
|
||||
let!(:user) { create :user }
|
||||
let!(:team) { create :team, created_by: user }
|
||||
|
||||
let!(:projects) do
|
||||
SEARCH_SPECIAL_CHARACTER_TEST_NAMES.map do |name|
|
||||
Project.create!(name: name, team: user.teams.first, created_by: user)
|
||||
end
|
||||
end
|
||||
|
||||
it '#where_attributes_like_boolean finds all projects by name' do
|
||||
SEARCH_SPECIAL_CHARACTER_TEST_NAMES.each do |name|
|
||||
expect(Project.where_attributes_like_boolean(:name, "#{name} OR something")&.first&.name).to eq name
|
||||
puts "Found #{name}"
|
||||
end
|
||||
end
|
||||
|
||||
it '#where_attributes_like_boolean finds all projects by exact name' do
|
||||
SEARCH_SPECIAL_CHARACTER_TEST_NAMES.filter { |n| n != 'NameWith"DoubleQuote' }.each do |name|
|
||||
expect(Project.where_attributes_like_boolean(:name, "\"#{name}\"")&.first&.name).to eq name
|
||||
expect(Project.where_attributes_like_boolean(:name, "\"#{name} not exact\"").count).to eq 0
|
||||
puts "Found #{name}"
|
||||
end
|
||||
end
|
||||
|
||||
it '#where_attributes_like finds all projects by name ' do
|
||||
SEARCH_SPECIAL_CHARACTER_TEST_NAMES.each do |name|
|
||||
expect(Project.where_attributes_like(:name, name)&.first&.name).to eq name
|
||||
puts "Found #{name}"
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue