Ticket #3880: nested_form_helper.patch
| File nested_form_helper.patch, 4.6 kB (added by seangeo@gmail.com, 2 years ago) |
|---|
-
C:/Projects/rails/actionpack/lib/action_view/helpers/form_helper.rb
old new 231 231 DEFAULT_DATE_OPTIONS = { :discard_type => true }.freeze unless const_defined?(:DEFAULT_DATE_OPTIONS) 232 232 233 233 def initialize(object_name, method_name, template_object, local_binding = nil, object = nil) 234 @object_name, @method_name = object_name.to_s, method_name.to_s 234 @object_name, @method_names = object_name.to_s, method_name.to_s.split(/\./) 235 @method_name = @method_names.last 235 236 @template_object, @local_binding = template_object, local_binding 236 237 @object = object 237 238 if @object_name.sub!(/\[\]$/,"") … … 322 323 end 323 324 324 325 def object 325 @object || @template_object.instance_variable_get("@#{@object_name}") 326 target = @object || @template_object.instance_variable_get("@#{@object_name}") 327 @method_names[0..-2].each do |method_name_fragment| 328 target = target.send(method_name_fragment) 329 end 330 target 326 331 end 327 332 328 333 def value 329 unless object.nil? 334 unless object.nil? 330 335 object.send(@method_name) 331 336 end 332 337 end 333 338 334 def value_before_type_cast 335 unless object.nil? 339 def value_before_type_cast 340 object = self.object 341 unless object.nil? 336 342 object.respond_to?(@method_name + "_before_type_cast") ? 337 343 object.send(@method_name + "_before_type_cast") : 338 344 object.send(@method_name) … … 353 359 options["id"] ||= tag_id 354 360 end 355 361 end 356 362 363 def method_names_for_tag_name 364 @method_names.map {|method_name| "[#{method_name}]"}.join('') 365 end 366 357 367 def tag_name 358 "#{@object_name} [#{@method_name}]"368 "#{@object_name}#{method_names_for_tag_name}" 359 369 end 360 370 361 371 def tag_name_with_index(index) 362 "#{@object_name}[#{index}] [#{@method_name}]"372 "#{@object_name}[#{index}]#{method_names_for_tag_name}" 363 373 end 364 374 375 def method_names_for_id 376 @method_names.join('_') 377 end 378 365 379 def tag_id 366 "#{@object_name}_#{ @method_name}"380 "#{@object_name}_#{method_names_for_id}" 367 381 end 368 382 369 383 def tag_id_with_index(index) 370 "#{@object_name}_#{index}_#{ @method_name}"384 "#{@object_name}_#{index}_#{method_names_for_id}" 371 385 end 372 386 end 373 387 -
C:/Projects/rails/actionpack/test/template/form_helper_test.rb
old new 8 8 include ActionView::Helpers::TextHelper 9 9 10 10 silence_warnings do 11 Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on, :cost) 11 Post = Struct.new("Post", :title, :author, :body, :secret, :written_on, :cost) 12 Person = Struct.new("Person", :name, :email_address) 12 13 Post.class_eval do 13 14 alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast) 14 15 alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast) 15 alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)16 #alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast) 16 17 end 17 18 end 18 19 … … 24 25 def @post.id_before_type_cast; 123; end 25 26 26 27 @post.title = "Hello World" 27 @post.author_name = "" 28 @post.author = Person.new 29 @post.author.name = "Joe Bloggs" 30 @post.author.email_address = "joe.bloggs@example.com" 28 31 @post.body = "Back to the hill and over it again!" 29 32 @post.secret = 1 30 33 @post.written_on = Date.new(2004, 6, 15) … … 66 69 expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />' 67 70 assert_dom_equal expected, text_field("post", "title", "maxlength" => 35) 68 71 assert_dom_equal expected, text_field("post", "title", :maxlength => 35) 72 end 73 74 def test_text_field_with_nested_value 75 expected = '<input id="post_author_name" name="post[author][name]" size="30" type="text" value="Joe Bloggs" />' 76 assert_dom_equal(expected, text_field("post", "author.name")) 69 77 end 70 78 71 79 def test_check_box