Lots going on
[lyrix.git] / vendor / rails / actionpack / test / template / form_helper_test.rb
bloba852f2af179f92112b773eecedc99b2409be7ddd
1 require "#{File.dirname(__FILE__)}/../abstract_unit"
3 silence_warnings do
4   Post = Struct.new(:title, :author_name, :body, :secret, :written_on, :cost)
5   Post.class_eval do
6     alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast)
7     alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast)
8     alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)
10     def new_record=(boolean)
11       @new_record = boolean
12     end
13     
14     def new_record?
15       @new_record
16     end
17   end
19   class Comment
20     attr_reader :id
21     attr_reader :post_id
22     def save; @id = 1; @post_id = 1 end
23     def new_record?; @id.nil? end
24     def name
25       @id.nil? ? 'new comment' : "comment ##{@id}"
26     end
27   end
28 end
30 class Comment::Nested < Comment; end
33 class FormHelperTest < Test::Unit::TestCase
34   include ActionView::Helpers::FormHelper
35   include ActionView::Helpers::FormTagHelper
36   include ActionView::Helpers::UrlHelper
37   include ActionView::Helpers::TagHelper
38   include ActionView::Helpers::TextHelper
39   include ActionView::Helpers::ActiveRecordHelper
40   include ActionView::Helpers::RecordIdentificationHelper
41   include ActionController::PolymorphicRoutes
43   def setup
44     @post = Post.new
45     @comment = Comment.new
46     def @post.errors() 
47       Class.new{ 
48         def on(field); "can't be empty" if field == "author_name"; end 
49         def empty?() false end 
50         def count() 1 end
51         def full_messages() [ "Author name can't be empty" ] end  
52       }.new 
53     end
54     def @post.id; 123; end
55     def @post.id_before_type_cast; 123; end
57     @post.title       = "Hello World"
58     @post.author_name = ""
59     @post.body        = "Back to the hill and over it again!"
60     @post.secret      = 1
61     @post.written_on  = Date.new(2004, 6, 15)
63     @controller = Class.new do
64       attr_reader :url_for_options
65       def url_for(options)
66         @url_for_options = options
67         "http://www.example.com"
68       end
69     end
70     @controller = @controller.new
71   end
73   def test_text_field
74     assert_dom_equal(
75       '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title")
76     )
77     assert_dom_equal(
78       '<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />', password_field("post", "title")
79     )
80     assert_dom_equal(
81       '<input id="person_name" name="person[name]" size="30" type="password" />', password_field("person", "name")
82     )
83   end
85   def test_text_field_with_escapes
86     @post.title = "<b>Hello World</b>"
87     assert_dom_equal(
88       '<input id="post_title" name="post[title]" size="30" type="text" value="&lt;b&gt;Hello World&lt;/b&gt;" />', text_field("post", "title")
89     )
90   end
92   def test_text_field_with_options
93     expected = '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />'
94     assert_dom_equal expected, text_field("post", "title", "size" => 35)
95     assert_dom_equal expected, text_field("post", "title", :size => 35)
96   end
98   def test_text_field_assuming_size
99     expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />'
100     assert_dom_equal expected, text_field("post", "title", "maxlength" => 35)
101     assert_dom_equal expected, text_field("post", "title", :maxlength => 35)
102   end
104   def test_text_field_removing_size
105     expected = '<input id="post_title" maxlength="35" name="post[title]" type="text" value="Hello World" />'
106     assert_dom_equal expected, text_field("post", "title", "maxlength" => 35, "size" => nil)
107     assert_dom_equal expected, text_field("post", "title", :maxlength => 35, :size => nil)
108   end
110   def test_text_field_doesnt_change_param_values
111     object_name = 'post[]'
112     expected = '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />'
113     assert_equal expected, text_field(object_name, "title")
114     assert_equal object_name, "post[]"
115   end
117   def test_hidden_field
118     assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Hello World" />',
119       hidden_field("post", "title")
120   end
122   def test_hidden_field_with_escapes
123     @post.title = "<b>Hello World</b>"
124     assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="&lt;b&gt;Hello World&lt;/b&gt;" />',
125       hidden_field("post", "title")
126   end
128   def test_text_field_with_options
129     assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Something Else" />',
130       hidden_field("post", "title", :value => "Something Else")
131   end
133   def test_check_box
134     assert_dom_equal(
135       '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
136       check_box("post", "secret")
137     )
138     @post.secret = 0
139     assert_dom_equal(
140       '<input id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
141       check_box("post", "secret")
142     )
143     assert_dom_equal(
144       '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
145       check_box("post", "secret" ,{"checked"=>"checked"})
146     )
147     @post.secret = true
148     assert_dom_equal(
149       '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
150       check_box("post", "secret")
151     )
152   end
154   def test_check_box_with_explicit_checked_and_unchecked_values
155     @post.secret = "on"
156     assert_dom_equal(
157       '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" /><input name="post[secret]" type="hidden" value="off" />',
158       check_box("post", "secret", {}, "on", "off")
159     )
160   end
162   def test_radio_button
163     assert_dom_equal('<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
164       radio_button("post", "title", "Hello World")
165     )
166     assert_dom_equal('<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
167       radio_button("post", "title", "Goodbye World")
168     )
169   end
171   def test_radio_button_is_checked_with_integers
172     assert_dom_equal('<input checked="checked" id="post_secret_1" name="post[secret]" type="radio" value="1" />',
173       radio_button("post", "secret", "1")
174    )
175   end
177   def test_radio_button_respects_passed_in_id
178      assert_dom_equal('<input checked="checked" id="foo" name="post[secret]" type="radio" value="1" />',
179        radio_button("post", "secret", "1", :id=>"foo")
180     )
181   end
183   def test_text_area
184     assert_dom_equal(
185       '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
186       text_area("post", "body")
187     )
188   end
190   def test_text_area_with_escapes
191     @post.body        = "Back to <i>the</i> hill and over it again!"
192     assert_dom_equal(
193       '<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>',
194       text_area("post", "body")
195     )
196   end
198   def test_text_area_with_alternate_value
199     assert_dom_equal(
200       '<textarea cols="40" id="post_body" name="post[body]" rows="20">Testing alternate values.</textarea>',
201       text_area("post", "body", :value => 'Testing alternate values.')
202     )
203   end
205   def test_text_area_with_size_option
206     assert_dom_equal(
207       '<textarea cols="183" id="post_body" name="post[body]" rows="820">Back to the hill and over it again!</textarea>',
208       text_area("post", "body", :size => "183x820")
209     )
210   end
212   def test_explicit_name
213     assert_dom_equal(
214       '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess")
215     )
216     assert_dom_equal(
217       '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
218       text_area("post", "body", "name" => "really!")
219     )
220     assert_dom_equal(
221       '<input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" /><input name="i mean it" type="hidden" value="0" />',
222       check_box("post", "secret", "name" => "i mean it")
223     )
224     assert_dom_equal text_field("post", "title", "name" => "dont guess"),
225                  text_field("post", "title", :name => "dont guess")
226     assert_dom_equal text_area("post", "body", "name" => "really!"),
227                  text_area("post", "body", :name => "really!")
228     assert_dom_equal check_box("post", "secret", "name" => "i mean it"),
229                  check_box("post", "secret", :name => "i mean it")
230   end
232   def test_explicit_id
233     assert_dom_equal(
234       '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess")
235     )
236     assert_dom_equal(
237       '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
238       text_area("post", "body", "id" => "really!")
239     )
240     assert_dom_equal(
241       '<input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
242       check_box("post", "secret", "id" => "i mean it")
243     )
244     assert_dom_equal text_field("post", "title", "id" => "dont guess"),
245                  text_field("post", "title", :id => "dont guess")
246     assert_dom_equal text_area("post", "body", "id" => "really!"),
247                  text_area("post", "body", :id => "really!")
248     assert_dom_equal check_box("post", "secret", "id" => "i mean it"),
249                  check_box("post", "secret", :id => "i mean it")
250   end
252   def test_auto_index
253     pid = @post.id
254     assert_dom_equal(
255       "<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title")
256     )
257     assert_dom_equal(
258       "<textarea cols=\"40\" id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
259       text_area("post[]", "body")
260     )
261     assert_dom_equal(
262       "<input checked=\"checked\" id=\"post_#{pid}_secret\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" /><input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" />",
263       check_box("post[]", "secret")
264     )
265    assert_dom_equal(
266 "<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />",
267       radio_button("post[]", "title", "Hello World")
268     )
269     assert_dom_equal("<input id=\"post_#{pid}_title_goodbye_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
270       radio_button("post[]", "title", "Goodbye World")
271     )
272   end
274   def test_form_for
275     _erbout = ''
277     form_for(:post, @post, :html => { :id => 'create-post' }) do |f|
278       _erbout.concat f.text_field(:title)
279       _erbout.concat f.text_area(:body)
280       _erbout.concat f.check_box(:secret)
281       _erbout.concat f.submit('Create post')
282     end
284     expected = 
285       "<form action='http://www.example.com' id='create-post' method='post'>" +
286       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
287       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
288       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
289       "<input name='post[secret]' type='hidden' value='0' />" +
290       "<input name='commit' id='post_submit' type='submit' value='Create post' />" +
291       "</form>"
293     assert_dom_equal expected, _erbout
294   end
296   def test_form_for_with_method
297     _erbout = ''
299     form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f|
300       _erbout.concat f.text_field(:title)
301       _erbout.concat f.text_area(:body)
302       _erbout.concat f.check_box(:secret)
303     end
305     expected = 
306       "<form action='http://www.example.com' id='create-post' method='post'>" +
307       "<div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>" +
308       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
309       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
310       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
311       "<input name='post[secret]' type='hidden' value='0' />" +
312       "</form>"
314     assert_dom_equal expected, _erbout
315   end
317   def test_form_for_without_object
318     _erbout = ''
320     form_for(:post, :html => { :id => 'create-post' }) do |f|
321       _erbout.concat f.text_field(:title)
322       _erbout.concat f.text_area(:body)
323       _erbout.concat f.check_box(:secret)
324     end
326     expected = 
327       "<form action='http://www.example.com' id='create-post' method='post'>" +
328       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
329       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
330       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
331       "<input name='post[secret]' type='hidden' value='0' />" +
332       "</form>"
334     assert_dom_equal expected, _erbout
335   end
337   def test_form_for_with_index
338     _erbout = ''
339     
340     form_for("post[]", @post) do |f|
341       _erbout.concat f.text_field(:title)
342       _erbout.concat f.text_area(:body)
343       _erbout.concat f.check_box(:secret)
344     end
345     
346     expected = 
347       "<form action='http://www.example.com' method='post'>" +
348       "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
349       "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
350       "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
351       "<input name='post[123][secret]' type='hidden' value='0' />" +
352       "</form>"
354     assert_dom_equal expected, _erbout
355   end
357   def test_nested_fields_for
358     _erbout = ''
359     form_for(:post, @post) do |f|
360       f.fields_for(:comment, @post) do |c|
361         _erbout.concat c.text_field(:title)
362       end
363     end
365     expected = "<form action='http://www.example.com' method='post'>" +
366                "<input name='post[comment][title]' size='30' type='text' id='post_comment_title' value='Hello World' />" +
367                "</form>"
369     assert_dom_equal expected, _erbout
370   end
372   def test_fields_for
373     _erbout = ''
375     fields_for(:post, @post) do |f|
376       _erbout.concat f.text_field(:title)
377       _erbout.concat f.text_area(:body)
378       _erbout.concat f.check_box(:secret)
379     end
381     expected = 
382       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
383       "<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]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
385       "<input name='post[secret]' type='hidden' value='0' />"
387     assert_dom_equal expected, _erbout
388   end
390   def test_fields_for_without_object
391     _erbout = ''
392     fields_for(:post) do |f|
393       _erbout.concat f.text_field(:title)
394       _erbout.concat f.text_area(:body)
395       _erbout.concat f.check_box(:secret)
396     end
398     expected = 
399       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
400       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
401       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
402       "<input name='post[secret]' type='hidden' value='0' />"
404     assert_dom_equal expected, _erbout
405   end
407   def test_fields_for_object_with_bracketed_name
408     _erbout = ''
409     fields_for("author[post]", @post) do |f|
410       _erbout.concat f.text_field(:title)
411     end
413     assert_dom_equal "<input name='author[post][title]' size='30' type='text' id='author_post_title' value='Hello World' />",
414       _erbout
415   end
417   def test_form_builder_does_not_have_form_for_method
418     assert ! ActionView::Helpers::FormBuilder.instance_methods.include?('form_for')
419   end
421   def test_form_for_and_fields_for
422     _erbout = ''
424     form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
425       _erbout.concat post_form.text_field(:title)
426       _erbout.concat post_form.text_area(:body)
428       fields_for(:parent_post, @post) do |parent_fields|
429         _erbout.concat parent_fields.check_box(:secret)
430       end
431     end
433     expected = 
434       "<form action='http://www.example.com' id='create-post' method='post'>" +
435       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
436       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
437       "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" +
438       "<input name='parent_post[secret]' type='hidden' value='0' />" +
439       "</form>"
441     assert_dom_equal expected, _erbout
442   end
444   class LabelledFormBuilder < ActionView::Helpers::FormBuilder
445     (field_helpers - %w(hidden_field)).each do |selector|
446       src = <<-END_SRC
447         def #{selector}(field, *args, &proc)
448           "<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>"
449         end
450       END_SRC
451       class_eval src, __FILE__, __LINE__
452     end
453   end
455   def test_form_for_with_labelled_builder
456     _erbout = ''
458     form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
459       _erbout.concat f.text_field(:title)
460       _erbout.concat f.text_area(:body)
461       _erbout.concat f.check_box(:secret)
462     end
464     expected = 
465       "<form action='http://www.example.com' method='post'>" +
466       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
467       "<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/>" +
468       "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
469       "<input name='post[secret]' type='hidden' value='0' /><br/>" +
470       "</form>"
472     assert_dom_equal expected, _erbout
473   end
475   def test_default_form_builder
476     old_default_form_builder, ActionView::Base.default_form_builder =
477       ActionView::Base.default_form_builder, LabelledFormBuilder
479     _erbout = ''
480     form_for(:post, @post) do |f|
481       _erbout.concat f.text_field(:title)
482       _erbout.concat f.text_area(:body)
483       _erbout.concat f.check_box(:secret)
484     end
486     expected = 
487       "<form action='http://www.example.com' method='post'>" +
488       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
489       "<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/>" +
490       "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
491       "<input name='post[secret]' type='hidden' value='0' /><br/>" +
492       "</form>"
494     assert_dom_equal expected, _erbout
495   ensure
496     ActionView::Base.default_form_builder = old_default_form_builder
497   end
499   def test_default_form_builder_with_active_record_helpers
500      
501     _erbout = '' 
502     form_for(:post, @post) do |f|
503        _erbout.concat f.error_message_on('author_name')
504        _erbout.concat f.error_messages
505     end    
506     
507     expected = %(<form action='http://www.example.com' method='post'>) + 
508                %(<div class='formError'>can't be empty</div>) + 
509                %(<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>) +
510                %(</form>)
511     
512     assert_dom_equal expected, _erbout
514   end
516   # Perhaps this test should be moved to prototype helper tests.
517   def test_remote_form_for_with_labelled_builder
518     self.extend ActionView::Helpers::PrototypeHelper
519      _erbout = ''
521      remote_form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
522        _erbout.concat f.text_field(:title)
523        _erbout.concat f.text_area(:body)
524        _erbout.concat f.check_box(:secret)
525      end
527      expected = 
528        %(<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">) +
529        "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
530        "<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/>" +
531        "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
532        "<input name='post[secret]' type='hidden' value='0' /><br/>" +
533        "</form>"
535      assert_dom_equal expected, _erbout
536   end
537    
538   def test_fields_for_with_labelled_builder
539     _erbout = ''
540     
541     fields_for(:post, @post, :builder => LabelledFormBuilder) do |f|
542       _erbout.concat f.text_field(:title)
543       _erbout.concat f.text_area(:body)
544       _erbout.concat f.check_box(:secret)
545     end
546     
547     expected = 
548       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
549       "<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/>" +
550       "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
551       "<input name='post[secret]' type='hidden' value='0' /><br/>"
552     
553     assert_dom_equal expected, _erbout
554   end
556   def test_form_for_with_html_options_adds_options_to_form_tag
557     _erbout = ''
558     
559     form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
560     expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\"></form>"
561     
562     assert_dom_equal expected, _erbout
563   end
565   def test_form_for_with_string_url_option
566     _erbout = ''
568     form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end
570     assert_equal '<form action="http://www.otherdomain.com" method="post"></form>', _erbout
571   end
573   def test_form_for_with_hash_url_option
574     _erbout = ''
576     form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end
578     assert_equal 'controller', @controller.url_for_options[:controller]
579     assert_equal 'action', @controller.url_for_options[:action]
580   end
582   def test_form_for_with_record_url_option
583     _erbout = ''
585     form_for(:post, @post, :url => @post) do |f| end
587     expected = "<form action=\"/posts/123\" method=\"post\"></form>"
588     assert_equal expected, _erbout
589   end
591   def test_form_for_with_existing_object
592     _erbout = ''
594     form_for(@post) do |f| end
596     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>"
597     assert_equal expected, _erbout
598   end
600   def test_form_for_with_new_object
601     _erbout = ''
603     post = Post.new
604     post.new_record = true
605     def post.id() nil end
607     form_for(post) do |f| end
609     expected = "<form action=\"/posts\" class=\"new_post\" id=\"new_post\" method=\"post\"></form>"
610     assert_equal expected, _erbout
611   end
613   def test_form_for_with_existing_object_in_list
614     @post.new_record = false
615     @comment.save
616     _erbout = ''
617     form_for([@post, @comment]) {}
619     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>)
620     assert_dom_equal expected, _erbout
621   end
623   def test_form_for_with_new_object_in_list
624     @post.new_record = false
625     _erbout = ''
626     form_for([@post, @comment]) {}
628     expected = %(<form action="#{comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
629     assert_dom_equal expected, _erbout
630   end
632   def test_form_for_with_existing_object_and_namespace_in_list
633     @post.new_record = false
634     @comment.save
635     _erbout = ''
636     form_for([:admin, @post, @comment]) {}
637   
638     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>)
639     assert_dom_equal expected, _erbout
640   end
641   
642   def test_form_for_with_new_object_and_namespace_in_list
643     @post.new_record = false
644     _erbout = ''
645     form_for([:admin, @post, @comment]) {}
646   
647     expected = %(<form action="#{admin_comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
648     assert_dom_equal expected, _erbout
649   end
651   def test_form_for_with_existing_object_and_custom_url
652     _erbout = ''
654     form_for(@post, :url => "/super_posts") do |f| end
656     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>"
657     assert_equal expected, _erbout
658   end
660   def test_remote_form_for_with_html_options_adds_options_to_form_tag
661     self.extend ActionView::Helpers::PrototypeHelper
662     _erbout = ''
663     
664     remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
665     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>"
666     
667     assert_dom_equal expected, _erbout
668   end
671   protected
672     def comments_path(post)
673       "/posts/#{post.id}/comments"
674     end
675     alias_method :post_comments_path, :comments_path
677     def comment_path(post, comment)
678       "/posts/#{post.id}/comments/#{comment.id}"
679     end
680     alias_method :post_comment_path, :comment_path
681     
682     def admin_comments_path(post)
683       "/admin/posts/#{post.id}/comments"
684     end
685     alias_method :admin_post_comments_path, :admin_comments_path
686     
687     def admin_comment_path(post, comment)
688       "/admin/posts/#{post.id}/comments/#{comment.id}"
689     end
690     alias_method :admin_post_comment_path, :admin_comment_path
691     
692     def posts_path
693       "/posts"
694     end 
695     
696     def post_path(post)
697       "/posts/#{post.id}"
698     end