Permalink
Browse files

Fix date helper :include_blank regression. Closes #3811. [mhw]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5759 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 9a27b29 commit 7f32a85464feb2a5c980e53491e0de83e3666f98 @jeremy jeremy committed Dec 20, 2006
Showing with 44 additions and 1 deletion.
  1. +1 −1 actionpack/lib/action_view/helpers/date_helper.rb
  2. +43 −0 actionpack/test/template/date_helper_test.rb
@@ -361,7 +361,7 @@ def date_or_time_select(options)
defaults = { :discard_type => true }
options = defaults.merge(options)
datetime = value(object)
- datetime ||= Time.now if options[:include_blank]
+ datetime ||= Time.now unless options[:include_blank]
position = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 }
@@ -863,6 +863,49 @@ def test_date_select_with_different_order
assert_equal expected, date_select("post", "written_on", :order => [:day, :month, :year])
end
+ def test_date_select_with_nil
+ @post = Post.new
+
+ start_year = Time.now.year-5
+ end_year = Time.now.year+5
+ expected = %{<select id="post_written_on_1i" name="post[written_on(1i)]">\n}
+ start_year.upto(end_year) { |i| expected << %(<option value="#{i}"#{' selected="selected"' if i == Time.now.year}>#{i}</option>\n) }
+ expected << "</select>\n"
+
+ expected << %{<select id="post_written_on_2i" name="post[written_on(2i)]">\n}
+ 1.upto(12) { |i| expected << %(<option value="#{i}"#{' selected="selected"' if i == Time.now.month}>#{Time.now.change(:month => i).strftime("%B")}</option>\n) }
+ expected << "</select>\n"
+
+ expected << %{<select id="post_written_on_3i" name="post[written_on(3i)]">\n}
+ 1.upto(31) { |i| expected << %(<option value="#{i}"#{' selected="selected"' if i == Time.now.day}>#{i}</option>\n) }
+ expected << "</select>\n"
+
+ assert_equal expected, date_select("post", "written_on")
+ end
+
+ def test_date_select_with_nil_and_blank
+ @post = Post.new
+
+ start_year = Time.now.year-5
+ end_year = Time.now.year+5
+ 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}">#{Time.now.change(:month => i).strftime("%B")}</option>\n) }
+ expected << "</select>\n"
+
+ expected << %{<select id="post_written_on_3i" name="post[written_on(3i)]">\n}
+ expected << "<option value=\"\"></option>\n"
+ 1.upto(31) { |i| expected << %(<option value="#{i}">#{i}</option>\n) }
+ expected << "</select>\n"
+
+ assert_equal expected, date_select("post", "written_on", :include_blank => true)
+ end
+
def test_date_select_cant_override_discard_hour
@post = Post.new
@post.written_on = Date.new(2004, 6, 15)

0 comments on commit 7f32a85

Please sign in to comment.