FIX: a search term containing '& could lead to errors

This also makes sure that the search term in front or after special characters isn't ignored.
This commit is contained in:
Gerhard Schlager 2018-11-21 22:07:13 +01:00
parent 58c795ef30
commit c376670bd2
2 changed files with 13 additions and 7 deletions

View File

@ -832,8 +832,8 @@ class Search
ts_config = ActiveRecord::Base.connection.quote(ts_config) if ts_config ts_config = ActiveRecord::Base.connection.quote(ts_config) if ts_config
all_terms = data.scan(/'([^']+)'\:\d+/).flatten all_terms = data.scan(/'([^']+)'\:\d+/).flatten
all_terms.map! do |t| all_terms.map! do |t|
t.split(/[\)\(&']/)[0] t.split(/[\)\(&']/).find(&:present?)
end.compact! end.reject!(&:blank?)
query = ActiveRecord::Base.connection.quote( query = ActiveRecord::Base.connection.quote(
all_terms all_terms

View File

@ -945,12 +945,18 @@ describe Search do
end end
end end
it 'can parse complex strings using ts_query helper' do context '#ts_query' do
str = " grigio:babel deprecated? " it 'can parse complex strings using ts_query helper' do
str << "page page on Atmosphere](https://atmospherejs.com/grigio/babel)xxx: aaa.js:222 aaa'\"bbb" str = " grigio:babel deprecated? "
str << "page page on Atmosphere](https://atmospherejs.com/grigio/babel)xxx: aaa.js:222 aaa'\"bbb"
ts_query = Search.ts_query(term: str, ts_config: "simple") ts_query = Search.ts_query(term: str, ts_config: "simple")
DB.exec("SELECT to_tsvector('bbb') @@ " << ts_query) expect { DB.exec("SELECT to_tsvector('bbb') @@ " << ts_query) }.to_not raise_error
ts_query = Search.ts_query(term: "foo.bar/'&baz", ts_config: "simple")
expect { DB.exec("SELECT to_tsvector('bbb') @@ " << ts_query) }.to_not raise_error
expect(ts_query).to include("baz")
end
end end
context '#word_to_date' do context '#word_to_date' do