Class: Lolita::Configuration::Search
- Includes:
- Builder
- Defined in:
- lib/lolita/configuration/search.rb
Overview
Proxy class for search. It supports two methods #with and #run. By default with method accepts method name or nothing and creates Lolita::Search::Simple instance that will be used to run search on current dbi. By default search run against all content fields, but when :fields options is passed it search in those fields only.
Example
class Post < ActiveRecord::Base
include Lolita::Configuration
lolita do
list do
search :fields => [:name]
end
end
end
#with method also accepts class or instance of some class, that will be used to run search. That class should have #run method that accepts query and request.
Example
class MyCustomSearch
def initialize(dbi)
@dbi = dbi
end
def run(query,request = nil)
@dbi.klass.where(:my_field => query)
end
end
Also you can put your search method in model. For more see Lolita::Search::Simple
Example
class Post < ActiveRecord::Base
include Lolita::Configuration
lolita do
list do
search :my_custom_search
end
end
def self.my_custom_search(query,request)
self.where(:title => query, :user_id => request.params[:user_id])
end
end
Instance Attribute Summary collapse
-
#dbi ⇒ Object
readonly
Returns the value of attribute dbi.
-
#fields ⇒ Object
writeonly
Sets the attribute fields.
- #with(value = nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(dbi, *args, &block) ⇒ Search
constructor
A new instance of Search.
- #run(query, request = nil) ⇒ Object
- #update(method_name, list, request) ⇒ Object
Methods included from Builder
#build, #builder, #builder=, #builder_default_name, #builder_default_options, #builder_default_state
Constructor Details
#initialize(dbi, *args, &block) ⇒ Search
Returns a new instance of Search.
48 49 50 51 52 53 |
# File 'lib/lolita/configuration/search.rb', line 48 def initialize dbi, *args, &block @dbi = dbi set_attributes(args ? args. : {}) instance_eval(&block) if block_given? @with ||= args[0]!=true && args[0] ? args[0] : nil end |
Instance Attribute Details
#dbi ⇒ Object (readonly)
Returns the value of attribute dbi.
45 46 47 |
# File 'lib/lolita/configuration/search.rb', line 45 def dbi @dbi end |
#fields=(value) ⇒ Object (writeonly)
Sets the attribute fields
46 47 48 |
# File 'lib/lolita/configuration/search.rb', line 46 def fields=(value) @fields = value end |
#with(value = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/lolita/configuration/search.rb', line 55 def with(value = nil) @with = value if value if !@with || [String,Symbol].include?(@with.class) @with = Lolita::Search::Simple.new(dbi,@with, :fields => @fields) elsif @with.class == Class initialize_arity = @with.instance_method(:initialize).arity @with = if initialize_arity < 0 || initialize_arity > 1 @with.new(dbi,:fields => @fields) else @with.new(dbi) end end @with end |
Instance Method Details
#run(query, request = nil) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/lolita/configuration/search.rb', line 70 def run(query,request = nil) if self.with.method(:run).arity < 0 || self.with.method(:run).arity > 1 self.with.run(query,request) else self.with.run(query) end end |
#update(method_name, list, request) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/lolita/configuration/search.rb', line 78 def update method_name,list,request if method_name == :paginate && list.search search_criteria = list.search.run(request && request.params[:q] || "",request) page_criteria = if search_criteria.respond_to?(:where) list.page_criteria.merge(search_criteria) elsif search_criteria.nil? list.page_criteria else search_criteria end list.instance_variable_set(:@page_criteria,page_criteria) end end |