FIX: remove invalid chars from ts_query helper

This commit is contained in:
Sam 2015-08-13 17:55:10 +10:00
parent eed040767f
commit ecac786f86
2 changed files with 12 additions and 1 deletions

View file

@ -462,7 +462,6 @@ class Search
def self.ts_query(term, locale = nil, joiner = "&")
data = Post.exec_sql("SELECT to_tsvector(:locale, :term)",
locale: locale || long_locale,
term: term
@ -470,6 +469,10 @@ class Search
locale = Post.sanitize(locale) if locale
all_terms = data.scan(/'([^']+)'\:\d+/).flatten
all_terms.map! do |t|
t.split(/[\)\(&']/)[0]
end.compact!
query = Post.sanitize(all_terms.map {|t| "#{PG::Connection.escape_string(t)}:*"}.join(" #{joiner} "))
"TO_TSQUERY(#{locale || query_locale}, #{query})"
end

View file

@ -443,5 +443,13 @@ describe Search do
end
end
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:"
ts_query = Search.ts_query(str, "simple")
Post.exec_sql("SELECT to_tsvector('bbb') @@ " << ts_query)
end
end