From db6179d12f06895aa3d813c495ff77bbddf439d1 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 15 Dec 2004 12:47:50 +0000 Subject: [PATCH] Fixed DateHelper#date_select so that you can pass include_blank as an option even if you don't use start_year and end_year #59 [what-a-day] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@167 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 + .../lib/action_view/helpers/date_helper.rb | 3 +- actionpack/test/template/date_helper_test.rb | 144 ++++++++++++++++++ 3 files changed, 148 insertions(+), 1 deletion(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index c9755382537..f08e5c9a5dc 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed DateHelper#date_select so that you can pass include_blank as an option even if you don't use start_year and end_year #59 [what-a-day] + * Added that controllers will now search for a layout in $template_root/layouts/$controller_name.r(html|xml), so PostsController will look for layouts/posts.rhtml or layouts/posts.rxml and automatically configure this layout if found #307 [Marcel] diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 8af3afb034c..58dab783d56 100755 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -158,7 +158,8 @@ module ActionView # select_year(Date.today, :start_year => 1992, :end_year => 2007) def select_year(date, options = {}) year_options = [] - unless date.kind_of?(Fixnum) then default_start_year, default_end_year = date.year - 5, date.year + 5 end + y = date.kind_of?(Fixnum) ? (y = (date == 0) ? Date.today.year : date) : date.year + default_start_year, default_end_year = y-5, y+5 (options[:start_year] || default_start_year).upto(options[:end_year] || default_end_year) do |year| year_options << ((date.kind_of?(Fixnum) ? date : date.year) == year ? diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 5e9d79b26e8..30cb73da710 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -101,4 +101,148 @@ class DateHelperTest < Test::Unit::TestCase Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005, :prefix => "date[first]" ) end + + def test_select_date_with_no_start_year + expected = %(\n" + + expected << %(\n" + + expected << %(\n" + + assert_equal expected, select_date( + Time.mktime(Date.today.year, 8, 16), :end_year => Date.today.year+1, :prefix => "date[first]" + ) + end + + def test_select_date_with_no_end_year + expected = %(\n" + + expected << %(\n" + + expected << %(\n" + + assert_equal expected, select_date( + Time.mktime(2003, 8, 16), :start_year => 2003, :prefix => "date[first]" + ) + end + + def test_select_date_with_no_start_or_end_year + expected = %(\n" + + expected << %(\n" + + expected << %(\n" + + assert_equal expected, select_date( + Time.mktime(Date.today.year, 8, 16), :prefix => "date[first]" + ) + end + + def test_date_select_with_zero_value + expected = %(\n" + + expected << %(\n" + + expected << %(\n" + + assert_equal expected, select_date(0, :start_year => 2003, :end_year => 2005, :prefix => "date[first]") + end + + def test_date_select_with_zero_value_and_no_start_year + expected = %(\n" + + expected << %(\n" + + expected << %(\n" + + assert_equal expected, select_date(0, :end_year => Date.today.year+1, :prefix => "date[first]") + end + + def test_date_select_with_zero_value_and_no_end_year + expected = %(\n" + + expected << %(\n" + + expected << %(\n" + + assert_equal expected, select_date(0, :start_year => 2003, :prefix => "date[first]") + end + + def test_date_select_with_zero_value_and_no_start_and_end_year + expected = %(\n" + + expected << %(\n" + + expected << %(\n" + + assert_equal expected, select_date(0, :prefix => "date[first]") + end + end