form_helper_test.rb 46.3 KB
Newer Older
1
require 'abstract_unit'
D
Initial  
David Heinemeier Hansson 已提交
2

3 4 5 6 7 8
silence_warnings do
  Post = Struct.new(:title, :author_name, :body, :secret, :written_on, :cost)
  Post.class_eval do
    alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast)
    alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast)
    alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)
9
    alias_method :secret?, :secret
10

11 12 13
    def new_record=(boolean)
      @new_record = boolean
    end
14

15 16 17
    def new_record?
      @new_record
    end
18 19 20 21 22 23

    attr_accessor :author
    def author_attributes=(attributes); end

    attr_accessor :comments
    def comments_attributes=(attributes); end
24
  end
25 26 27 28

  class Comment
    attr_reader :id
    attr_reader :post_id
29
    def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
30 31
    def save; @id = 1; @post_id = 1 end
    def new_record?; @id.nil? end
32
    def to_param; @id; end
33
    def name
34
      @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
35 36
    end
  end
37

38 39 40 41 42
  class Author < Comment
    attr_accessor :post
    def post_attributes=(attributes); end
  end
end
43

44 45
class FormHelperTest < ActionView::TestCase
  tests ActionView::Helpers::FormHelper
D
Initial  
David Heinemeier Hansson 已提交
46 47

  def setup
48
    super
49
    @post = Post.new
50
    @comment = Comment.new
51 52 53 54
    def @post.errors()
      Class.new{
        def on(field); "can't be empty" if field == "author_name"; end
        def empty?() false end
55
        def count() 1 end
56 57
        def full_messages() [ "Author name can't be empty" ] end
      }.new
58
    end
59
    def @post.id; 123; end
60
    def @post.id_before_type_cast; 123; end
61
    def @post.to_param; '123'; end
62

D
Initial  
David Heinemeier Hansson 已提交
63 64 65 66 67
    @post.title       = "Hello World"
    @post.author_name = ""
    @post.body        = "Back to the hill and over it again!"
    @post.secret      = 1
    @post.written_on  = Date.new(2004, 6, 15)
68 69

    @controller = Class.new do
70
      attr_reader :url_for_options
71
      def url_for(options)
72
        @url_for_options = options
73 74 75 76
        "http://www.example.com"
      end
    end
    @controller = @controller.new
D
Initial  
David Heinemeier Hansson 已提交
77 78
  end

79 80 81 82 83 84 85
  def test_label
    assert_dom_equal('<label for="post_title">Title</label>', label("post", "title"))
    assert_dom_equal('<label for="post_title">The title goes here</label>', label("post", "title", "The title goes here"))
    assert_dom_equal(
      '<label class="title_label" for="post_title">Title</label>',
      label("post", "title", nil, :class => 'title_label')
    )
86
    assert_dom_equal('<label for="post_secret">Secret?</label>', label("post", "secret?"))
87
  end
88

89 90
  def test_label_with_symbols
    assert_dom_equal('<label for="post_title">Title</label>', label(:post, :title))
91
    assert_dom_equal('<label for="post_secret">Secret?</label>', label(:post, :secret?))
92 93
  end

94 95 96 97 98 99 100 101
  def test_label_with_for_attribute_as_symbol
    assert_dom_equal('<label for="my_for">Title</label>', label(:post, :title, nil, :for => "my_for"))
  end

  def test_label_with_for_attribute_as_string
    assert_dom_equal('<label for="my_for">Title</label>', label(:post, :title, nil, "for" => "my_for"))
  end

D
Initial  
David Heinemeier Hansson 已提交
102
  def test_text_field
103
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
104 105
      '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title")
    )
106
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
107 108
      '<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />', password_field("post", "title")
    )
109
    assert_dom_equal(
110
      '<input id="person_name" name="person[name]" size="30" type="password" />', password_field("person", "name")
D
Initial  
David Heinemeier Hansson 已提交
111 112 113 114 115
    )
  end

  def test_text_field_with_escapes
    @post.title = "<b>Hello World</b>"
116
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
117 118 119 120
      '<input id="post_title" name="post[title]" size="30" type="text" value="&lt;b&gt;Hello World&lt;/b&gt;" />', text_field("post", "title")
    )
  end

121 122 123 124 125 126 127 128
  def test_text_field_with_html_entities
    @post.title = "The HTML Entity for & is &amp;"
    assert_dom_equal(
      '<input id="post_title" name="post[title]" size="30" type="text" value="The HTML Entity for &amp; is &amp;amp;" />',
      text_field("post", "title")
    )
  end

D
Initial  
David Heinemeier Hansson 已提交
129
  def test_text_field_with_options
130
    expected = '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />'
131 132
    assert_dom_equal expected, text_field("post", "title", "size" => 35)
    assert_dom_equal expected, text_field("post", "title", :size => 35)
