Module: MongoMapper::Plugins::Clortho::ClassMethods

Defined in:
lib/clortho.rb

Instance Method Summary collapse

Instance Method Details

#searchable(*args) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/clortho.rb', line 26

def searchable(*args)
  options = args.extract_options!
  
  args.each do |arg|
    key :"#{arg}_keywords", String, default: ""
    key :"#{arg}_keywords_array", Array, default: []
    
    searchable_with_options << [arg.to_sym, options]
    
    class_eval <<-CODE
      class << self
        def search_#{arg}_keywords_for(*keywords)
          records = []
          keywords.each do |word|
            records << self.all.select{ |record| record[:'#{arg}_keywords_array'].include?(word.downcase) }
          end
          records.flatten.uniq
        end
      end
    CODE
  end
end