Module: Mongoid::TextSearch::ClassMethods

Defined in:
lib/mongoid_text_search.rb

Instance Method Summary collapse

Instance Method Details

#generate_clean_keywords(string) ⇒ Object



39
40
41
# File 'lib/mongoid_text_search.rb', line 39

def generate_clean_keywords(string)
  KeywordGenerator.clean_keywords(string)
end

#search_fieldsObject



12
13
14
# File 'lib/mongoid_text_search.rb', line 12

def search_fields
  @search_fields ||= []
end

#text_search_for_fields(*fields) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mongoid_text_search.rb', line 16

def text_search_for_fields(*fields)
  @search_fields = fields
          
  # Create keyword field that will store all the keywords.
  field :keywords, :type => Array
  index :keywords
  
  # Create a method for searching based on keywords of all specified field.
  singleton = class << self; self end
  singleton.send :define_method, "with_keywords" do |keywords, *options|
    options = options.first || {}
    if !keywords.nil? && keywords.count > 0
      if options[:match].to_s == "all" || options.empty?
        where(:keywords.all => keywords.collect!{ |keyword| /.*#{keyword}.*/i })
      elsif options[:match].to_s == "any"
        where(:keywords.in => keywords.collect!{ |keyword| /.*#{keyword}.*/i })
      end
    else
      where() # Select everything.
    end
  end
end