D
Initial  
David Heinemeier Hansson 已提交
133
  end
134

D
Initial  
David Heinemeier Hansson 已提交
135
  def test_text_field_assuming_size
136
    expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />'
137 138
    assert_dom_equal expected, text_field("post", "title", "maxlength" => 35)
    assert_dom_equal expected, text_field("post", "title", :maxlength => 35)
D
Initial  
David Heinemeier Hansson 已提交
139
  end
140

141 142 143 144 145 146
  def test_text_field_removing_size
    expected = '<input id="post_title" maxlength="35" name="post[title]" type="text" value="Hello World" />'
    assert_dom_equal expected, text_field("post", "title", "maxlength" => 35, "size" => nil)
    assert_dom_equal expected, text_field("post", "title", :maxlength => 35, :size => nil)
  end

147 148 149 150 151 152 153
  def test_text_field_doesnt_change_param_values
    object_name = 'post[]'
    expected = '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />'
    assert_equal expected, text_field(object_name, "title")
    assert_equal object_name, "post[]"
  end

154 155 156
  def test_hidden_field
    assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Hello World" />',
      hidden_field("post", "title")
157 158
      assert_dom_equal '<input id="post_secret" name="post[secret]" type="hidden" value="1" />',
        hidden_field("post", "secret?")
159 160 161 162 163 164 165 166 167 168 169 170 171
  end

  def test_hidden_field_with_escapes
    @post.title = "<b>Hello World</b>"
    assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="&lt;b&gt;Hello World&lt;/b&gt;" />',
      hidden_field("post", "title")
  end

  def test_text_field_with_options
    assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Something Else" />',
      hidden_field("post", "title", :value => "Something Else")
  end

D
Initial  
David Heinemeier Hansson 已提交
172
  def test_check_box
173
    assert_dom_equal(
174
      '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
D
Initial  
David Heinemeier Hansson 已提交
175 176 177
      check_box("post", "secret")
    )
    @post.secret = 0
178
    assert_dom_equal(
179
      '<input name="post[secret]" type="hidden" value="0" /><input id="post_secret" name="post[secret]" type="checkbox" value="1" />',
180
      check_box("post", "secret")
D
Initial  
David Heinemeier Hansson 已提交
181
    )
182
    assert_dom_equal(
183
      '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
184 185
      check_box("post", "secret" ,{"checked"=>"checked"})
    )
D
Initial  
David Heinemeier Hansson 已提交
186
    @post.secret = true
187
    assert_dom_equal(
188
      '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
D
Initial  
David Heinemeier Hansson 已提交
189 190
      check_box("post", "secret")
    )
191
    assert_dom_equal(
192
      '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
193 194
      check_box("post", "secret?")
    )
195 196 197

    @post.secret = ['0']
    assert_dom_equal(
198
      '<input name="post[secret]" type="hidden" value="0" /><input id="post_secret" name="post[secret]" type="checkbox" value="1" />',
199 200 201 202
      check_box("post", "secret")
    )
    @post.secret = ['1']
    assert_dom_equal(
203
      '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
204 205
      check_box("post", "secret")
    )
D
Initial  
David Heinemeier Hansson 已提交
206
  end
207

208 209
  def test_check_box_with_explicit_checked_and_unchecked_values
    @post.secret = "on"
210
    assert_dom_equal(
211
      '<input name="post[secret]" type="hidden" value="off" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" />',
212 213 214
      check_box("post", "secret", {}, "on", "off")
    )
  end
215

216 217
  def test_checkbox_disabled_still_submits_checked_value
    assert_dom_equal(
218
      '<input name="post[secret]" type="hidden" value="1" /><input checked="checked" disabled="disabled" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
219 220 221 222
      check_box("post", "secret", { :disabled => :true })
    )
  end

223
  def test_radio_button
224
    assert_dom_equal('<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
225
      radio_button("post", "title", "Hello World")
226
    )
227
    assert_dom_equal('<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
228
      radio_button("post", "title", "Goodbye World")
229
    )
230 231 232
    assert_dom_equal('<input id="item_subobject_title_inside_world" name="item[subobject][title]" type="radio" value="inside world"/>',
      radio_button("item[subobject]", "title", "inside world")
    )
233
  end
234

235 236 237 238 239
  def test_radio_button_is_checked_with_integers
    assert_dom_equal('<input checked="checked" id="post_secret_1" name="post[secret]" type="radio" value="1" />',
      radio_button("post", "secret", "1")
   )
  end
240

241 242 243 244 245
  def test_radio_button_respects_passed_in_id
     assert_dom_equal('<input checked="checked" id="foo" name="post[secret]" type="radio" value="1" />',
       radio_button("post", "secret", "1", :id=>"foo")
    )
  end
246

D
Initial  
David Heinemeier Hansson 已提交
247
  def test_text_area
