Module: QuickSearch::ClassMethods

Defined in:
lib/quick-search/search.rb

Instance Method Summary collapse

Instance Method Details

#quick_search(search, tokens_array = []) ⇒ Object

Performs a quick search. Returns the used tokens in the second parameter.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/quick-search/search.rb', line 22

def quick_search(search, tokens_array = [])
  adapter # fail fast, if the adapter can not be created

  relation = all
  (search || '').split(/\s+/).each do |token|
    next unless token.present?

    if exprs = eval_expressions(relation, token)
      relation = exprs
      next
    end

    @quick_search_fields ||= adapter.default_quick_search_fields
    tokens_array << token

    relation = adapter.make_clauses_for_token(relation, token, @quick_search_fields)
  end
  relation
end

#quick_search_expression(rx, proc) ⇒ Object

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.

Parameters:

  • rx (Regexp)

    A regular expression, in which to test the quick search tokens.

  • proc (Proc)

    The proc to run when the token matches. It will receive the MatchData as parameter.



17
18
19
# File 'lib/quick-search/search.rb', line 17

def quick_search_expression(rx, proc)
  (@quick_search_expressions ||= {})[rx] = proc
end

#quick_search_fields(*fields) ⇒ Object

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



9
10
11
# File 'lib/quick-search/search.rb', line 9

def quick_search_fields(*fields)
  @quick_search_fields = fields
end