From 947cec29d5f074682052e8412904cc88a914fdbc Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 13 Nov 2005 11:13:11 +0000 Subject: 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 --- actionpack/test/template/date_helper_test.rb | 38 ++++++++++- actionpack/test/template/form_helper_test.rb | 79 ++++++++++++++++++++-- .../test/template/form_options_helper_test.rb | 60 ++++++++++++++++ actionpack/test/template/form_tag_helper_test.rb | 4 ++ 4 files changed, 175 insertions(+), 6 deletions(-) (limited to 'actionpack/test/template') 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 = "\n" + + "\n" + + "\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 = "\n\n\n — \n : \n" + + assert_dom_equal(expected, _erbout) + end + def test_date_select_with_zero_value_and_no_start_year expected = %(" + + "" + + "" + + "" + + "" + + 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 = + "" + + "" + + "" + + "" + + 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 = + "
" + + "" + + "" + + "" + + "" + + "
" + + 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 = "" + + _erbout = '' + + fields_for :post => @post do |f| + _erbout.concat f.select(:category, %w( abe hest)) + end + + assert_dom_equal( + "", + _erbout + ) + end + def test_select_with_blank @post = Post.new @post.category = "" @@ -283,6 +300,28 @@ class FormOptionsHelperTest < Test::Unit::TestCase ) end + def test_collection_select_under_fields_for + @posts = [ + Post.new(" went home", "", "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( + "", + _erbout + ) + end + def test_collection_select_with_blank_and_style @posts = [ Post.new(" went home", "", "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( + "", + _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 = %() assert_dom_equal expected, actual end + + def test_pass + assert_equal 1, 1 + end end -- cgit v1.2.3