248
    assert_dom_equal(
249
      '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
250 251 252
      text_area("post", "body")
    )
  end
253

D
Initial  
David Heinemeier Hansson 已提交
254 255
  def test_text_area_with_escapes
    @post.body        = "Back to <i>the</i> hill and over it again!"
256
    assert_dom_equal(
257
      '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to &lt;i&gt;the&lt;/i&gt; hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
258 259 260
      text_area("post", "body")
    )
  end
261

262 263 264 265 266 267
  def test_text_area_with_alternate_value
    assert_dom_equal(
      '<textarea cols="40" id="post_body" name="post[body]" rows="20">Testing alternate values.</textarea>',
      text_area("post", "body", :value => 'Testing alternate values.')
    )
  end
268

269 270 271 272 273 274 275 276
  def test_text_area_with_html_entities
    @post.body        = "The HTML Entity for & is &amp;"
    assert_dom_equal(
      '<textarea cols="40" id="post_body" name="post[body]" rows="20">The HTML Entity for &amp; is &amp;amp;</textarea>',
      text_area("post", "body")
    )
  end

277 278 279 280 281 282
  def test_text_area_with_size_option
    assert_dom_equal(
      '<textarea cols="183" id="post_body" name="post[body]" rows="820">Back to the hill and over it again!</textarea>',
      text_area("post", "body", :size => "183x820")
    )
  end
283

D
Initial  
David Heinemeier Hansson 已提交
284
  def test_explicit_name
285
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
286
      '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess")
287
    )
288
    assert_dom_equal(
289
      '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
290 291
      text_area("post", "body", "name" => "really!")
    )
292
    assert_dom_equal(
293
      '<input name="i mean it" type="hidden" value="0" /><input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" />',
D
Initial  
David Heinemeier Hansson 已提交
294 295
      check_box("post", "secret", "name" => "i mean it")
    )
296
    assert_dom_equal text_field("post", "title", "name" => "dont guess"),
297
                 text_field("post", "title", :name => "dont guess")
298
    assert_dom_equal text_area("post", "body", "name" => "really!"),
299
                 text_area("post", "body", :name => "really!")
300
    assert_dom_equal check_box("post", "secret", "name" => "i mean it"),
301
                 check_box("post", "secret", :name => "i mean it")
D
Initial  
David Heinemeier Hansson 已提交
302
  end
303

D
Initial  
David Heinemeier Hansson 已提交
304
  def test_explicit_id
305
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
306
      '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess")
307
    )
308
    assert_dom_equal(
309
      '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
310 311
      text_area("post", "body", "id" => "really!")
    )
312
    assert_dom_equal(
313
      '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" />',
D
Initial  
David Heinemeier Hansson 已提交
314 315
      check_box("post", "secret", "id" => "i mean it")
    )
316
    assert_dom_equal text_field("post", "title", "id" => "dont guess"),
317
                 text_field("post", "title", :id => "dont guess")
318
    assert_dom_equal text_area("post", "body", "id" => "really!"),
319
                 text_area("post", "body", :id => "really!")
320
    assert_dom_equal check_box("post", "secret", "id" => "i mean it"),
321
                 check_box("post", "secret", :id => "i mean it")
D
Initial  
David Heinemeier Hansson 已提交
322
  end
323 324 325

  def test_auto_index
    pid = @post.id
326 327 328 329
    assert_dom_equal(
      "<label for=\"post_#{pid}_title\">Title</label>",
      label("post[]", "title")
    )
330
    assert_dom_equal(
331 332
      "<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title")
    )
333
    assert_dom_equal(
334
      "<textarea cols=\"40\" id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
335 336
      text_area("post[]", "body")
    )
337
    assert_dom_equal(
338
      "<input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" /><input checked=\"checked\" id=\"post_#{pid}_secret\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" />",
339 340
      check_box("post[]", "secret")
    )
341
   assert_dom_equal(
342
"<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />",
343 344
      radio_button("post[]", "title", "Hello World")
    )
345
    assert_dom_equal("<input id=\"post_#{pid}_title_goodbye_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
346 347 348
      radio_button("post[]", "title", "Goodbye World")
    )
  end
349 350

  def test_form_for
351
    form_for(:post, @post, :html => { :id => 'create-post' }) do |f|
352 353 354 355 356
      concat f.label(:title)
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
      concat f.submit('Create post')
357 358
    end

359
    expected =
360
      "<form action='http://www.example.com' id='create-post' method='post'>" +
361
      "<label for='post_title'>Title</label>" +
362 363
      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
364
      "<input name='post[secret]' type='hidden' value='0' />" +
365
      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
366
      "<input name='commit' id='post_submit' type='submit' value='Create post' />" +
367 368
      "</form>"

369
    assert_dom_equal expected, output_buffer
370 371 372 373
  end

  def test_form_for_with_method
    form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f|
