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

Ticket #7139: patch-for-7139.diff

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

Patch with tests and docs

  • 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( 
  • 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}]"