From 057c2fecac7ef814cd902a3cfd98bb77342f65ef Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Bernardo=20de=20P=C3=A1dua?= Date: Tue, 4 Nov 2008 00:28:17 -0200 Subject: [PATCH] Fix regression bug that made date_select and datetime_select raise a Null Pointer Exception when a nil date/datetime was passed and only month and year were displayed. --- actionpack/lib/action_view/helpers/date_helper.rb | 4 +- actionpack/test/template/date_helper_test.rb | 40 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index d4d2c6e..919c937 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -539,7 +539,7 @@ module ActionView # 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 @options[:discard_day] && !@options[:discard_month] + if @datetime && @options[:discard_day] && !@options[:discard_month] @datetime = @datetime.change(:day => 1) end @@ -567,7 +567,7 @@ module ActionView # 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 @options[:discard_day] && !@options[:discard_month] + if @datetime && @options[:discard_day] && !@options[:discard_month] @datetime = @datetime.change(:day => 1) end end diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 1a645bc..49ba140 100644 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1149,6 +1149,46 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, date_select("post", "written_on", :include_blank => true) end + + def test_date_select_with_nil_and_blank_and_order + @post = Post.new + + start_year = Time.now.year-5 + end_year = Time.now.year+5 + + expected = '' + "\n" + expected << %{\n" + + expected << %{\n" + + assert_dom_equal expected, date_select("post", "written_on", :order=>[:year, :month], :include_blank=>true) + end + + def test_date_select_with_nil_and_blank_and_order + @post = Post.new + + start_year = Time.now.year-5 + end_year = Time.now.year+5 + + expected = '' + "\n" + expected << %{\n" + + expected << %{\n" + + assert_dom_equal expected, date_select("post", "written_on", :order=>[:year, :month], :include_blank=>true) + end def test_date_select_cant_override_discard_hour @post = Post.new -- 1.5.6.3