Ticket #7139: patch-for-7139.2.diff
| File patch-for-7139.2.diff, 10.5 kB (added by jarkko, 2 years ago) |
|---|
-
actionpack/test/template/form_helper_test.rb
old new 76 76 assert_equal expected, text_field(object_name, "title") 77 77 assert_equal object_name, "post[]" 78 78 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 79 107 80 108 def test_check_box 81 109 assert_dom_equal( … … 229 257 _erbout = '' 230 258 231 259 form_for(:post, @post, :html => { :id => 'create-post' }) do |f| 260 _erbout.concat f.label_for(:title) 232 261 _erbout.concat f.text_field(:title) 233 262 _erbout.concat f.text_area(:body) 234 263 _erbout.concat f.check_box(:secret) … … 236 265 237 266 expected = 238 267 "<form action='http://www.example.com' id='create-post' method='post'>" + 268 "<label for='post_title'>Post Title</label>" + 239 269 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 240 270 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 241 271 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + … … 249 279 _erbout = '' 250 280 251 281 form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f| 282 _erbout.concat f.label_for(:title) 252 283 _erbout.concat f.text_field(:title) 253 284 _erbout.concat f.text_area(:body) 254 285 _erbout.concat f.check_box(:secret) … … 257 288 expected = 258 289 "<form action='http://www.example.com' id='create-post' method='post'>" + 259 290 "<div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>" + 291 "<label for='post_title'>Post Title</label>" + 260 292 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 261 293 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 262 294 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + … … 270 302 _erbout = '' 271 303 272 304 form_for(:post, :html => { :id => 'create-post' }) do |f| 305 _erbout.concat f.label_for(:title) 273 306 _erbout.concat f.text_field(:title) 274 307 _erbout.concat f.text_area(:body) 275 308 _erbout.concat f.check_box(:secret) … … 277 310 278 311 expected = 279 312 "<form action='http://www.example.com' id='create-post' method='post'>" + 313 "<label for='post_title'>Post Title</label>" + 280 314 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 281 315 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 282 316 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + … … 290 324 _erbout = '' 291 325 292 326 form_for("post[]", @post) do |f| 327 _erbout.concat f.label_for(:title) 293 328 _erbout.concat f.text_field(:title) 294 329 _erbout.concat f.text_area(:body) 295 330 _erbout.concat f.check_box(:secret) … … 297 332 298 333 expected = 299 334 "<form action='http://www.example.com' method='post'>" + 335 "<label for='post_123_title'>Post Title</label>" + 300 336 "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" + 301 337 "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 302 338 "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" + … … 325 361 _erbout = '' 326 362 327 363 fields_for(:post, @post) do |f| 364 _erbout.concat f.label_for(:title) 328 365 _erbout.concat f.text_field(:title) 329 366 _erbout.concat f.text_area(:body) 330 367 _erbout.concat f.check_box(:secret) 331 368 end 332 369 333 370 expected = 371 "<label for='post_title'>Post Title</label>" + 334 372 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 335 373 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 336 374 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + … … 342 380 def test_fields_for_without_object 343 381 _erbout = '' 344 382 fields_for(:post) do |f| 383 _erbout.concat f.label_for(:title) 345 384 _erbout.concat f.text_field(:title) 346 385 _erbout.concat f.text_area(:body) 347 386 _erbout.concat f.check_box(:secret) 348 387 end 349 388 350 389 expected = 390 "<label for='post_title'>Post Title</label>" + 351 391 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 352 392 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 353 393 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + … … 374 414 _erbout = '' 375 415 376 416 form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form| 417 _erbout.concat post_form.label_for(:title) 377 418 _erbout.concat post_form.text_field(:title) 378 419 _erbout.concat post_form.text_area(:body) 379 420 380 421 fields_for(:parent_post, @post) do |parent_fields| 422 _erbout.concat parent_fields.label_for(:secret) 381 423 _erbout.concat parent_fields.check_box(:secret) 382 424 end 383 425 end 384 426 385 427 expected = 386 428 "<form action='http://www.example.com' id='create-post' method='post'>" + 429 "<label for='post_title'>Post Title</label>" + 387 430 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 388 431 "<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>" + 389 433 "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" + 390 434 "<input name='parent_post[secret]' type='hidden' value='0' />" + 391 435 "</form>" -
actionpack/lib/action_view/helpers/form_helper.rb
old new 151 151 yield builder.new(object_name, object, self, options, block) 152 152 end 153 153 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 154 173 # Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object 155 174 # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a 156 175 # hash with +options+. … … 280 299 tag("input", options) 281 300 end 282 301 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 283 309 def to_text_area_tag(options = {}) 284 310 options = DEFAULT_TEXT_AREA_OPTIONS.merge(options.stringify_keys) 285 311 add_default_name_and_id(options) … … 391 417 options["id"] ||= tag_id 392 418 end 393 419 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 394 431 395 432 def tag_name 396 433 "#{@object_name}[#{@method_name}]" … … 424 461 @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc 425 462 end 426 463 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| 428 465 src = <<-end_src 429 466 def #{selector}(method, options = {}) 430 467 @template.send(#{selector.inspect}, @object_name, method, options.merge(:object => @object)) … … 438 475 @template.fields_for(name, *args, &block) 439 476 end 440 477 478 def label_for(method, value = nil, options = {}) 479 @template.label_for(@object_name, method, value, options.merge(:object => @object)) 480 end 481 441 482 def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") 442 483 @template.check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value) 443 484 end