374 375 376
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
377 378
    end

379
    expected =
380
      "<form action='http://www.example.com' id='create-post' method='post'>" +
381
      "<div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>" +
382 383
      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
384
      "<input name='post[secret]' type='hidden' value='0' />" +
385
      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
386 387
      "</form>"

388
    assert_dom_equal expected, output_buffer
389 390
  end

391
  def test_form_for_without_object
392
    form_for(:post, :html => { :id => 'create-post' }) do |f|
393 394 395
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
396 397
    end

398
    expected =
399
      "<form action='http://www.example.com' id='create-post' method='post'>" +
400 401 402
      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
      "<input name='post[secret]' type='hidden' value='0' />" +
403
      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
404 405
      "</form>"

406
    assert_dom_equal expected, output_buffer
407
  end
408

409 410
  def test_form_for_with_index
    form_for("post[]", @post) do |f|
411 412 413 414
      concat f.label(:title)
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
415
    end
416

417
    expected =
418
      "<form action='http://www.example.com' method='post'>" +
419
      "<label for=\"post_123_title\">Title</label>" +
420 421
      "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
      "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
422
      "<input name='post[123][secret]' type='hidden' value='0' />" +
423
      "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
424
      "</form>"
425

426
    assert_dom_equal expected, output_buffer
427 428
  end

429 430
  def test_form_for_with_nil_index_option_override
    form_for("post[]", @post, :index => nil) do |f|
431 432 433
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
434 435 436 437 438 439 440
    end

    expected =
      "<form action='http://www.example.com' method='post'>" +
      "<input name='post[][title]' size='30' type='text' id='post__title' value='Hello World' />" +
      "<textarea name='post[][body]' id='post__body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
      "<input name='post[][secret]' type='hidden' value='0' />" +
441
      "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />" +
442 443
      "</form>"

444
    assert_dom_equal expected, output_buffer
445 446
  end

447 448 449
  def test_nested_fields_for
    form_for(:post, @post) do |f|
      f.fields_for(:comment, @post) do |c|
450
        concat c.text_field(:title)
451 452 453 454 455 456 457
      end
    end

    expected = "<form action='http://www.example.com' method='post'>" +
               "<input name='post[comment][title]' size='30' type='text' id='post_comment_title' value='Hello World' />" +
               "</form>"

458
    assert_dom_equal expected, output_buffer
459
  end
460

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
  def test_nested_fields_for_with_nested_collections
    form_for('post[]', @post) do |f|
      concat f.text_field(:title)
      f.fields_for('comment[]', @comment) do |c|
        concat c.text_field(:name)
      end
    end

    expected = "<form action='http://www.example.com' method='post'>" +
               "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
               "<input name='post[123][comment][][name]' size='30' type='text' id='post_123_comment__name' value='new comment' />" +
               "</form>"

    assert_dom_equal expected, output_buffer
  end

477
  def test_nested_fields_for_with_index_and_parent_fields
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    form_for('post', @post, :index => 1) do |c|
      concat c.text_field(:title)
      c.fields_for('comment', @comment, :index => 1) do |r|
        concat r.text_field(:name)
      end
    end

    expected = "<form action='http://www.example.com' method='post'>" +
               "<input name='post[1][title]' size='30' type='text' id='post_1_title' value='Hello World' />" +
               "<input name='post[1][comment][1][name]' size='30' type='text' id='post_1_comment_1_name' value='new comment' />" +
               "</form>"

    assert_dom_equal expected, output_buffer
  end

