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

Ticket #7139: patch-for-7139.2.diff

File patch-for-7139.2.diff, 10.5 kB (added by jarkko, 2 years ago)

Updated patch with support for form_for and form builders

  • actionpack/test/template/form_helper_test.rb

    old new  
    7676    assert_equal expected, text_field(object_name, "title") 
    7777    assert_equal object_name, "post[]" 
    7878  end 
     79   
     80  def test_label_for_without_value 
     81    expected = '<label for="post_title">Post Title</label>' 
     82    assert_equal expected, label_for("post", "title") 
     83    assert_equal expected, label_for(:post, :title) 
     84     
     85    expected = '<label for="post_author_name">Post Author name</label>' 
     86    assert_equal expected, label_for(:post, :author_name) 
     87  end 
     88   
     89  def test_label_for 
     90    expected = '<label for="post_title">Funky Title</label>' 
     91    assert_equal expected, label_for("post", "title", "Funky Title") 
     92    assert_equal expected, label_for(:post, :title, "Funky Title") 
     93  end 
     94   
     95  def test_label_for_with_params 
     96    expected = '<label class="blues" for="post_title" id="funky">Funky Title</label>' 
     97    assert_equal expected, label_for("post", "title", "Funky Title",  
     98                                     :id => "funky", :class => "blues") 
     99  end 
     100   
     101  def test_label_for_doesnt_change_param_values 
     102    object_name = 'post[]' 
     103    expected = '<label for="post_123_title">Post Title</label>' 
     104    assert_equal expected, label_for(object_name, "title") 
     105    assert_equal object_name, "post[]" 
     106  end 
    79107 
    80108  def test_check_box 
    81109    assert_dom_equal( 
     
    229257    _erbout = '' 
    230258 
    231259    form_for(:post, @post, :html => { :id => 'create-post' }) do |f| 
     260      _erbout.concat f.label_for(:title) 
    232261      _erbout.concat f.text_field(:title) 
    233262      _erbout.concat f.text_area(:body) 
    234263      _erbout.concat f.check_box(:secret) 
     
    236265 
    237266    expected =  
    238267      "<form action='http://www.example.com' id='create-post' method='post'>" + 
     268      "<label for='post_title'>Post Title</label>" + 
    239269      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 
    240270      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 
    241271      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + 
     
    249279    _erbout = '' 
    250280 
    251281    form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f| 
     282      _erbout.concat f.label_for(:title) 
    252283      _erbout.concat f.text_field(:title) 
    253284      _erbout.concat f.text_area(:body) 
    254285      _erbout.concat f.check_box(:secret) 
     
    257288    expected =  
    258289      "<form action='http://www.example.com' id='create-post' method='post'>" + 
    259290      "<div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>" + 
     291      "<label for='post_title'>Post Title</label>" + 
    260292      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 
    261293      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 
    262294      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + 
     
    270302    _erbout = '' 
    271303 
    272304    form_for(:post, :html => { :id => 'create-post' }) do |f| 
     305      _erbout.concat f.label_for(:title) 
    273306      _erbout.concat f.text_field(:title) 
    274307      _erbout.concat f.text_area(:body) 
    275308      _erbout.concat f.check_box(:secret) 
     
    277310 
    278311    expected =  
    279312      "<form action='http://www.example.com' id='create-post' method='post'>" + 
     313      "<label for='post_title'>Post Title</label>" + 
    280314      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 
    281315      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 
    282316      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + 
     
    290324    _erbout = '' 
    291325     
    292326    form_for("post[]", @post) do |f| 
     327      _erbout.concat f.label_for(:title) 
    293328      _erbout.concat f.text_field(:title) 
    294329      _erbout.concat f.text_area(:body) 
    295330      _erbout.concat f.check_box(:secret) 
     
    297332     
    298333    expected =  
    299334      "<form action='http://www.example.com' method='post'>" + 
     335      "<label for='post_123_title'>Post Title</label>" + 
    300336      "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" + 
    301337      "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 
    302338      "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" + 
     
    325361    _erbout = '' 
    326362 
    327363    fields_for(:post, @post) do |f| 
     364      _erbout.concat f.label_for(:title) 
    328365      _erbout.concat f.text_field(:title) 
    329366      _erbout.concat f.text_area(:body) 
    330367      _erbout.concat f.check_box(:secret) 
    331368    end 
    332369 
    333370    expected =  
     371      "<label for='post_title'>Post Title</label>" + 
    334372      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 
    335373      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 
    336374      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + 
     
    342380  def test_fields_for_without_object 
    343381    _erbout = '' 
    344382    fields_for(:post) do |f| 
     383      _erbout.concat f.label_for(:title) 
    345384      _erbout.concat f.text_field(:title) 
    346385      _erbout.concat f.text_area(:body) 
    347386      _erbout.concat f.check_box(:secret) 
    348387    end 
    349388 
    350389    expected =  
     390      "<label for='post_title'>Post Title</label>" + 
    351391      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 
    352392      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 
    353393      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + 
     
    374414    _erbout = '' 
    375415 
    376416    form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form| 
     417      _erbout.concat post_form.label_for(:title) 
    377418      _erbout.concat post_form.text_field(:title) 
    378419      _erbout.concat post_form.text_area(:body) 
    379420 
    380421      fields_for(:parent_post, @post) do |parent_fields| 
     422        _erbout.concat parent_fields.label_for(:secret) 
    381423        _erbout.concat parent_fields.check_box(:secret) 
    382424      end 
    383425    end 
    384426 
    385427    expected =  
    386428      "<form action='http://www.example.com' id='create-post' method='post'>" + 
     429      "<label for='post_title'>Post Title</label>" + 
    387430      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 
    388431      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 
     432      "<label for='parent_post_secret'>Parent_post Secret</label>" + 
    389433      "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" + 
    390434      "<input name='parent_post[secret]' type='hidden' value='0' />" + 
    391435      "</form>" 
  • actionpack/lib/action_view/helpers/form_helper.rb

    old new  
    151151        yield builder.new(object_name, object, self, options, block) 
    152152      end 
    153153 
     154      # Returns a label tag tailored for labeling a form field for a  
     155      # specified attribute (identified by +method+) on an object 
     156      # assigned to the template (identified by +object+). Additional  
     157      # options on the label tag can be passed as a 
     158      # hash with +options+. 
     159      # 
     160      # Examples (call, result): 
     161      #   label_for("post", "title") 
     162      #     <label for="post_title">Post Title</label> 
     163      #      
     164      #   label_for("post", "title", "Funky Title") 
     165      #     <label for="post_title">Funky Title</label> 
     166      # 
     167      #   label_for("post", "title", "Funky Title", :id => "funky") 
     168      #     <label for="post_title" id="funky">Funky Title</label> 
     169      def label_for(object_name, method, value = nil, options = {}) 
     170        InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_label_tag(value, options) 
     171      end 
     172 
    154173      # Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object 
    155174      # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a 
    156175      # hash with +options+. 
     
    280299        tag("input", options) 
    281300      end 
    282301 
     302      def to_label_tag(value = nil, options = {}) 
     303        options = options.stringify_keys 
     304        value ||= "#{sanitized_object_name.capitalize} #{@method_name.gsub("_", " ").capitalize}" 
     305        add_default_for(options) 
     306        content_tag_without_error_wrapping("label", value, options) 
     307      end 
     308 
    283309      def to_text_area_tag(options = {}) 
    284310        options = DEFAULT_TEXT_AREA_OPTIONS.merge(options.stringify_keys) 
    285311        add_default_name_and_id(options) 
     
    391417            options["id"]   ||= tag_id 
    392418          end 
    393419        end 
     420         
     421        def add_default_for(options) 
     422          if options.has_key?("index") 
     423            options["for"]   ||= tag_id_with_index(options["index"]) 
     424            options.delete("index") 
     425          elsif defined?(@auto_index) 
     426            options["for"]   ||= tag_id_with_index(@auto_index) 
     427          else 
     428            options["for"]   ||= tag_id 
     429          end 
     430        end 
    394431 
    395432        def tag_name 
    396433          "#{@object_name}[#{@method_name}]" 
     
    424461        @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc         
    425462      end 
    426463       
    427       (field_helpers - %w(check_box radio_button fields_for)).each do |selector| 
     464      (field_helpers - %w(check_box radio_button fields_for label_for)).each do |selector| 
    428465        src = <<-end_src 
    429466          def #{selector}(method, options = {}) 
    430467            @template.send(#{selector.inspect}, @object_name, method, options.merge(:object => @object)) 
     
    438475        @template.fields_for(name, *args, &block) 
    439476      end 
    440477 
     478      def label_for(method, value = nil, options = {}) 
     479        @template.label_for(@object_name, method, value, options.merge(:object => @object)) 
     480      end 
     481 
    441482      def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") 
    442483        @template.check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value) 
    443484      end