| Class | ActiveRecord::Base |
| In: |
lib/active_record_bridge.rb
lib/active_record_integration.rb |
| Parent: | Object |
This method has following consequences:
Practically speaking, a SearchApi::Search::Base subclass that targets this model is created, prefilled with many automatic search keys (see SearchApi::Bridge::ActiveRecord).
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
# File lib/active_record_integration.rb, line 234
234: def has_search_api(&block) # :yields:
235: # Creates a new SearchApi::Search::Base subclass
236: search_class = Class.new(::SearchApi::Search::Base)
237:
238: # Tells the SearchApi::Search::Base subclass which models it searches in
239: search_class.model(self, :type_cast=>true)
240:
241: # Let given block define search keys
242: search_class.class_eval(&block) if block
243:
244: (class << self; self; end).instance_eval do
245: # The search_class method returns the SearchApi::Search::Base subclass.
246: define_method(:search_class) { search_class }
247:
248: # Alter class behavior so that the SearchApi::Search::Base subclass seemlessly integrates.
249: include ::SearchApi::Integration::ActiveRecord::Base
250: end
251:
252: nil # don't pollute class creation
253: end
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.
# File lib/active_record_bridge.rb, line 372
372: def search_api_bridge
373: SearchApi::Bridge::ActiveRecord.new(self)
374: end