module Tantot::Extensions::Chewy

Public Class Methods

register_watch(model, type_name, options, block) click to toggle source
# File lib/tantot/extensions/chewy.rb, line 39
def self.register_watch(model, type_name, options, block)
  method = options.delete(:method)
  model._tantot_chewy_callbacks ||= {}
  model._tantot_chewy_callbacks[type_name] ||= []
  model._tantot_chewy_callbacks[type_name] << [method, options, block]
end

Public Instance Methods

watch_index(type_name, *args, &block) click to toggle source

watch_index 'index#type', attribute, attribute, {method: [:self | :method | ignore and pass a block | ignore and don't pass a block, equivalent of :self]} [block]

# File lib/tantot/extensions/chewy.rb, line 12
def watch_index(type_name, *args, &block)
  options = args.extract_options!
  watch_options = {}
  watch_options[:only] = options[:only] if options[:only]

  if options[:association]
    reflection = self.reflect_on_association(options[:association])
    raise ArgumentError.new("Association #{options[:association]} not found on #{self.class.name}") unless reflection
    case reflection.macro
    when :belongs_to
      watch_options[:always] = reflection.foreign_key
    when :has_one, :has_many
      if reflection.options[:through]
        if reflection.through_reflection.belongs_to?
          watch_options[:always] = reflection.through_reflection.foreign_key
        end
      end
    else
      raise NotImplementedError.new("Association of type #{reflection.macro} not yet supported")
    end
  end

  Tantot::Extensions::Chewy.register_watch(self, type_name, options, block)
  watch('tantot/extensions/chewy/chewy', *args, watch_options)
end