Module: Chewy::Index::Observe::Helpers

Included in:
Chewy::Index::Observe
Defined in:
lib/chewy/index/observe/active_record_methods.rb

Instance Method Summary collapse

Instance Method Details

#extract_callback_options!(args) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/chewy/index/observe/active_record_methods.rb', line 36

def extract_callback_options!(args)
  options = args.extract_options!
  result = options.each_key.with_object({}) do |key, hash|
    hash[key] = options.delete(key) if %i[if unless].include?(key)
  end
  args.push(options) unless options.empty?
  result
end

#update_proc(index_name, *args, &block) ⇒ Object



5
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
33
34
# File 'lib/chewy/index/observe/active_record_methods.rb', line 5

def update_proc(index_name, *args, &block)
  options = args.extract_options!
  method = args.first

  proc do
    reference = if index_name.is_a?(Proc)
      if index_name.arity.zero?
        instance_exec(&index_name)
      else
        index_name.call(self)
      end
    else
      index_name
    end

    index = Chewy.derive_name(reference)

    next if Chewy.strategy.current.name == :bypass

    backreference = if method && method.to_s == 'self'
      self
    elsif method
      send(method)
    else
      instance_eval(&block)
    end

    index.update_index(backreference, options)
  end
end