removing log dir from .gitignore
[monkeycharger.git] / vendor / rails / actionpack / lib / action_controller / macros / auto_complete.rb
blob8282e2c10c8b9d760bb2ec0c3be27d0e13365901
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
4   # backing.
5   module Macros
6     module AutoComplete #:nodoc:
7       def self.included(base) #:nodoc:
8         base.extend(ClassMethods)
9       end
11       # DEPRECATION WARNING: This method will become a separate plugin when Rails 2.0 ships.
12       #
13       # Example:
14       #
15       #   # Controller
16       #   class BlogController < ApplicationController
17       #     auto_complete_for :post, :title
18       #   end
19       #
20       #   # View
21       #   <%= text_field_with_auto_complete :post, title %>
22       #
23       # By default, auto_complete_for limits the results to 10 entries,
24       # and sorts by the given field.
25       # 
26       # auto_complete_for takes a third parameter, an options hash to
27       # the find method used to search for the records:
28       #
29       #   auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC'
30       #
31       # For help on defining text input fields with autocompletion, 
32       # see ActionView::Helpers::JavaScriptHelper.
33       #
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
37       module ClassMethods
38         def auto_complete_for(object, method, options = {})
39           define_method("auto_complete_for_#{object}_#{method}") do
40             find_options = { 
41               :conditions => [ "LOWER(#{method}) LIKE ?", '%' + params[object][method].downcase + '%' ], 
42               :order => "#{method} ASC",
43               :limit => 10 }.merge!(options)
44             
45             @items = object.to_s.camelize.constantize.find(:all, find_options)
47             render :inline => "<%= auto_complete_result @items, '#{method}' %>"
48           end
49         end
50       end
51     end
52   end
53 end