module PgSearch::Multisearch

def rebuild(model)

def rebuild(model)
  model.transaction do
    PgSearch::Document.where(:searchable_type => model.name).delete_all
    model.connection.execute(rebuild_sql(model))
  end
end

def rebuild_sql(model)

def rebuild_sql(model)
  connection = model.connection
  columns = Array.wrap(
    model.pg_search_multisearchable_options[:against]
  )
  content_expressions = columns.map do |column|
    %Q{coalesce(:model_table.#{column}, '')}
  end.join(" || ' ' || ")
  REBUILD_SQL_TEMPLATE.gsub(
    ":content_expressions", content_expressions
  ).gsub(
    ":model_name", connection.quote(model.name)
  ).gsub(
    ":model_table", model.quoted_table_name
  ).gsub(
    ":documents_table", PgSearch::Document.quoted_table_name
  )
end