diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-11-13 11:13:11 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-11-13 11:13:11 +0000 |
commit | 947cec29d5f074682052e8412904cc88a914fdbc (patch) | |
tree | 2888aa13f47984175aba6f83bacf6e925b5d5a60 /actionpack/test | |
parent | 4a5b042fc7805f95b4a096cafe0d57e105246095 (diff) | |
download | rails-947cec29d5f074682052e8412904cc88a914fdbc.tar.gz rails-947cec29d5f074682052e8412904cc88a914fdbc.tar.bz2 rails-947cec29d5f074682052e8412904cc88a914fdbc.zip |
Added FormHelper#form_for and FormHelper#fields_for that makes it easier to work with forms for single objects also if they don't reside in instance variables [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3003 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/abstract_unit.rb | 3 | ||||
-rwxr-xr-x | actionpack/test/template/date_helper_test.rb | 38 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 79 | ||||
-rw-r--r-- | actionpack/test/template/form_options_helper_test.rb | 60 | ||||
-rw-r--r-- | actionpack/test/template/form_tag_helper_test.rb | 4 |
5 files changed, 177 insertions, 7 deletions
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 0f8c309e06..eaf70322c3 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -1,9 +1,10 @@ $:.unshift(File.dirname(__FILE__) + '/../lib') -$:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib') +$:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib/active_support') $:.unshift(File.dirname(__FILE__) + '/fixtures/helpers') require 'test/unit' require 'action_controller' +require 'breakpoint' require 'action_controller/test_process' diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index a890baa115..bb6cd19013 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1,9 +1,13 @@ require 'test/unit' -require File.dirname(__FILE__) + '/../../lib/action_view/helpers/date_helper' require File.dirname(__FILE__) + "/../abstract_unit" class DateHelperTest < Test::Unit::TestCase include ActionView::Helpers::DateHelper + include ActionView::Helpers::FormHelper + + silence_warnings do + Post = Struct.new("Post", :written_on, :updated_at) + end def test_distance_in_words from = Time.mktime(2004, 3, 6, 21, 41, 18) @@ -497,6 +501,38 @@ class DateHelperTest < Test::Unit::TestCase assert_equal expected, select_date(0, :start_year => 2003, :end_year => 2005, :prefix => "date[first]") end + def test_date_select_within_fields_for + @post = Post.new + @post.written_on = Date.new(2004, 6, 15) + + _erbout = '' + + fields_for :post => @post do |f| + _erbout.concat f.date_select(:written_on) + end + + expected = "<select name='post[written_on(1i)]'>\n<option value='1999'>1999</option>\n<option value='2000'>2000</option>\n<option value='2001'>2001</option>\n<option value='2002'>2002</option>\n<option value='2003'>2003</option>\n<option selected='selected' value='2004'>2004</option>\n<option value='2005'>2005</option>\n<option value='2006'>2006</option>\n<option value='2007'>2007</option>\n<option value='2008'>2008</option>\n<option value='2009'>2009</option>\n</select>\n" + + "<select name='post[written_on(2i)]'>\n<option value='1'>January</option>\n<option value='2'>February</option>\n<option value='3'>March</option>\n<option value='4'>April</option>\n<option value='5'>May</option>\n<option selected='selected' value='6'>June</option>\n<option value='7'>July</option>\n<option value='8'>August</option>\n<option value='9'>September</option>\n<option value='10'>October</option>\n<option value='11'>November</option>\n<option value='12'>December</option>\n</select>\n" + + "<select name='post[written_on(3i)]'>\n<option value='1'>1</option>\n<option value='2'>2</option>\n<option value='3'>3</option>\n<option value='4'>4</option>\n<option value='5'>5</option>\n<option value='6'>6</option>\n<option value='7'>7</option>\n<option value='8'>8</option>\n<option value='9'>9</option>\n<option value='10'>10</option>\n<option value='11'>11</option>\n<option value='12'>12</option>\n<option value='13'>13</option>\n<option value='14'>14</option>\n<option selected='selected' value='15'>15</option>\n<option value='16'>16</option>\n<option value='17'>17</option>\n<option value='18'>18</option>\n<option value='19'>19</option>\n<option value='20'>20</option>\n<option value='21'>21</option>\n<option value='22'>22</option>\n<option value='23'>23</option>\n<option value='24'>24</option>\n<option value='25'>25</option>\n<option value='26'>26</option>\n<option value='27'>27</option>\n<option value='28'>28</option>\n<option value='29'>29</option>\n<option value='30'>30</option>\n<option value='31'>31</option>\n</select>\n" + + assert_dom_equal(expected, _erbout) + end + + def test_datetime_select_within_fields_for + @post = Post.new + @post.updated_at = Time.local(2004, 6, 15, 16, 35) + + _erbout = '' + + fields_for :post => @post do |f| + _erbout.concat f.datetime_select(:updated_at) + end + + expected = "<select name='post[updated_at(1i)]'>\n<option value='1999'>1999</option>\n<option value='2000'>2000</option>\n<option value='2001'>2001</option>\n<option value='2002'>2002</option>\n<option value='2003'>2003</option>\n<option selected='selected' value='2004'>2004</option>\n<option value='2005'>2005</option>\n<option value='2006'>2006</option>\n<option value='2007'>2007</option>\n<option value='2008'>2008</option>\n<option value='2009'>2009</option>\n</select>\n<select name='post[updated_at(2i)]'>\n<option value='1'>January</option>\n<option value='2'>February</option>\n<option value='3'>March</option>\n<option value='4'>April</option>\n<option value='5'>May</option>\n<option selected='selected' value='6'>June</option>\n<option value='7'>July</option>\n<option value='8'>August</option>\n<option value='9'>September</option>\n<option value='10'>October</option>\n<option value='11'>November</option>\n<option value='12'>December</option>\n</select>\n<select name='post[updated_at(3i)]'>\n<option value='1'>1</option>\n<option value='2'>2</option>\n<option value='3'>3</option>\n<option value='4'>4</option>\n<option value='5'>5</option>\n<option value='6'>6</option>\n<option value='7'>7</option>\n<option value='8'>8</option>\n<option value='9'>9</option>\n<option value='10'>10</option>\n<option value='11'>11</option>\n<option value='12'>12</option>\n<option value='13'>13</option>\n<option value='14'>14</option>\n<option selected='selected' value='15'>15</option>\n<option value='16'>16</option>\n<option value='17'>17</option>\n<option value='18'>18</option>\n<option value='19'>19</option>\n<option value='20'>20</option>\n<option value='21'>21</option>\n<option value='22'>22</option>\n<option value='23'>23</option>\n<option value='24'>24</option>\n<option value='25'>25</option>\n<option value='26'>26</option>\n<option value='27'>27</option>\n<option value='28'>28</option>\n<option value='29'>29</option>\n<option value='30'>30</option>\n<option value='31'>31</option>\n</select>\n — <select name='post[updated_at(4i)]'>\n<option value='00'>00</option>\n<option value='01'>01</option>\n<option value='02'>02</option>\n<option value='03'>03</option>\n<option value='04'>04</option>\n<option value='05'>05</option>\n<option value='06'>06</option>\n<option value='07'>07</option>\n<option value='08'>08</option>\n<option value='09'>09</option>\n<option value='10'>10</option>\n<option value='11'>11</option>\n<option value='12'>12</option>\n<option value='13'>13</option>\n<option value='14'>14</option>\n<option value='15'>15</option>\n<option selected='selected' value='16'>16</option>\n<option value='17'>17</option>\n<option value='18'>18</option>\n<option value='19'>19</option>\n<option value='20'>20</option>\n<option value='21'>21</option>\n<option value='22'>22</option>\n<option value='23'>23</option>\n</select>\n : <select name='post[updated_at(5i)]'>\n<option value='00'>00</option>\n<option value='01'>01</option>\n<option value='02'>02</option>\n<option value='03'>03</option>\n<option value='04'>04</option>\n<option value='05'>05</option>\n<option value='06'>06</option>\n<option value='07'>07</option>\n<option value='08'>08</option>\n<option value='09'>09</option>\n<option value='10'>10</option>\n<option value='11'>11</option>\n<option value='12'>12</option>\n<option value='13'>13</option>\n<option value='14'>14</option>\n<option value='15'>15</option>\n<option value='16'>16</option>\n<option value='17'>17</option>\n<option value='18'>18</option>\n<option value='19'>19</option>\n<option value='20'>20</option>\n<option value='21'>21</option>\n<option value='22'>22</option>\n<option value='23'>23</option>\n<option value='24'>24</option>\n<option value='25'>25</option>\n<option value='26'>26</option>\n<option value='27'>27</option>\n<option value='28'>28</option>\n<option value='29'>29</option>\n<option value='30'>30</option>\n<option value='31'>31</option>\n<option value='32'>32</option>\n<option value='33'>33</option>\n<option value='34'>34</option>\n<option selected='selected' value='35'>35</option>\n<option value='36'>36</option>\n<option value='37'>37</option>\n<option value='38'>38</option>\n<option value='39'>39</option>\n<option value='40'>40</option>\n<option value='41'>41</option>\n<option value='42'>42</option>\n<option value='43'>43</option>\n<option value='44'>44</option>\n<option value='45'>45</option>\n<option value='46'>46</option>\n<option value='47'>47</option>\n<option value='48'>48</option>\n<option value='49'>49</option>\n<option value='50'>50</option>\n<option value='51'>51</option>\n<option value='52'>52</option>\n<option value='53'>53</option>\n<option value='54'>54</option>\n<option value='55'>55</option>\n<option value='56'>56</option>\n<option value='57'>57</option>\n<option value='58'>58</option>\n<option value='59'>59</option>\n</select>\n" + + assert_dom_equal(expected, _erbout) + end + def test_date_select_with_zero_value_and_no_start_year expected = %(<select name="date[first][year]">\n) (Date.today.year-5).upto(Date.today.year+1) { |y| expected << %(<option value="#{y}">#{y}</option>\n) } diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 08b5421c81..ae8cc50a73 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -1,10 +1,11 @@ -require 'test/unit' -require 'erb' -require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_helper' -require File.dirname(__FILE__) + '/../../../activesupport/lib/active_support/core_ext/hash' #for stringify keys +require File.dirname(__FILE__) + '/../abstract_unit' class FormHelperTest < Test::Unit::TestCase include ActionView::Helpers::FormHelper + include ActionView::Helpers::FormTagHelper + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TagHelper + include ActionView::Helpers::TextHelper silence_warnings do Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on, :cost) @@ -27,6 +28,13 @@ class FormHelperTest < Test::Unit::TestCase @post.body = "Back to the hill and over it again!" @post.secret = 1 @post.written_on = Date.new(2004, 6, 15) + + @controller = Class.new do + def url_for(options, *parameters_for_method_reference) + "http://www.example.com" + end + end + @controller = @controller.new end def test_text_field @@ -187,4 +195,65 @@ class FormHelperTest < Test::Unit::TestCase radio_button("post[]", "title", "Goodbye World") ) end -end + + def test_form_for + _erbout = '' + + form_for(:post => @post) do |f| + _erbout.concat f.text_field(:title) + _erbout.concat f.text_area(:body) + _erbout.concat f.check_box(:secret) + 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]' checked='checked' type='checkbox' id='post_secret' value='1' />" + + "<input name='post[secret]' type='hidden' value='0' />" + + "</form>" + + assert_dom_equal expected, _erbout + end + + def test_fields_for + _erbout = '' + + fields_for(:post => @post) do |f| + _erbout.concat f.text_field(:title) + _erbout.concat f.text_area(:body) + _erbout.concat f.check_box(:secret) + 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>" + + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + + "<input name='post[secret]' type='hidden' value='0' />" + + assert_dom_equal expected, _erbout + end + + def test_form_for_and_fields_for + _erbout = '' + + form_for(:post => @post) do |post_form| + _erbout.concat post_form.text_field(:title) + _erbout.concat post_form.text_area(:body) + + fields_for(:parent_post => @post) do |parent_fields| + _erbout.concat parent_fields.check_box(:secret) + 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' />" + + "<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]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" + + "<input name='parent_post[secret]' type='hidden' value='0' />" + + "</form>" + + assert_dom_equal expected, _erbout + end +end
\ No newline at end of file diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 36d68492ec..4d22f0cfef 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -23,6 +23,7 @@ end ActionView::Helpers::FormOptionsHelper::TimeZone = MockTimeZone class FormOptionsHelperTest < Test::Unit::TestCase + include ActionView::Helpers::FormHelper include ActionView::Helpers::FormOptionsHelper silence_warnings do @@ -222,6 +223,22 @@ class FormOptionsHelperTest < Test::Unit::TestCase ) end + def test_select_under_fields_for + @post = Post.new + @post.category = "<mus>" + + _erbout = '' + + fields_for :post => @post do |f| + _erbout.concat f.select(:category, %w( abe <mus> hest)) + end + + assert_dom_equal( + "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"<mus>\" selected=\"selected\"><mus></option>\n<option value=\"hest\">hest</option></select>", + _erbout + ) + end + def test_select_with_blank @post = Post.new @post.category = "<mus>" @@ -283,6 +300,28 @@ class FormOptionsHelperTest < Test::Unit::TestCase ) end + def test_collection_select_under_fields_for + @posts = [ + Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + @post = Post.new + @post.author_name = "Babe" + + _erbout = '' + + fields_for :post => @post do |f| + _erbout.concat f.collection_select(:author_name, @posts, :author_name, :author_name) + end + + assert_dom_equal( + "<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"<Abe>\"><Abe></option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>", + _erbout + ) + end + def test_collection_select_with_blank_and_style @posts = [ Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"), @@ -324,6 +363,27 @@ class FormOptionsHelperTest < Test::Unit::TestCase html end + def test_time_zone_select_under_fields_for + @firm = Firm.new("D") + + _erbout = '' + + fields_for :firm => @firm do |f| + _erbout.concat f.time_zone_select(:time_zone) + end + + assert_dom_equal( + "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" + + "<option value=\"A\">A</option>\n" + + "<option value=\"B\">B</option>\n" + + "<option value=\"C\">C</option>\n" + + "<option value=\"D\" selected=\"selected\">D</option>\n" + + "<option value=\"E\">E</option>" + + "</select>", + _erbout + ) + end + def test_time_zone_select_with_blank @firm = Firm.new("D") html = time_zone_select("firm", "time_zone", nil, :include_blank => true) diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 546d3e6772..401d3fbadc 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -93,5 +93,9 @@ class FormTagHelperTest < Test::Unit::TestCase expected = %(<input id="admin" name="title" type="text" value="Hello!" />) assert_dom_equal expected, actual end + + def test_pass + assert_equal 1, 1 + end end |