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

Ticket #3880: nested_form_helper.patch

File nested_form_helper.patch, 4.6 kB (added by seangeo@gmail.com, 2 years ago)
  • C:/Projects/rails/actionpack/lib/action_view/helpers/form_helper.rb

    old new  
    231231      DEFAULT_DATE_OPTIONS = { :discard_type => true }.freeze unless const_defined?(:DEFAULT_DATE_OPTIONS) 
    232232 
    233233      def initialize(object_name, method_name, template_object, local_binding = nil, object = nil) 
    234         @object_name, @method_name = object_name.to_s, method_name.to_s 
     234        @object_name, @method_names = object_name.to_s, method_name.to_s.split(/\./) 
     235        @method_name = @method_names.last 
    235236        @template_object, @local_binding = template_object, local_binding 
    236237        @object = object 
    237238        if @object_name.sub!(/\[\]$/,"") 
     
    322323      end 
    323324       
    324325      def object 
    325         @object || @template_object.instance_variable_get("@#{@object_name}") 
     326        target = @object || @template_object.instance_variable_get("@#{@object_name}") 
     327        @method_names[0..-2].each do |method_name_fragment| 
     328          target = target.send(method_name_fragment) 
     329        end 
     330        target 
    326331      end 
    327  
     332       
    328333      def value 
    329         unless object.nil? 
     334        unless object.nil?      
    330335          object.send(@method_name) 
    331336        end 
    332337      end 
    333338 
    334       def value_before_type_cast 
    335         unless object.nil? 
     339      def value_before_type_cast 
     340        object = self.object 
     341        unless object.nil?         
    336342          object.respond_to?(@method_name + "_before_type_cast") ? 
    337343            object.send(@method_name + "_before_type_cast") : 
    338344            object.send(@method_name) 
     
    353359            options["id"]   ||= tag_id 
    354360          end 
    355361        end 
    356  
     362 
     363        def method_names_for_tag_name 
     364          @method_names.map {|method_name| "[#{method_name}]"}.join('') 
     365        end 
     366         
    357367        def tag_name 
    358           "#{@object_name}[#{@method_name}]
     368          "#{@object_name}#{method_names_for_tag_name}
    359369        end 
    360370 
    361371        def tag_name_with_index(index) 
    362           "#{@object_name}[#{index}][#{@method_name}]
     372          "#{@object_name}[#{index}]#{method_names_for_tag_name}
    363373        end 
    364  
     374 
     375        def method_names_for_id 
     376          @method_names.join('_') 
     377        end 
     378         
    365379        def tag_id 
    366           "#{@object_name}_#{@method_name}" 
     380          "#{@object_name}_#{method_names_for_id}" 
    367381        end 
    368382 
    369383        def tag_id_with_index(index) 
    370           "#{@object_name}_#{index}_#{@method_name}" 
     384          "#{@object_name}_#{index}_#{method_names_for_id}" 
    371385        end 
    372386    end 
    373387 
  • C:/Projects/rails/actionpack/test/template/form_helper_test.rb

    old new  
    88  include ActionView::Helpers::TextHelper 
    99 
    1010  silence_warnings do 
    11     Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on, :cost) 
     11    Post = Struct.new("Post", :title, :author, :body, :secret, :written_on, :cost) 
     12    Person = Struct.new("Person", :name, :email_address) 
    1213    Post.class_eval do 
    1314      alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast) 
    1415      alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast) 
    15       alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast) 
     16      #alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast) 
    1617    end 
    1718  end 
    1819 
     
    2425    def @post.id_before_type_cast; 123; end 
    2526 
    2627    @post.title       = "Hello World" 
    27     @post.author_name = "" 
     28    @post.author = Person.new 
     29    @post.author.name = "Joe Bloggs" 
     30    @post.author.email_address = "joe.bloggs@example.com" 
    2831    @post.body        = "Back to the hill and over it again!" 
    2932    @post.secret      = 1 
    3033    @post.written_on  = Date.new(2004, 6, 15) 
     
    6669    expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />' 
    6770    assert_dom_equal expected, text_field("post", "title", "maxlength" => 35) 
    6871    assert_dom_equal expected, text_field("post", "title", :maxlength => 35) 
     72  end 
     73   
     74  def test_text_field_with_nested_value 
     75    expected = '<input id="post_author_name" name="post[author][name]" size="30" type="text" value="Joe Bloggs" />' 
     76    assert_dom_equal(expected, text_field("post", "author.name")) 
    6977  end 
    7078 
    7179  def test_check_box