lib/pg_search/features/tsearch.rb in pg_search-0.5.7 vs lib/pg_search/features/tsearch.rb in pg_search-0.6.0
- old
+ new
@@ -1,12 +1,10 @@
require "active_support/core_ext/module/delegation"
module PgSearch
module Features
class TSearch < Feature
- delegate :connection, :quoted_table_name, :to => :'@model'
-
def initialize(*args)
super
if options[:prefix] && model.connection.send(:postgresql_version) < 80400
raise PgSearch::NotSupportedForPostgresqlVersion.new(<<-MESSAGE.gsub /^\s*/, '')
@@ -14,23 +12,21 @@
MESSAGE
end
end
def conditions
- ["(#{tsdocument}) @@ (#{tsquery})", interpolations]
+ Arel::Nodes::Grouping.new(
+ Arel::Nodes::InfixOperation.new("@@", arel_wrap(tsdocument), arel_wrap(tsquery))
+ )
end
def rank
- tsearch_rank
+ arel_wrap(tsearch_rank)
end
private
- def interpolations
- {:query => query.to_s, :dictionary => dictionary.to_s}
- end
-
DISALLOWED_TSQUERY_CHARACTERS = /['?\\:]/
def tsquery_for_term(term)
sanitized_term = term.gsub(DISALLOWED_TSQUERY_CHARACTERS, " ")
@@ -43,11 +39,14 @@
term_sql,
connection.quote(" '"),
(connection.quote(':*') if options[:prefix])
].compact.join(" || ")
- "to_tsquery(:dictionary, #{tsquery_sql})"
+ Arel::Nodes::NamedFunction.new(
+ "to_tsquery",
+ [dictionary, Arel.sql(tsquery_sql)]
+ ).to_sql
end
def tsquery
return "''" if query.blank?
query_terms = query.split(" ").compact
@@ -59,11 +58,15 @@
if options[:tsvector_column]
column_name = connection.quote_column_name(options[:tsvector_column])
"#{quoted_table_name}.#{column_name}"
else
columns.map do |search_column|
- tsvector = "to_tsvector(:dictionary, #{normalize(search_column.to_sql)})"
+ tsvector = Arel::Nodes::NamedFunction.new(
+ "to_tsvector",
+ [dictionary, Arel.sql(normalize(search_column.to_sql))]
+ ).to_sql
+
if search_column.weight.nil?
tsvector
else
"setweight(#{tsvector}, #{connection.quote(search_column.weight)})"
end
@@ -83,14 +86,18 @@
def normalization
options[:normalization] || 0
end
def tsearch_rank
- ["ts_rank((#{tsdocument}), (#{tsquery}), #{normalization})", interpolations]
+ "ts_rank((#{tsdocument}), (#{tsquery}), #{normalization})"
end
def dictionary
options[:dictionary] || :simple
+ end
+
+ def arel_wrap(sql_string)
+ Arel::Nodes::Grouping.new(Arel.sql(sql_string))
end
end
end
end