Class: MySQL::Search::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql/search/updater.rb

Overview

Updates search indices based on associated models.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_text_searchable:, associated_model:, association_path: []) ⇒ Updater

Returns a new instance of Updater.



12
13
14
15
16
17
# File 'lib/mysql/search/updater.rb', line 12

def initialize(full_text_searchable:, associated_model:, association_path: [])
  @full_text_searchable = full_text_searchable
  @associated_model = associated_model
  @joins_args = translate_to_joins_args(association_path)
  @source_class = "#{full_text_searchable.name}Source".constantize
end

Instance Attribute Details

#associated_modelObject (readonly)

Returns the value of attribute associated_model.



7
8
9
# File 'lib/mysql/search/updater.rb', line 7

def associated_model
  @associated_model
end

#full_text_searchableObject (readonly)

Returns the value of attribute full_text_searchable.



7
8
9
# File 'lib/mysql/search/updater.rb', line 7

def full_text_searchable
  @full_text_searchable
end

#joins_argsObject (readonly)

Returns the value of attribute joins_args.



7
8
9
# File 'lib/mysql/search/updater.rb', line 7

def joins_args
  @joins_args
end

#source_classObject (readonly)

Returns the value of attribute source_class.



7
8
9
# File 'lib/mysql/search/updater.rb', line 7

def source_class
  @source_class
end

Instance Method Details

#updateObject



19
20
21
22
23
24
25
26
27
# File 'lib/mysql/search/updater.rb', line 19

def update
  full_text_searchable.transaction do
    full_text_searchables.find_each do |model|
      search_index = model.search_index || model.build_search_index

      search_index.update(source_class.new(model).extract)
    end
  end
end