class Lolita::Search::Simple

Default search class for Lolita::Search. Lolita::Configuration::Search uses this by default. It accepts method name as constructor argument, when none is given it call Lolita::DBI#search.

Attributes

dbi[R]
search_method[RW]

Method in model used to run a search.

Public Class Methods

new(dbi, *args) click to toggle source

Accepts search method as optional argument

# File lib/lolita/search/simple.rb, line 13
def initialize(dbi, *args)
  @dbi = dbi
  @options = args.extract_options!
  @search_method = args[0]
end

Public Instance Methods

run(query,*args) click to toggle source

Require dbi (Lolita::DBI instance), query (String) and request and dbi as optional argument. Also you can pass options.

Example

search.run("query",:fields => [:name])
# this will search only in :name field
search.run("query",nil, Lolita::DBI::Base.create(Category))
# this will use Category dbi for search

When there is search method defined, it uses that otherwise run default search.

# File lib/lolita/search/simple.rb, line 27
def run(query,*args)
  with_query(query,*args) do
    if self.search_method
      run_custom_search
    else
      run_default_search
    end
  end
end

Private Instance Methods

with_query(query,*args) { || ... } click to toggle source
# File lib/lolita/search/simple.rb, line 39
def with_query(query,*args)
  begin
    options = args.extract_options!
    @old_dbi = self.dbi
    @old_options = @options 
    @options = options if options.any?
    @dbi = args[1] if args[1]
    @query = query
    @request = args[0]
    yield
  ensure
    @dbi = @old_dbi
    @options = @old_options
    @query,@request = nil,nil
  end
end