Class: TireAsyncIndex::Workers::UpdateIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/tire_async_index/workers/update_index.rb

Overview

Worker for updating ElasticSearch index

Direct Known Subclasses

Resque, Sidekiq

Instance Method Summary collapse

Instance Method Details

#find_class_const(class_name) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/tire_async_index/workers/update_index.rb', line 33

def find_class_const(class_name)
  if defined?(RUBY_VERSION) && RUBY_VERSION.match(/^2\./)
    Kernel.const_get(class_name)
  else
    class_name.split('::').reduce(Object) do |mod, const_name|
      mod.const_get(const_name)
    end
  end
end

#get_finder_method(klass) ⇒ Object



43
44
45
# File 'lib/tire_async_index/workers/update_index.rb', line 43

def get_finder_method(klass)
  klass.respond_to?(:tire_async_finder) ? :tire_async_finder : :find
end

#process(action_type, class_name, id) ⇒ Object

Update or delete ElasticSearch index based on the action_type parameter.

Parameters:

action_type - Determine index direction. (allowed value - Update or Delete)
class_name - Model class name
id - Document id


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tire_async_index/workers/update_index.rb', line 15

def process(action_type, class_name, id)
  klass = find_class_const(class_name)

  case action_type.to_sym
    when :update
      object = klass.send(get_finder_method(klass), id)

      if object.present? && object.respond_to?(:tire)
        object.tire.update_index
      end
    when :delete

      klass.new.tap do |inst|
        inst.tire.index.remove(inst.tire.document_type, { _id: id })
      end
  end
end