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 219 219 yield builder.new(object_name, object, self, options, block) 220 220 end 221 221 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 222 241 # Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object 223 242 # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a 224 243 # hash with +options+. These options will be tagged onto the html as an HTML element attribute as in the example … … 398 417 end 399 418 end 400 419 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 401 428 def to_input_field_tag(field_type, options = {}) 402 429 options = options.stringify_keys 403 430 options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size") … … 574 601 @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc 575 602 end 576 603 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| 578 605 src = <<-end_src 579 606 def #{selector}(method, options = {}) 580 607 @template.send(#{selector.inspect}, @object_name, method, options.merge(:object => @object)) … … 588 615 @template.fields_for(name, *args, &block) 589 616 end 590 617 618 def label(method, text = nil, options = {}) 619 @template.label(@object_name, method, text, options.merge(:object => @object)) 620 end 621 591 622 def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") 592 623 @template.check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value) 593 624 end -
actionpack/test/template/form_helper_test.rb
old new 70 70 @controller = @controller.new 71 71 end 72 72 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 73 86 def test_text_field 74 87 assert_dom_equal( 75 88 '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title") … … 275 288 _erbout = '' 276 289 277 290 form_for(:post, @post, :html => { :id => 'create-post' }) do |f| 291 _erbout.concat f.label(:title) 278 292 _erbout.concat f.text_field(:title) 279 293 _erbout.concat f.text_area(:body) 280 294 _erbout.concat f.check_box(:secret) … … 283 297 284 298 expected = 285 299 "<form action='http://www.example.com' id='create-post' method='post'>" + 300 "<label for='post_title'>Title</label>" + 286 301 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 287 302 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 288 303 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +