diff options
author | Bernardo de Pádua <berpasan@gmail.com> | 2008-11-04 00:28:17 -0200 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2008-11-04 18:15:54 +0100 |
commit | b2cd318c2e3f4d19813a5c62903319a6683aa561 (patch) | |
tree | f30348da0b0661a4351a9c2d171e199db3ceee96 | |
parent | a909eecbbd42e70a5bc0e099485f07dc64db5d38 (diff) | |
download | rails-b2cd318c2e3f4d19813a5c62903319a6683aa561.tar.gz rails-b2cd318c2e3f4d19813a5c62903319a6683aa561.tar.bz2 rails-b2cd318c2e3f4d19813a5c62903319a6683aa561.zip |
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 [#1289 state:committed]
Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/date_helper.rb | 4 | ||||
-rw-r--r-- | actionpack/test/template/date_helper_test.rb | 40 |
3 files changed, 44 insertions, 2 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 17fada156d..3d1e5c13ec 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *2.2.1 [RC2 or 2.2 final]* +* 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 #1289 [Bernardo Padua/Tor Erik] + * Simplified the logging format for parameters (don't include controller, action, and format as duplicates) [DHH] * Remove the logging of the Session ID when the session store is CookieStore [DHH] diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index d4d2c6ef53..919c937444 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 1a645bccc6..49ba140c23 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 = '<input name="post[written_on(3i)]" type="hidden" id="post_written_on_3i"/>' + "\n" + expected << %{<select id="post_written_on_1i" name="post[written_on(1i)]">\n} + expected << "<option value=\"\"></option>\n" + start_year.upto(end_year) { |i| expected << %(<option value="#{i}">#{i}</option>\n) } + expected << "</select>\n" + + expected << %{<select id="post_written_on_2i" name="post[written_on(2i)]">\n} + expected << "<option value=\"\"></option>\n" + 1.upto(12) { |i| expected << %(<option value="#{i}">#{Date::MONTHNAMES[i]}</option>\n) } + expected << "</select>\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 = '<input name="post[written_on(3i)]" type="hidden" id="post_written_on_3i"/>' + "\n" + expected << %{<select id="post_written_on_1i" name="post[written_on(1i)]">\n} + expected << "<option value=\"\"></option>\n" + start_year.upto(end_year) { |i| expected << %(<option value="#{i}">#{i}</option>\n) } + expected << "</select>\n" + + expected << %{<select id="post_written_on_2i" name="post[written_on(2i)]">\n} + expected << "<option value=\"\"></option>\n" + 1.upto(12) { |i| expected << %(<option value="#{i}">#{Date::MONTHNAMES[i]}</option>\n) } + expected << "</select>\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 |