493
  def test_form_for_with_index_and_nested_fields_for
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
    form_for(:post, @post, :index => 1) do |f|
      f.fields_for(:comment, @post) do |c|
        concat c.text_field(:title)
      end
    end

    expected = "<form action='http://www.example.com' method='post'>" +
               "<input name='post[1][comment][title]' size='30' type='text' id='post_1_comment_title' value='Hello World' />" +
               "</form>"

    assert_dom_equal expected, output_buffer
  end

  def test_nested_fields_for_with_index_on_both
    form_for(:post, @post, :index => 1) do |f|
      f.fields_for(:comment, @post, :index => 5) do |c|
        concat c.text_field(:title)
      end
    end

    expected = "<form action='http://www.example.com' method='post'>" +
               "<input name='post[1][comment][5][title]' size='30' type='text' id='post_1_comment_5_title' value='Hello World' />" +
               "</form>"

    assert_dom_equal expected, output_buffer
  end

  def test_nested_fields_for_with_auto_index
    form_for("post[]", @post) do |f|
      f.fields_for(:comment, @post) do |c|
        concat c.text_field(:title)
      end
    end

    expected = "<form action='http://www.example.com' method='post'>" +
               "<input name='post[123][comment][title]' size='30' type='text' id='post_123_comment_title' value='Hello World' />" +
               "</form>"

    assert_dom_equal expected, output_buffer
  end

  def test_nested_fields_for_with_auto_index_on_both
    form_for("post[]", @post) do |f|
      f.fields_for("comment[]", @post) do |c|
        concat c.text_field(:title)
      end
    end

    expected = "<form action='http://www.example.com' method='post'>" +
               "<input name='post[123][comment][123][title]' size='30' type='text' id='post_123_comment_123_title' value='Hello World' />" +
               "</form>"

    assert_dom_equal expected, output_buffer
  end

  def test_nested_fields_for_with_index_and_auto_index
    form_for("post[]", @post) do |f|
      f.fields_for(:comment, @post, :index => 5) do |c|
        concat c.text_field(:title)
      end
    end

    form_for(:post, @post, :index => 1) do |f|
      f.fields_for("comment[]", @post) do |c|
        concat c.text_field(:title)
      end
    end

    expected = "<form action='http://www.example.com' method='post'>" +
               "<input name='post[123][comment][5][title]' size='30' type='text' id='post_123_comment_5_title' value='Hello World' />" +
               "</form>" +
               "<form action='http://www.example.com' method='post'>" +
               "<input name='post[1][comment][123][title]' size='30' type='text' id='post_1_comment_123_title' value='Hello World' />" +
               "</form>"

    assert_dom_equal expected, output_buffer
  end

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
  def test_nested_fields_for_with_a_new_record_on_a_nested_attributes_one_to_one_association
    @post.author = Author.new

    form_for(:post, @post) do |f|
      concat f.text_field(:title)
      f.fields_for(:author) do |af|
        concat af.text_field(:name)
      end
    end

    expected = '<form action="http://www.example.com" method="post">' +
               '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
               '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="new author" />' +
               '</form>'

    assert_dom_equal expected, output_buffer
  end

590 591 592 593 594 595 596 597 598
  def test_nested_fields_for_with_explicitly_passed_object_on_a_nested_attributes_one_to_one_association
    form_for(:post, @post) do |f|
      f.fields_for(:author, Author.new(123)) do |af|
        assert_not_nil af.object
        assert_equal 123, af.object.id
      end
    end
  end

599 600 601 602 603 604 605 606 607 608 609 610
  def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to_one_association
    @post.author = Author.new(321)

    form_for(:post, @post) do |f|
      concat f.text_field(:title)
      f.fields_for(:author) do |af|
        concat af.text_field(:name)
      end
    end

    expected = '<form action="http://www.example.com" method="post">' +
               '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
611
               '<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />' +
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
               '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' +
               '</form>'

    assert_dom_equal expected, output_buffer
  end

  def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collection_association
    @post.comments = Array.new(2) { |id| Comment.new(id + 1) }

    form_for(:post, @post) do |f|
      concat f.text_field(:title)
      @post.comments.each do |comment|
        f.fields_for(:comments, comment) do |cf|
          concat cf.text_field(:name)
        end
      end
    end

    expected = '<form action="http://www.example.com" method="post">' +
               '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
632 633 634 635
               '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' +
               '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' +
               '<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' +
               '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' +
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
               '</form>'

    assert_dom_equal expected, output_buffer
  end

  def test_nested_fields_for_with_new_records_on_a_nested_attributes_collection_association
    @post.comments = [Comment.new, Comment.new]

    form_for(:post, @post) do |f|
      concat f.text_field(:title)
      @post.comments.each do |comment|
        f.fields_for(:comments, comment) do |cf|
          concat cf.text_field(:name)
        end
      end
    end

    expected = '<form action="http://www.example.com" method="post">' +
               '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
655 656
               '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="new comment" />' +
               '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' +
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
               '</form>'

    assert_dom_equal expected, output_buffer
  end

  def test_nested_fields_for_with_existing_and_new_records_on_a_nested_attributes_collection_association
    @post.comments = [Comment.new(321), Comment.new]

    form_for(:post, @post) do |f|
      concat f.text_field(:title)
      @post.comments.each do |comment|
        f.fields_for(:comments, comment) do |cf|
          concat cf.text_field(:name)
        end
      end
    end

    expected = '<form action="http://www.example.com" method="post">' +
               '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
676 677 678
               '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
               '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' +
               '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' +
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
               '</form>'

    assert_dom_equal expected, output_buffer
  end

  def test_nested_fields_for_on_a_nested_attributes_collection_association_yields_only_builder
    @post.comments = [Comment.new(321), Comment.new]
    yielded_comments = []

    form_for(:post, @post) do |f|
      concat f.text_field(:title)
      f.fields_for(:comments) do |cf|
        concat cf.text_field(:name)
        yielded_comments << cf.object
      end
    end

    expected = '<form action="http://www.example.com" method="post">' +
               '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
698 699 700
               '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
               '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' +
               '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' +
