Skip to content

Commit

Permalink
Refactor TSearch#tsdocument
Browse files Browse the repository at this point in the history
  • Loading branch information
amarshall committed Jun 6, 2014
1 parent 03a3d73 commit 96c827b
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions lib/pg_search/features/tsearch.rb
Expand Up @@ -54,25 +54,8 @@ def tsquery
end

def tsdocument
tsdocument_terms = []

columns_to_use = options[:tsvector_column] ?
columns.select { |c| c.is_a?(PgSearch::Configuration::ForeignColumn) } :
columns

if columns_to_use.present?
tsdocument_terms << columns_to_use.map do |search_column|
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
end.join(" || ")
tsdocument_terms = (columns_to_use || []).map do |search_column|
column_to_tsvector(search_column)
end

if options[:tsvector_column]
Expand Down Expand Up @@ -107,6 +90,27 @@ def dictionary
def arel_wrap(sql_string)
Arel::Nodes::Grouping.new(Arel.sql(sql_string))
end

def columns_to_use
if options[:tsvector_column]
columns.select { |c| c.is_a?(PgSearch::Configuration::ForeignColumn) }
else
columns
end
end

def column_to_tsvector(search_column)
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
end
end
end
end

4 comments on commit 96c827b

@amarshall
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nertzy This & 9a6f6c7 puts us at a 4.0 on Code Climate 😃. (Also taking recommendations for a better name for column_to_tsvector.)

Code Climate

@nertzy
Copy link
Collaborator

@nertzy nertzy commented on 96c827b Jun 10, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just #to_tsvector for now? It could also be extracted to a small service object.

@amarshall
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though that makes it seem like it’s converting the whole self to a tsvector-like thing, which it’s not.

@nertzy
Copy link
Collaborator

@nertzy nertzy commented on 96c827b Jun 10, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.