Skip to content
This repository
Browse code

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>
  • Loading branch information...
commit b2cd318c2e3f4d19813a5c62903319a6683aa561 1 parent a909eec
Bernardo de Pádua authored November 04, 2008 dhh committed November 04, 2008
2  actionpack/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *2.2.1 [RC2 or 2.2 final]*
2 2
 
  3
+* 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]
  4
+
3 5
 * Simplified the logging format for parameters (don't include controller, action, and format as duplicates) [DHH]
4 6
 
5 7
 * Remove the logging of the Session ID when the session store is CookieStore [DHH]
4  actionpack/lib/action_view/helpers/date_helper.rb
@@ -539,7 +539,7 @@ def select_datetime
539 539
 
540 540
           # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are
541 541
           # valid (otherwise it could be 31 and february wouldn't be a valid date)
542  
-          if @options[:discard_day] && !@options[:discard_month]
  542
+          if @datetime && @options[:discard_day] && !@options[:discard_month]
543 543
             @datetime = @datetime.change(:day => 1)
544 544
           end
545 545
 
@@ -567,7 +567,7 @@ def select_date
567 567
 
568 568
           # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are
569 569
           # valid (otherwise it could be 31 and february wouldn't be a valid date)
570  
-          if @options[:discard_day] && !@options[:discard_month]
  570
+          if @datetime && @options[:discard_day] && !@options[:discard_month]
571 571
             @datetime = @datetime.change(:day => 1)
572 572
           end
573 573
         end
40  actionpack/test/template/date_helper_test.rb
@@ -1149,6 +1149,46 @@ def test_date_select_with_nil_and_blank
1149 1149
 
1150 1150
     assert_dom_equal expected, date_select("post", "written_on", :include_blank => true)
1151 1151
   end
  1152
+  
  1153
+  def test_date_select_with_nil_and_blank_and_order
  1154
+    @post = Post.new
  1155
+
  1156
+    start_year = Time.now.year-5
  1157
+    end_year   = Time.now.year+5
  1158
+    
  1159
+    expected = '<input name="post[written_on(3i)]" type="hidden" id="post_written_on_3i"/>' + "\n"
  1160
+    expected <<   %{<select id="post_written_on_1i" name="post[written_on(1i)]">\n}
  1161
+    expected << "<option value=\"\"></option>\n"
  1162
+    start_year.upto(end_year) { |i| expected << %(<option value="#{i}">#{i}</option>\n) }
  1163
+    expected << "</select>\n"
  1164
+
  1165
+    expected << %{<select id="post_written_on_2i" name="post[written_on(2i)]">\n}
  1166
+    expected << "<option value=\"\"></option>\n"
  1167
+    1.upto(12) { |i| expected << %(<option value="#{i}">#{Date::MONTHNAMES[i]}</option>\n) }
  1168
+    expected << "</select>\n"
  1169
+
  1170
+    assert_dom_equal expected, date_select("post", "written_on", :order=>[:year, :month], :include_blank=>true)
  1171
+  end
  1172
+
  1173
+  def test_date_select_with_nil_and_blank_and_order
  1174
+    @post = Post.new
  1175
+
  1176
+    start_year = Time.now.year-5
  1177
+    end_year   = Time.now.year+5
  1178
+
  1179
+    expected = '<input name="post[written_on(3i)]" type="hidden" id="post_written_on_3i"/>' + "\n"
  1180
+    expected <<   %{<select id="post_written_on_1i" name="post[written_on(1i)]">\n}
  1181
+    expected << "<option value=\"\"></option>\n"
  1182
+    start_year.upto(end_year) { |i| expected << %(<option value="#{i}">#{i}</option>\n) }
  1183
+    expected << "</select>\n"
  1184
+
  1185
+    expected << %{<select id="post_written_on_2i" name="post[written_on(2i)]">\n}
  1186
+    expected << "<option value=\"\"></option>\n"
  1187
+    1.upto(12) { |i| expected << %(<option value="#{i}">#{Date::MONTHNAMES[i]}</option>\n) }
  1188
+    expected << "</select>\n"
  1189
+
  1190
+    assert_dom_equal expected, date_select("post", "written_on", :order=>[:year, :month], :include_blank=>true)
  1191
+  end
1152 1192
 
1153 1193
   def test_date_select_cant_override_discard_hour
1154 1194
     @post = Post.new

0 notes on commit b2cd318

Please sign in to comment.
Something went wrong with that request. Please try again.