diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index ee9f0da9b8..be67042999 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -674,9 +674,9 @@ module ActionView @options[:discard_minute] ||= true if @options[:discard_hour] @options[:discard_second] ||= true unless @options[:include_seconds] && !@options[:discard_minute] - # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are - # valid (otherwise it could be 31 and February wouldn't be a valid date) - if @datetime && @options[:discard_day] && !@options[:discard_month] + # If the day is hidden, the day should be set to the 1st so all month and year choices are valid. Otherwise, + # February 31st or February 29th, 2011 can be selected, which are invalid. + if @datetime && @options[:discard_day] @datetime = @datetime.change(:day => 1) end @@ -701,9 +701,9 @@ module ActionView @options[:discard_month] ||= true unless order.include?(:month) @options[:discard_day] ||= true if @options[:discard_month] || !order.include?(:day) - # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are - # valid (otherwise it could be 31 and February wouldn't be a valid date) - if @datetime && @options[:discard_day] && !@options[:discard_month] + # If the day is hidden, the day should be set to the 1st so all month and year choices are valid. Otherwise, + # February 31st or February 29th, 2011 can be selected, which are invalid. + if @datetime && @options[:discard_day] @datetime = @datetime.change(:day => 1) end diff --git a/actionpack/test/template/active_model_helper_test.rb b/actionpack/test/template/active_model_helper_test.rb index 18468ee91a..24511df444 100644 --- a/actionpack/test/template/active_model_helper_test.rb +++ b/actionpack/test/template/active_model_helper_test.rb @@ -43,14 +43,14 @@ class ActiveModelHelperTest < ActionView::TestCase def test_date_select_with_errors assert_dom_equal( - %(
\n\n\n
), + %(
\n\n\n
), date_select("post", "updated_at", :discard_month => true, :discard_day => true, :start_year => 2004, :end_year => 2005) ) end def test_datetime_select_with_errors assert_dom_equal( - %(
\n\n\n\n : \n
), + %(
\n\n\n\n : \n
), datetime_select("post", "updated_at", :discard_year => true, :discard_month => true, :discard_day => true, :minute_step => 60) ) end diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 1d934b153f..f622097b57 100644 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -711,7 +711,7 @@ class DateHelperTest < ActionView::TestCase # Since the order is incomplete nothing will be shown expected = %(\n) expected << %(\n) - expected << %(\n) + expected << %(\n) assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005, :prefix => "date[first]", :order => [:day]) end @@ -943,7 +943,7 @@ class DateHelperTest < ActionView::TestCase expected << "\n" expected << %(\n) - expected << %(\n) + expected << %(\n) assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), { :date_separator => " / ", :discard_month => true, :discard_day => true, :start_year => 2003, :end_year => 2005, :prefix => "date[first]"}) end @@ -1396,6 +1396,20 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, date_select("post", "written_on", :order => [ :month, :year ]) end + def test_date_select_without_day_and_month + @post = Post.new + @post.written_on = Date.new(2004, 2, 29) + + expected = "\n" + expected << "\n" + + expected << %{\n" + + assert_dom_equal expected, date_select("post", "written_on", :order => [ :year ]) + end + def test_date_select_without_day_with_separator @post = Post.new @post.written_on = Date.new(2004, 6, 15) @@ -2427,7 +2441,7 @@ class DateHelperTest < ActionView::TestCase 1999.upto(2009) { |i| expected << %(\n) } expected << "\n" expected << %{\n} - expected << %{\n} + expected << %{\n} expected << " — " @@ -2448,7 +2462,7 @@ class DateHelperTest < ActionView::TestCase expected = %{\n} expected << %{\n} - expected << %{\n} + expected << %{\n} expected << %{\n} expected << %{\n} - expected << %{\n} + expected << %{\n} expected << %{