Module: Searchlight::Adapters::Mongoid::Search

Defined in:
lib/searchlight/adapters/mongoid.rb

Instance Method Summary collapse

Instance Method Details

#field?(attributes_name) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/searchlight/adapters/mongoid.rb', line 30

def field?(attributes_name)
  model_class.fields.has_key? attributes_name.to_s
end

#model_classObject



34
35
36
# File 'lib/searchlight/adapters/mongoid.rb', line 34

def model_class
  search_target.is_a?(::Mongoid::Criteria) ? search_target.klass : search_target
end

#searches(*attributes_names) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/searchlight/adapters/mongoid.rb', line 12

def searches(*attributes_names)
  super

  attributes_names.map do |attribute_name|
    method_name = "search_#{attribute_name}"
    if field?(attribute_name)
      define_method method_name do
        search.where(attribute_name.to_s => public_send(attribute_name))
      end
    else
      define_method method_name do
        raise Searchlight::Adapters::Mongoid::UndefinedColumn,
        "Class `#{self.class.model_class}` has no field `#{attribute_name}`; please define `search_#{attribute_name}` on `#{self.class}` to clarify what you intend to search for"
      end
    end
  end
end