Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 2191

Show
Ignore:
Timestamp:
09/11/05 07:52:53 (3 years ago)
Author:
david
Message:

Added in-place editing support in the spirit of auto complete with ActionController::Base.in_place_edit_for, JavascriptHelper#in_place_editor_field, and Javascript support from script.aculo.us #2038 [Jon Tirsen] Moved auto-completion and in-place editing into the Macros module and their helper counterparts into JavaScriptMacrosHelper

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r2184 r2191  
    11*SVN* 
     2 
     3* Moved auto-completion and in-place editing into the Macros module and their helper counterparts into JavaScriptMacrosHelper 
     4 
     5* Added in-place editing support in the spirit of auto complete with ActionController::Base.in_place_edit_for, JavascriptHelper#in_place_editor_field, and Javascript support from script.aculo.us #2038 [Jon Tirsen] 
    26 
    37* Added :disabled option to all data selects that'll make the elements inaccessible for change #2167, #253 [eigentone] 
  • trunk/actionpack/lib/action_controller.rb

    r1915 r2191  
    5050require 'action_controller/verification' 
    5151require 'action_controller/streaming' 
    52 require 'action_controller/auto_complete' 
    5352require 'action_controller/session_management' 
     53require 'action_controller/macros/auto_complete' 
     54require 'action_controller/macros/in_place_editing' 
    5455 
    5556require 'action_view' 
     
    7273  include ActionController::Verification 
    7374  include ActionController::Streaming 
    74   include ActionController::AutoComplete 
    7575  include ActionController::SessionManagement 
     76  include ActionController::Macros::AutoComplete 
     77  include ActionController::Macros::InPlaceEditing 
    7678end 
  • trunk/actionpack/lib/action_controller/macros/auto_complete.rb

    r1815 r2191  
    11module ActionController 
    2   module AutoComplete #:nodoc: 
    3     def self.append_features(base) #:nodoc: 
    4       super 
    5       base.extend(ClassMethods) 
    6     end 
     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.append_features(base) #:nodoc: 
     8        super 
     9        base.extend(ClassMethods) 
     10      end 
    711 
    8     # Example: 
    9    
    10     #   # Controller 
    11     #   class BlogController < ApplicationController 
    12     #     auto_complete_for :post, :title 
    13     #   end 
    14    
    15     #   # View 
    16     #   <%= text_field_with_auto_complete :post, title %> 
    17    
    18     # By default, auto_complete_for limits the results to 10 entries, 
    19     # and sorts by the given field. 
    20     #  
    21     # auto_complete_for takes a third parameter, an options hash to 
    22     # the find method used to search for the records: 
    23    
    24     #   auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC' 
    25    
    26     # For help on defining text input fields with autocompletion,  
    27     # see ActionView::Helpers::JavaScriptHelper. 
    28    
    29     # For more examples, see script.aculo.us: 
    30     # * http://script.aculo.us/demos/ajax/autocompleter 
    31     # * http://script.aculo.us/demos/ajax/autocompleter_customized 
    32     module ClassMethods 
    33       def auto_complete_for(object, method, options = {}) 
    34         define_method("auto_complete_for_#{object}_#{method}") do 
    35           find_options = {  
    36             :conditions => [ "LOWER(#{method}) LIKE ?", '%' + params[object][method].downcase + '%' ],  
    37             :order => "#{method} ASC", 
    38             :limit => 10 }.merge!(options) 
     12      # Example: 
     13     
     14      #   # Controller 
     15      #   class BlogController < ApplicationController 
     16      #     auto_complete_for :post, :title 
     17      #   end 
     18     
     19      #   # View 
     20      #   <%= text_field_with_auto_complete :post, title %> 
     21     
     22      # By default, auto_complete_for limits the results to 10 entries, 
     23      # and sorts by the given field. 
     24      #  
     25      # auto_complete_for takes a third parameter, an options hash to 
     26      # the find method used to search for the records: 
     27     
     28      #   auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC' 
     29     
     30      # For help on defining text input fields with autocompletion,  
     31      # see ActionView::Helpers::JavaScriptHelper. 
     32     
     33      # For more examples, see script.aculo.us: 
     34      # * http://script.aculo.us/demos/ajax/autocompleter 
     35      # * http://script.aculo.us/demos/ajax/autocompleter_customized 
     36      module ClassMethods 
     37        def auto_complete_for(object, method, options = {}) 
     38          define_method("auto_complete_for_#{object}_#{method}") do 
     39            find_options = {  
     40              :conditions => [ "LOWER(#{method}) LIKE ?", '%' + params[object][method].downcase + '%' ],  
     41              :order => "#{method} ASC", 
     42              :limit => 10 }.merge!(options) 
    3943             
    40           @items = object.to_s.camelize.constantize.find(:all, find_options) 
     44            @items = object.to_s.camelize.constantize.find(:all, find_options) 
    4145 
    42           render :inline => "<%= auto_complete_result @items, '#{method}' %>" 
     46            render :inline => "<%= auto_complete_result @items, '#{method}' %>" 
     47          end 
    4348        end 
    4449      end 
  • trunk/actionpack/lib/action_view/helpers/form_helper.rb

    r2165 r2191  
    235235        tag_text << ">True</option></select>" 
    236236      end 
    237  
     237       
     238      def to_content_tag(tag_name, options = {}) 
     239        content_tag(tag_name, value, options) 
     240      end 
     241       
    238242      def object 
    239243        @template_object.instance_variable_get "@#{@object_name}" 
  • trunk/actionpack/lib/action_view/helpers/javascript_helper.rb

    r2164 r2191  
    343343          build_observer('Form.EventObserver', form_id, options) 
    344344        end 
    345       end 
    346        
    347             
    348       # Adds AJAX autocomplete functionality to the text input field with the  
    349       # DOM ID specified by +field_id+. 
    350       # 
    351       # This function expects that the called action returns a HTML <ul> list, 
    352       # or nothing if no entries should be displayed for autocompletion. 
    353       # 
    354       # You'll probably want to turn the browser's built-in autocompletion off, 
    355       # su be sure to include a autocomplete="off" attribute with your text 
    356       # input field. 
    357       #  
    358       # Required +options+ are: 
    359       # <tt>:url</tt>::       Specifies the DOM ID of the element whose 
    360       #                       innerHTML should be updated with the autocomplete 
    361       #                       entries returned by XMLHttpRequest. 
    362       #  
    363       # Addtional +options+ are: 
    364       # <tt>:update</tt>::    Specifies the DOM ID of the element whose  
    365       #                       innerHTML should be updated with the autocomplete 
    366       #                       entries returned by the AJAX request.  
    367       #                       Defaults to field_id + '_auto_complete' 
    368       # <tt>:with</tt>::      A JavaScript expression specifying the 
    369       #                       parameters for the XMLHttpRequest. This defaults 
    370       #                       to 'fieldname=value'. 
    371       # <tt>:indicator</tt>:: Specifies the DOM ID of an elment which will be 
    372       #                       displayed while autocomplete is running.  
    373       def auto_complete_field(field_id, options = {}) 
    374         function =  "new Ajax.Autocompleter(" 
    375         function << "'#{field_id}', " 
    376         function << "'" + (options[:update] || "#{field_id}_auto_complete") + "', " 
    377         function << "'#{url_for(options[:url])}'" 
    378          
    379         js_options = {} 
    380         js_options[:tokens] = array_or_string_for_javascript(options[:tokens]) if options[:tokens] 
    381         js_options[:callback]   = "function(element, value) { return #{options[:with]} }" if options[:with] 
    382         js_options[:indicator]  = "'#{options[:indicator]}'" if options[:indicator] 
    383         function << (', ' + options_for_javascript(js_options) + ')') 
    384  
    385         javascript_tag(function) 
    386       end 
    387        
    388       # Use this method in your view to generate a return for the AJAX automplete requests. 
    389       # 
    390       # Example action: 
    391       # 
    392       #   def auto_complete_for_item_title 
    393       #     @items = Item.find(:all,  
    394       #       :conditions => [ 'LOWER(description) LIKE ?',  
    395       #       '%' + request.raw_post.downcase + '%' ]) 
    396       #     render :inline => '<%= auto_complete_result(@items, 'description') %>' 
    397       #   end 
    398       # 
    399       # The auto_complete_result can of course also be called from a view belonging to the  
    400       # auto_complete action if you need to decorate it further. 
    401       def auto_complete_result(entries, field, phrase = nil) 
    402         return unless entries 
    403         items = entries.map { |entry| content_tag("li", phrase ? highlight(entry[field], phrase) : h(entry[field])) } 
    404         content_tag("ul", items.uniq) 
    405       end 
    406        
    407       # Wrapper for text_field with added AJAX autocompletion functionality. 
    408       # 
    409       # In your controller, you'll need to define an action called 
    410       # auto_complete_for_object_method to respond the AJAX calls, 
    411       #  
    412       # See the RDoc on ActionController::AutoComplete to learn more about this. 
    413       def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {}) 
    414         (completion_options[:skip_style] ? "" : auto_complete_stylesheet) + 
    415         text_field(object, method, { :autocomplete => "off" }.merge!(tag_options)) + 
    416         content_tag("div", "", :id => "#{object}_#{method}_auto_complete", :class => "auto_complete") + 
    417         auto_complete_field("#{object}_#{method}", { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options)) 
    418345      end 
    419346