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
all_terms = data.scan(/'([^']+)'\:\d+/).flatten
all_terms.map! do |t|
t.split(/[\)\(&']/)[0]
end.compact!
t.split(/[\)\(&']/).find(&:present?)
end.reject!(&:blank?)
query = ActiveRecord::Base.connection.quote(
all_terms

View File

@ -945,12 +945,18 @@ describe Search do
end
end
context '#ts_query' do
it 'can parse complex strings using ts_query helper' do
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")
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
context '#word_to_date' do