701 702 703 704 705 706
               '</form>'

    assert_dom_equal expected, output_buffer
    assert_equal yielded_comments, @post.comments
  end

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
  def test_nested_fields_for_with_child_index_option_override_on_a_nested_attributes_collection_association
    @post.comments = []

    form_for(:post, @post) do |f|
      f.fields_for(:comments, Comment.new(321), :child_index => 'abc') do |cf|
        concat cf.text_field(:name)
      end
    end

    expected = '<form action="http://www.example.com" method="post">' +
               '<input id="post_comments_attributes_abc_id" name="post[comments_attributes][abc][id]" type="hidden" value="321" />' +
               '<input id="post_comments_attributes_abc_name" name="post[comments_attributes][abc][name]" size="30" type="text" value="comment #321" />' +
               '</form>'

    assert_dom_equal expected, output_buffer
  end

724
  def test_fields_for
725
    fields_for(:post, @post) do |f|
726 727 728
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
729 730
    end

731
    expected =
732 733
      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
734 735
      "<input name='post[secret]' type='hidden' value='0' />" +
      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />"
736

737
    assert_dom_equal expected, output_buffer
738 739 740 741
  end

  def test_fields_for_with_index
    fields_for("post[]", @post) do |f|
742 743 744
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
745 746 747 748 749
    end

    expected =
      "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
      "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
750 751
      "<input name='post[123][secret]' type='hidden' value='0' />" +
      "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />"
752

753
    assert_dom_equal expected, output_buffer
754 755 756 757
  end

  def test_fields_for_with_nil_index_option_override
    fields_for("post[]", @post, :index => nil) do |f|
758 759 760
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
761 762 763 764 765
    end

    expected =
      "<input name='post[][title]' size='30' type='text' id='post__title' value='Hello World' />" +
      "<textarea name='post[][body]' id='post__body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
766 767
      "<input name='post[][secret]' type='hidden' value='0' />" +
      "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />"
768

769
    assert_dom_equal expected, output_buffer
770 771 772 773
  end

  def test_fields_for_with_index_option_override
    fields_for("post[]", @post, :index => "abc") do |f|
774 775 776
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
777 778 779 780 781
    end

    expected =
      "<input name='post[abc][title]' size='30' type='text' id='post_abc_title' value='Hello World' />" +
      "<textarea name='post[abc][body]' id='post_abc_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
782 783
      "<input name='post[abc][secret]' type='hidden' value='0' />" +
      "<input name='post[abc][secret]' checked='checked' type='checkbox' id='post_abc_secret' value='1' />"
784

785
    assert_dom_equal expected, output_buffer
786
  end
787 788 789

  def test_fields_for_without_object
    fields_for(:post) do |f|
790 791 792
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
793 794
    end

795
    expected =
796 797
      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
798 799
      "<input name='post[secret]' type='hidden' value='0' />" +
      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />"
800

801
    assert_dom_equal expected, output_buffer
802 803 804 805
  end

  def test_fields_for_with_only_object
    fields_for(@post) do |f|
806 807 808
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
809 810
    end

811
    expected =
812 813
      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
814 815
      "<input name='post[secret]' type='hidden' value='0' />" +
      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />"
816

817
    assert_dom_equal expected, output_buffer
818 819
  end

820 821
  def test_fields_for_object_with_bracketed_name
    fields_for("author[post]", @post) do |f|
822 823
      concat f.label(:title)
      concat f.text_field(:title)
824 825
    end

826 827
    assert_dom_equal "<label for=\"author_post_title\">Title</label>" +
    "<input name='author[post][title]' size='30' type='text' id='author_post_title' value='Hello World' />",
828
      output_buffer
829 830
  end

831 832
  def test_fields_for_object_with_bracketed_name_and_index
    fields_for("author[post]", @post, :index => 1) do |f|
833 834
      concat f.label(:title)
      concat f.text_field(:title)
835 836 837 838
    end

    assert_dom_equal "<label for=\"author_post_1_title\">Title</label>" +
      "<input name='author[post][1][title]' size='30' type='text' id='author_post_1_title' value='Hello World' />",
839
      output_buffer
840 841
  end

842 843 844
  def test_form_builder_does_not_have_form_for_method
    assert ! ActionView::Helpers::FormBuilder.instance_methods.include?('form_for')
  end
845

846
  def test_form_for_and_fields_for
847
    form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
848 849
      concat post_form.text_field(:title)
      concat post_form.text_area(:body)
850

851
      fields_for(:parent_post, @post) do |parent_fields|
852
        concat parent_fields.check_box(:secret)
853 854 855
      end
    end

856
    expected =
857
      "<form action='http://www.example.com' id='create-post' method='post'>" +
858 859 860
      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
      "<input name='parent_post[secret]' type='hidden' value='0' />" +
861
      "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" +
862 863
      "</form>"

864
    assert_dom_equal expected, output_buffer
