Class: Esse::ActiveRecord::Callbacks::IndexingOnUpdate

Inherits:
Esse::ActiveRecord::Callback show all
Defined in:
lib/esse/active_record/callbacks/indexing_on_update.rb

Instance Attribute Summary

Attributes inherited from Esse::ActiveRecord::Callback

#block_result, #options, #repo

Instance Method Summary collapse

Methods inherited from Esse::ActiveRecord::Callback

#initialize

Constructor Details

This class inherits a constructor from Esse::ActiveRecord::Callback

Instance Method Details

#call(model) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/esse/active_record/callbacks/indexing_on_update.rb', line 6

def call(model)
  record = block_result || model

  document = repo.serialize(record)
  return true unless document

  update_document(document)
  return true unless document.routing

  prev_record = model.class.new
  model.attributes.merge(model.previous_changes.transform_values(&:first)).each do |key, value|
    prev_record[key] = value
  end
  prev_document = repo.serialize(prev_record.tap(&:readonly!))

  return true unless prev_document
  return true if [prev_document.id, prev_document.routing].include?(nil)
  return true if prev_document.routing == document.routing
  return true if prev_document.id != document.id

  begin
    repo.index.delete(prev_document, **options)
  rescue Esse::Transport::NotFoundError
  end

  true
end