Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/search_api/active_record_integration.rb,
lib/search_api/active_record_bridge.rb
Class Method Summary collapse
-
.has_search_api(&block) ⇒ Object
This method has following consequences:.
-
.search_api_bridge ⇒ Object
Returns an SearchApi::Bridge::ActiveRecord instance.
Class Method Details
.has_search_api(&block) ⇒ Object
This method has following consequences:
-
The ActiveRecord::Base class is made searchable.
Practically speaking, a SearchApi::Search::Base subclass that targets this model is created, prefilled with many automatic search keys (see SearchApi::Bridge::ActiveRecord).
-
The ActiveRecord::Base class is able to define its own condition hash keys.
Practically speaking, the SearchApi::Integration::ActiveRecord::Base methods are included, and specifically its
search
method that allows to define custom keys for condition hashes.
Example:
class People < ActiveRecord::Base
has_search_api
# define age search key
search :age do |search|
{ :conditions => ['birth_date BETWEEN ? AND ?',
(Date.today-search.age.years),
(Date.today-(search.age-1).years+1.day)]}
end
end
People.search_class # => the SearchApi::Search::Base subclass for People.
People.find(:all, :conditions => { :age => 30 })
Optional block is for advanced purpose only. It is executed as a class_eval
block for the SearchApi::Search::Base subclass.
class People < ActiveRecord::Base
has_search_api do
...
end
end
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/search_api/active_record_integration.rb', line 234 def has_search_api(&block) # :yields: # Creates a new SearchApi::Search::Base subclass search_class = Class.new(::SearchApi::Search::Base) # Tells the SearchApi::Search::Base subclass which models it searches in search_class.model(self, :type_cast=>true) # Let given block define search keys search_class.class_eval(&block) if block (class << self; self; end).instance_eval do # The search_class method returns the SearchApi::Search::Base subclass. define_method(:search_class) { search_class } # Alter class behavior so that the SearchApi::Search::Base subclass seemlessly integrates. include ::SearchApi::Integration::ActiveRecord::Base end nil # don't pollute class creation end |
.search_api_bridge ⇒ Object
Returns an SearchApi::Bridge::ActiveRecord instance.
The presence of this method allows ActiveRecord::Base subclasses to be used as models by SearchApi::Search::Base subclasses.
381 382 383 |
# File 'lib/search_api/active_record_bridge.rb', line 381 def search_api_bridge SearchApi::Bridge::ActiveRecord.new(self) end |