Module: Autosuggest::ControllerMacros
- Included in:
- ActionController::Base
- Defined in:
- lib/autosuggest/controller_macros.rb
Instance Method Summary collapse
-
#autosuggest(object, name, options = {}) ⇒ Object
when called, you must add a custom route for action like this: resources :products do get :autosuggest_brand_name, :on => :collection end.
Instance Method Details
#autosuggest(object, name, options = {}) ⇒ Object
when called, you must add a custom route for action like this: resources :products do
get :autosuggest_brand_name, :on => :collection
end
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/autosuggest/controller_macros.rb', line 9 def autosuggest(object, name, ={}) [:display] ||= name [:order] ||= "#{name} ASC" [:limit] ||= 10 [:name] = name define_method "autosuggest_#{object}_#{name}" do .merge!(:query => params[:query], :object => objectify(object), :like_clause => resolve_like_clause) results = db_store(object).query() render :json => Yajl::Encoder.encode(results.map{|r| {:name => r.send([:display]), :value => r.id.to_s}}) end end |