Class: SearchDo::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/search_do/indexer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, configuration) ⇒ Indexer

Returns a new instance of Indexer.



6
7
8
9
# File 'lib/search_do/indexer.rb', line 6

def initialize(base, configuration)
  @base = base
  @configuration = configuration
end

Instance Attribute Details

#attributes_to_storeObject

Returns the value of attribute attributes_to_store.



5
6
7
# File 'lib/search_do/indexer.rb', line 5

def attributes_to_store
  @attributes_to_store
end

#if_changedObject

Returns the value of attribute if_changed.



5
6
7
# File 'lib/search_do/indexer.rb', line 5

def if_changed
  @if_changed
end

#searchable_fieldsObject

Returns the value of attribute searchable_fields.



5
6
7
# File 'lib/search_do/indexer.rb', line 5

def searchable_fields
  @searchable_fields
end

Instance Method Details

#add_callbacks!Object



46
47
48
49
50
51
# File 'lib/search_do/indexer.rb', line 46

def add_callbacks!
  @base.after_update(:update_index)
  @base.after_create(:add_to_index)
  @base.after_destroy(:remove_from_index)
  @base.after_save(:clear_changed_attributes)
end

#observing_fields(update = false) ⇒ Object



26
27
28
29
30
# File 'lib/search_do/indexer.rb', line 26

def observing_fields(update = false)
  expire_observing_fields_cache! if update
  @observing_fields ||=
    Set.new((if_changed + searchable_fields + attributes_to_store.values).map(&:to_s))
end

#record_timestamps!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/search_do/indexer.rb', line 32

def record_timestamps!
  begin
    detect_col = lambda{|candidate_col| @base.column_names.include?(candidate_col) }
    @attributes_to_store[backend_vocabulary :create_timestamp] =
      %w(created_at created_on).detect(&detect_col)
    @attributes_to_store[backend_vocabulary :update_timestamp] =
      %w(updated_at updated_on).detect(&detect_col)
    expire_observing_fields_cache!
  rescue
    #allow db-operations like schema loading etc to work without this crashing
    puts "Your database is non-existent or in a very bad state -- From:#{__FILE__}:#{__LINE__}"
  end
end