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

Ticket #4627: radio_button_label_option.patch

File radio_button_label_option.patch, 4.2 kB (added by gregg@kellogg-assoc.com, 3 years ago)

radio_button_label_option.patch

  • actionpack/lib/action_view/helpers/form_helper.rb

    old new  
    214214      # hash with +options+. 
    215215      # Example (call, result). Imagine that @post.category returns "rails": 
    216216      #   radio_button("post", "category", "rails") 
    217       #   radio_button("post", "category", "java"
     217      #   radio_button("post", "category", "java", :label => "Java"
    218218      #     <input type="radio" id="post_category" name="post[category]" value="rails" checked="checked" /> 
    219       #     <input type="radio" id="post_category" name="post[category]" value="java" /
     219      #     <input type="radio" id="post_category" name="post[category]" value="java">Java</input
    220220      # 
    221221      def radio_button(object_name, method, tag_value, options = {}) 
    222222        InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_radio_button_tag(tag_value, options) 
     
    265265          "#{@object_name}_#{@auto_index}_#{@method_name}_#{pretty_tag_value}" : 
    266266          "#{@object_name}_#{@method_name}_#{pretty_tag_value}" 
    267267        add_default_name_and_id(options) 
    268         tag("input", options) 
     268        if options.has_key?("label") 
     269          content_tag("input", html_escape(options.delete("label")), options) 
     270        else 
     271          tag("input", options) 
     272        end 
    269273      end 
    270274 
    271275      def to_text_area_tag(options = {}) 
  • actionpack/lib/action_view/helpers/form_tag_helper.rb

    old new  
    109109      end 
    110110 
    111111      # Creates a radio button. 
     112      # 
     113      # Options: 
     114      # * <tt>:label</tt> - A string specifying the content of the tag. 
     115      #     # Outputs <input id="people" name="people" type="radio" value="david">david</input> 
     116      #     <%=  radio_button_tag "people", "david", false, "label" => "david" %> 
    112117      def radio_button_tag(name, value, checked = false, options = {}) 
    113118        html_options = { "type" => "radio", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys) 
    114119        html_options["checked"] = "checked" if checked 
    115         tag :input, html_options 
     120        if (options.has_key?(:label)) 
     121          content_tag :input, html_options.delete("label"), html_options 
     122       else 
     123          tag :input, html_options 
     124        end 
    116125      end 
    117126 
    118127      # Creates a submit button with the text <tt>value</tt> as the caption. If options contains a pair with the key of "disable_with", 
  • actionpack/test/template/form_helper_test.rb

    old new  
    121121   ) 
    122122  end 
    123123 
     124  def test_radio_button_with_label 
     125    assert_dom_equal('<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World">Goodby World</input>', 
     126      radio_button("post", "title", "Goodbye World", :label => "Goodby World") 
     127    ) 
     128  end 
     129 
    124130  def test_text_area 
    125131    assert_dom_equal( 
    126132      '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>', 
  • actionpack/test/template/form_tag_helper_test.rb

    old new  
    5151    assert_dom_equal expected, actual 
    5252  end 
    5353 
     54  def test_radio_button_tag_with_label 
     55    actual = radio_button_tag "people", "david", false, :label => "david" 
     56    expected = %(<input id="people" name="people" type="radio" value="david">david</input>) 
     57    assert_dom_equal expected, actual 
     58  end 
     59 
    5460  def test_select_tag 
    5561    actual = select_tag "people", "<option>david</option>" 
    5662    expected = %(<select id="people" name="people"><option>david</option></select>)