865
  end
866

867 868
  def test_form_for_and_fields_for_with_object
    form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
869 870
      concat post_form.text_field(:title)
      concat post_form.text_area(:body)
871 872

      post_form.fields_for(@comment) do |comment_fields|
873
        concat comment_fields.text_field(:name)
874 875 876
      end
    end

877
    expected =
878 879 880 881 882 883
      "<form action='http://www.example.com' id='create-post' method='post'>" +
      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
      "<input name='post[comment][name]' type='text' id='post_comment_name' value='new comment' size='30' />" +
      "</form>"

884
    assert_dom_equal expected, output_buffer
885 886
  end

887
  class LabelledFormBuilder < ActionView::Helpers::FormBuilder
888 889 890 891 892 893 894 895 896
    (field_helpers - %w(hidden_field)).each do |selector|
      src = <<-END_SRC
        def #{selector}(field, *args, &proc)
          "<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>"
        end
      END_SRC
      class_eval src, __FILE__, __LINE__
    end
  end
897

898
  def test_form_for_with_labelled_builder
899
    form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
900 901 902
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
903 904
    end

905
    expected =
906 907 908
      "<form action='http://www.example.com' method='post'>" +
      "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
      "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
909
      "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" +
910 911
      "</form>"

912
    assert_dom_equal expected, output_buffer
913
  end
914 915 916 917 918 919

  def test_default_form_builder
    old_default_form_builder, ActionView::Base.default_form_builder =
      ActionView::Base.default_form_builder, LabelledFormBuilder

    form_for(:post, @post) do |f|
920 921 922
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
923 924
    end

925
    expected =
926 927 928
      "<form action='http://www.example.com' method='post'>" +
      "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
      "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
929
      "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" +
930 931
      "</form>"

932
    assert_dom_equal expected, output_buffer
933 934 935
  ensure
    ActionView::Base.default_form_builder = old_default_form_builder
  end
936

937 938
  def test_default_form_builder_with_active_record_helpers
    form_for(:post, @post) do |f|
939 940
       concat f.error_message_on('author_name')
       concat f.error_messages
941 942 943 944
    end

    expected = %(<form action='http://www.example.com' method='post'>) +
               %(<div class='formError'>can't be empty</div>) +
945 946
               %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>) +
               %(</form>)
947

948
    assert_dom_equal expected, output_buffer
949 950

  end
951

952 953 954
  def test_default_form_builder_no_instance_variable
    post = @post
    @post = nil
955

956
    form_for(:post, post) do |f|
957 958
       concat f.error_message_on('author_name')
       concat f.error_messages
959 960 961 962
    end

    expected = %(<form action='http://www.example.com' method='post'>) +
               %(<div class='formError'>can't be empty</div>) +
963 964
               %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>) +
               %(</form>)
965

966
    assert_dom_equal expected, output_buffer
967

968
  end
969

970 971 972 973
  # Perhaps this test should be moved to prototype helper tests.
  def test_remote_form_for_with_labelled_builder
    self.extend ActionView::Helpers::PrototypeHelper

974
     remote_form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
975 976 977
       concat f.text_field(:title)
       concat f.text_area(:body)
       concat f.check_box(:secret)
978 979
     end

980
     expected =
981
       %(<form action="http://www.example.com" onsubmit="new Ajax.Request('http://www.example.com', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" method="post">) +
982 983
       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
       "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
984
       "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" +
985 986
       "</form>"

987
     assert_dom_equal expected, output_buffer
988
  end
989

990
  def test_fields_for_with_labelled_builder
991
    fields_for(:post, @post, :builder => LabelledFormBuilder) do |f|
992 993 994
      concat f.text_field(:title)
      concat f.text_area(:body)
      concat f.check_box(:secret)
995
    end
996 997

    expected =
998 999
      "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
      "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
1000
      "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>"
1001

1002
    assert_dom_equal expected, output_buffer
1003
  end
1004

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
  def test_form_for_with_labelled_builder_with_nested_fields_for_without_options_hash
    klass = nil

    form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
      f.fields_for(:comments, Comment.new) do |nested_fields|
        klass = nested_fields.class
        ''
      end
    end

    assert_equal LabelledFormBuilder, klass
  end

  def test_form_for_with_labelled_builder_with_nested_fields_for_with_options_hash
    klass = nil

    form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
      f.fields_for(:comments, Comment.new, :index => 'foo') do |nested_fields|
        klass = nested_fields.class
        ''
      end
    end

    assert_equal LabelledFormBuilder, klass
  end

  class LabelledFormBuilderSubclass < LabelledFormBuilder; end

  def test_form_for_with_labelled_builder_with_nested_fields_for_with_custom_builder
    klass = nil

    form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
      f.fields_for(:comments, Comment.new, :builder => LabelledFormBuilderSubclass) do |nested_fields|
        klass = nested_fields.class
        ''
      end
    end

    assert_equal LabelledFormBuilderSubclass, klass
  end

