Skip to content

Commit

Permalink
Fix regression bug that made date_select and datetime_select raise a …
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
bernardo authored and dhh committed Nov 4, 2008
1 parent a909eec commit b2cd318
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 2 additions & 0 deletions 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]
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/helpers/date_helper.rb
Expand Up @@ -539,7 +539,7 @@ def select_datetime

# 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

Expand Down Expand Up @@ -567,7 +567,7 @@ def select_date

# 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
Expand Down
40 changes: 40 additions & 0 deletions actionpack/test/template/date_helper_test.rb
Expand Up @@ -1149,6 +1149,46 @@ def test_date_select_with_nil_and_blank

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
Expand Down

0 comments on commit b2cd318

Please sign in to comment.