Sha256: 2152a1f821ec20e52a52b8502ae522cd25cc9c328e36fb0e32ad5abc6c5182f8
Contents?: true
Size: 1.69 KB
Versions: 1
Compression:
Stored size: 1.69 KB
Contents
module Rails3JQueryAutocompleteMoc def self.included(base) base.extend(ClassMethods) end # Inspired on DHH's autocomplete plugin # # Usage: # # class ProductsController < Admin::BaseController # autocomplete :brand, :name # end # # This will magically generate an action autocomplete_brand_name, so, # don't forget to add it on your routes file # # resources :products do # get :autocomplete_brand_name, :on => :collection # end # # Now, on your view, all you have to do is have a text field like: # # f.text_field :brand_name, :autocomplete => autocomplete_brand_name_products_path # # module ClassMethods def autocomplete(object, method, options = {}) limit = options[:limit] || 10 order = options[:order] || "#{method} ASC" define_method("autocomplete_#{object}_#{method}") do unless params[:term] && params[:term].empty? unless object == "quick_search_term".to_sym items = object.to_s.camelize.constantize.where(["LOWER(#{method}) LIKE ?", "#{(options[:full] ? '%' : '')}#{params[:term].downcase}%"]).limit(limit).order(order) else items = QuickSearchTerm.terms_for_search_string(params[:term]).limit(limit) end else items = {} end render :json => json_for_autocomplete(items, (options[:display_value] ? options[:display_value] : method),(options[:value] ? options[:value] : method)) end end end private def json_for_autocomplete(items, label, value) items.collect {|i| {"id" => i.id, "label" => i.send(label), "value" => i.send(value)}} end end class ActionController::Base include Rails3JQueryAutocompleteMoc end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rails3-jquery-autocomplete-moc-0.3.0 | lib/rails3-jquery-autocomplete-moc.rb |