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

Ticket #8641: form_helper_labels.diff

File form_helper_labels.diff, 5.0 kB (added by jcoglan, 2 years ago)
  • actionpack/lib/action_view/helpers/form_helper.rb

    old new  
    219219        yield builder.new(object_name, object, self, options, block) 
    220220      end 
    221221 
     222      # Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object 
     223      # assigned to the template (identified by +object+). The text of label will default to the attribute name unless you specify 
     224      # it explicitly. Additional options on the label tag can be passed as a hash with +options+. These options will be tagged 
     225      # onto the html as an HTML element attribute as in the example shown. 
     226      # 
     227      # ==== Examples 
     228      #   label(:post, :title) 
     229      #   #=> <label for="post_title">Title</label> 
     230      # 
     231      #   label(:post, :title, "A short title") 
     232      #   #=> <label for="post_title">A short title</label> 
     233      # 
     234      #   label(:post, :title, "A short title", :class => "title_label") 
     235      #   #=> <label for="post_title" class="title_label">A short title</label> 
     236      # 
     237      def label(object_name, method, text = nil, options = {}) 
     238        InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_label_tag(text, options) 
     239      end 
     240 
    222241      # Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object 
    223242      # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a 
    224243      # hash with +options+.  These options will be tagged onto the html as an HTML element attribute as in the example 
     
    398417        end 
    399418      end 
    400419 
     420      def to_label_tag(text = nil, options = {}) 
     421        name_and_id = options.dup 
     422        add_default_name_and_id(name_and_id) 
     423        options["for"] = name_and_id["id"] 
     424        content = (text.blank? ? nil : text.to_s) || method_name.humanize 
     425        content_tag("label", content, options) 
     426      end 
     427 
    401428      def to_input_field_tag(field_type, options = {}) 
    402429        options = options.stringify_keys 
    403430        options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size") 
     
    574601        @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc         
    575602      end 
    576603       
    577       (field_helpers - %w(check_box radio_button fields_for)).each do |selector| 
     604      (field_helpers - %w(label check_box radio_button fields_for)).each do |selector| 
    578605        src = <<-end_src 
    579606          def #{selector}(method, options = {}) 
    580607            @template.send(#{selector.inspect}, @object_name, method, options.merge(:object => @object)) 
     
    588615        @template.fields_for(name, *args, &block) 
    589616      end 
    590617 
     618      def label(method, text = nil, options = {}) 
     619        @template.label(@object_name, method, text, options.merge(:object => @object)) 
     620      end 
     621 
    591622      def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") 
    592623        @template.check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value) 
    593624      end 
  • actionpack/test/template/form_helper_test.rb

    old new  
    7070    @controller = @controller.new 
    7171  end 
    7272 
     73  def test_label 
     74    assert_dom_equal('<label for="post_title">Title</label>', label("post", "title")) 
     75    assert_dom_equal('<label for="post_title">The title goes here</label>', label("post", "title", "The title goes here")) 
     76    assert_dom_equal( 
     77      '<label class="title_label" for="post_title">Title</label>', 
     78      label("post", "title", nil, :class => 'title_label') 
     79    ) 
     80  end 
     81   
     82  def test_label_with_symbols 
     83    assert_dom_equal('<label for="post_title">Title</label>', label(:post, :title)) 
     84  end 
     85 
    7386  def test_text_field 
    7487    assert_dom_equal( 
    7588      '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title") 
     
    275288    _erbout = '' 
    276289 
    277290    form_for(:post, @post, :html => { :id => 'create-post' }) do |f| 
     291      _erbout.concat f.label(:title) 
    278292      _erbout.concat f.text_field(:title) 
    279293      _erbout.concat f.text_area(:body) 
    280294      _erbout.concat f.check_box(:secret) 
     
    283297 
    284298    expected =  
    285299      "<form action='http://www.example.com' id='create-post' method='post'>" + 
     300      "<label for='post_title'>Title</label>" + 
    286301      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 
    287302      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 
    288303      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +