module QuickSearch::ClassMethods

Public Instance Methods

quick_search_expression(rx, proc) click to toggle source

Adds an expression to the quick search. Several expressions can be added, but only the first one that matches the search token will be ran. @param [Regexp] rx A regular expression, in which to test the quick search tokens. @param [Proc] proc The proc to run when the token matches. It will receive the MatchData as parameter.

# File lib/quick-search/search.rb, line 17
def quick_search_expression(rx, proc)
  (@quick_search_expressions ||= {})[rx] = proc
end
quick_search_fields(*fields) click to toggle source

Defines the fields that will be available to the quick search. If a hash is used on ActiveRecord, an +inner join+ will be made.

# File lib/quick-search/search.rb, line 9
def quick_search_fields(*fields)
  @quick_search_fields = fields
end

Private Instance Methods

adapter() click to toggle source
# File lib/quick-search/search.rb, line 44
def adapter
  @adapter ||= begin
    if defined?(ActiveRecord) && defined?(ActiveRecord::Base) && self < ActiveRecord::Base
      require 'quick-search/adapters/active_record_adapter'
      Adapters::ActiveRecordAdapter.new(self)
    elsif defined?(Mongoid) && defined?(Mongoid::Document) && self < Mongoid::Document
      require 'quick-search/adapters/mongoid_adapter'
      Adapters::MongoidAdapter.new(self)
    else
      raise UnsupportedAdapter.new self.name
    end
  end
end
eval_expressions(s, token) click to toggle source

Evaluates the expressions defined by quick_search_expression. @return [Object] the new query, or nil if there's no expression matching the token.

# File lib/quick-search/search.rb, line 60
def eval_expressions(s, token)
  return nil if @quick_search_expressions.blank?
  @quick_search_expressions.each do |rx, proc|
    if m = (/\A(?:#{rx})\z/.match(token))
      s = s.instance_exec(m, &proc)
      return s
    end
  end
  nil
end