lib/pg_search/features/tsearch.rb in pg_search-0.6.0 vs lib/pg_search/features/tsearch.rb in pg_search-0.6.1

- old
+ new

@@ -5,11 +5,11 @@ class TSearch < Feature def initialize(*args) super if options[:prefix] && model.connection.send(:postgresql_version) < 80400 - raise PgSearch::NotSupportedForPostgresqlVersion.new(<<-MESSAGE.gsub /^\s*/, '') + raise PgSearch::NotSupportedForPostgresqlVersion.new(<<-MESSAGE.strip_heredoc) Sorry, {:using => {:tsearch => {:prefix => true}}} only works in PostgreSQL 8.4 and above.") MESSAGE end end @@ -28,23 +28,22 @@ DISALLOWED_TSQUERY_CHARACTERS = /['?\\:]/ def tsquery_for_term(term) sanitized_term = term.gsub(DISALLOWED_TSQUERY_CHARACTERS, " ") - term_sql = normalize(connection.quote(sanitized_term)) + term_sql = Arel.sql(normalize(connection.quote(sanitized_term))) # After this, the SQL expression evaluates to a string containing the term surrounded by single-quotes. # If :prefix is true, then the term will also have :* appended to the end. - tsquery_sql = [ - connection.quote("' "), - term_sql, - connection.quote(" '"), - (connection.quote(':*') if options[:prefix]) - ].compact.join(" || ") + terms = ["' ", term_sql, " '", (':*' if options[:prefix])].compact + tsquery_sql = terms.inject do |memo, term| + Arel::Nodes::InfixOperation.new("||", memo, term) + end + Arel::Nodes::NamedFunction.new( "to_tsquery", - [dictionary, Arel.sql(tsquery_sql)] + [dictionary, tsquery_sql] ).to_sql end def tsquery return "''" if query.blank?