1046 1047 1048
  def test_form_for_with_html_options_adds_options_to_form_tag
    form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
    expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\"></form>"
1049

1050
    assert_dom_equal expected, output_buffer
1051
  end
1052

1053 1054 1055
  def test_form_for_with_string_url_option
    form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end

1056
    assert_equal '<form action="http://www.otherdomain.com" method="post"></form>', output_buffer
1057 1058 1059 1060 1061 1062 1063 1064
  end

  def test_form_for_with_hash_url_option
    form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end

    assert_equal 'controller', @controller.url_for_options[:controller]
    assert_equal 'action', @controller.url_for_options[:action]
  end
1065

1066 1067 1068 1069
  def test_form_for_with_record_url_option
    form_for(:post, @post, :url => @post) do |f| end

    expected = "<form action=\"/posts/123\" method=\"post\"></form>"
1070
    assert_equal expected, output_buffer
1071 1072 1073 1074 1075 1076
  end

  def test_form_for_with_existing_object
    form_for(@post) do |f| end

    expected = "<form action=\"/posts/123\" class=\"edit_post\" id=\"edit_post_123\" method=\"post\"><div style=\"margin:0;padding:0\"><input name=\"_method\" type=\"hidden\" value=\"put\" /></div></form>"
1077
    assert_equal expected, output_buffer
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
  end

  def test_form_for_with_new_object
    post = Post.new
    post.new_record = true
    def post.id() nil end

    form_for(post) do |f| end

    expected = "<form action=\"/posts\" class=\"new_post\" id=\"new_post\" method=\"post\"></form>"
1088
    assert_equal expected, output_buffer
1089 1090
  end

1091 1092 1093
  def test_form_for_with_existing_object_in_list
    @post.new_record = false
    @comment.save
1094

1095 1096 1097
    form_for([@post, @comment]) {}

    expected = %(<form action="#{comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div></form>)
1098
    assert_dom_equal expected, output_buffer
1099 1100 1101 1102
  end

  def test_form_for_with_new_object_in_list
    @post.new_record = false
1103

1104 1105 1106
    form_for([@post, @comment]) {}

    expected = %(<form action="#{comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
1107
    assert_dom_equal expected, output_buffer
1108 1109
  end

1110 1111 1112
  def test_form_for_with_existing_object_and_namespace_in_list
    @post.new_record = false
    @comment.save
1113

1114
    form_for([:admin, @post, @comment]) {}
1115

1116
    expected = %(<form action="#{admin_comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div></form>)
1117
    assert_dom_equal expected, output_buffer
1118
  end
1119

1120 1121
  def test_form_for_with_new_object_and_namespace_in_list
    @post.new_record = false
1122

1123
    form_for([:admin, @post, @comment]) {}
1124

1125
    expected = %(<form action="#{admin_comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
1126
    assert_dom_equal expected, output_buffer
1127 1128
  end

1129 1130 1131 1132
  def test_form_for_with_existing_object_and_custom_url
    form_for(@post, :url => "/super_posts") do |f| end

    expected = "<form action=\"/super_posts\" class=\"edit_post\" id=\"edit_post_123\" method=\"post\"><div style=\"margin:0;padding:0\"><input name=\"_method\" type=\"hidden\" value=\"put\" /></div></form>"
1133
    assert_equal expected, output_buffer
1134
  end
1135

1136 1137
  def test_remote_form_for_with_html_options_adds_options_to_form_tag
    self.extend ActionView::Helpers::PrototypeHelper
1138

1139 1140
    remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
    expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\" onsubmit=\"new Ajax.Request('http://www.example.com', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"></form>"
1141

1142
    assert_dom_equal expected, output_buffer
1143
  end
1144 1145

  protected
1146 1147 1148
    def comments_path(post)
      "/posts/#{post.id}/comments"
    end
1149
    alias_method :post_comments_path, :comments_path
1150 1151 1152 1153

    def comment_path(post, comment)
      "/posts/#{post.id}/comments/#{comment.id}"
    end
1154
    alias_method :post_comment_path, :comment_path
1155

1156 1157 1158 1159
    def admin_comments_path(post)
      "/admin/posts/#{post.id}/comments"
    end
    alias_method :admin_post_comments_path, :admin_comments_path
1160

1161 1162 1163 1164
    def admin_comment_path(post, comment)
      "/admin/posts/#{post.id}/comments/#{comment.id}"
    end
    alias_method :admin_post_comment_path, :admin_comment_path
1165

1166 1167
    def posts_path
      "/posts"
1168 1169
    end

1170 1171
    def post_path(post)
      "/posts/#{post.id}"
1172
    end
1173 1174 1175

    def protect_against_forgery?
      false
1176
    end
1177
end