Class: PgSearch::Multisearch::Rebuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_search/multisearch/rebuilder.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, time_source = Time.method(:now)) ⇒ Rebuilder

Arguments

model

Active Record model configured for multisearch.

time_source

Clock returning a timestamp for bulk inserts. Defaults to Time.method(:now) and is called once per bulk rebuild so created_at and updated_at share a timestamp.

Raises

ModelNotMultisearchable

If model is not configured for multisearch.



16
17
18
19
20
21
# File 'lib/pg_search/multisearch/rebuilder.rb', line 16

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

  @model = model
  @time_source = time_source
end

Instance Method Details

#rebuildObject

Populates search documents without first clearing existing documents. Uses the model's rebuild_pg_search_documents override when available; otherwise updates records individually for conditions, dynamic content, or additional attributes, and bulk-inserts for plain column content.



27
28
29
30
31
32
33
34
35
# File 'lib/pg_search/multisearch/rebuilder.rb', line 27

def rebuild
  if model.respond_to?(:rebuild_pg_search_documents)
    model.rebuild_pg_search_documents
  elsif conditional? || dynamic? || additional_attributes?
    model.find_each(&:update_pg_search_document)
  else
    model.connection.execute(rebuild_sql)
  end
end