class PgSearch::Multisearch::Rebuilder

def columns

def columns
  Array(model.pg_search_multisearchable_options[:against])
end

def connection

def connection
  model.connection
end

def content_expressions

def content_expressions
  columns.map { |column|
    %Q{coalesce(:model_table.#{column}::text, '')}
  }.join(" || ' ' || ")
end

def current_time

def current_time
  connection.quote(connection.quoted_date(@time_source.call))
end

def documents_table

def documents_table
  PgSearch::Document.quoted_table_name
end

def initialize(model, time_source = Time.method(:now))

def initialize(model, time_source = Time.method(:now))
  unless model.respond_to?(:pg_search_multisearchable_options)
    raise ModelNotMultisearchable.new(model)
  end
  @model = model
  @time_source = time_source
end

def model_name

def model_name
  connection.quote(model.name)
end

def model_table

def model_table
  model.quoted_table_name
end

def rebuild

def rebuild
  if model.respond_to?(:rebuild_pg_search_documents)
    model.rebuild_pg_search_documents
  elsif model.pg_search_multisearchable_options.key?(:if) || model.pg_search_multisearchable_options.key?(:unless)
    model.find_each { |record| record.update_pg_search_document }
  else
    model.connection.execute(rebuild_sql)
  end
end

def rebuild_sql

def rebuild_sql
  replacements.inject(REBUILD_SQL_TEMPLATE) do |sql, key|
    sql.gsub ":#{key}", send(key)
  end
end

def replacements

def replacements
  %w[content_expressions model_name model_table documents_table current_time]
end