1 module ActionController
2 # Macros are class-level calls that add pre-defined actions to the controller based on the parameters passed in.
3 # Currently, they're used to bridge the JavaScript macros, like autocompletion and in-place editing, with the controller
6 module AutoComplete #:nodoc:
7 def self.included(base) #:nodoc:
8 base.extend(ClassMethods)
11 # DEPRECATION WARNING: This method will become a separate plugin when Rails 2.0 ships.
16 # class BlogController < ApplicationController
17 # auto_complete_for :post, :title
21 # <%= text_field_with_auto_complete :post, title %>
23 # By default, auto_complete_for limits the results to 10 entries,
24 # and sorts by the given field.
26 # auto_complete_for takes a third parameter, an options hash to
27 # the find method used to search for the records:
29 # auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC'
31 # For help on defining text input fields with autocompletion,
32 # see ActionView::Helpers::JavaScriptHelper.
34 # For more examples, see script.aculo.us:
35 # * http://script.aculo.us/demos/ajax/autocompleter
36 # * http://script.aculo.us/demos/ajax/autocompleter_customized
38 def auto_complete_for(object, method, options = {})
39 define_method("auto_complete_for_#{object}_#{method}") do
41 :conditions => [ "LOWER(#{method}) LIKE ?", '%' + params[object][method].downcase + '%' ],
42 :order => "#{method} ASC",
43 :limit => 10 }.merge!(options)
45 @items = object.to_s.camelize.constantize.find(:all, find_options)
47 render :inline => "<%= auto_complete_result @items, '#{method}' %>"