Fixed that DateHelper#date_select should set the day to the 1st when its a hidden option and the month is visible (or invalid dates can be produced) [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6911 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2007-05-31 16:38:36 +00:00
parent ff5c7c8c40
commit 0cf79f07b0
2 changed files with 31 additions and 1 deletions

@ -109,6 +109,9 @@ def time_ago_in_words(from_time, include_seconds = false)
# date_select("credit_card", "bill_due", :default => { :day => 20 })
#
# The selects are prepared for multi-parameter assignment to an Active Record object.
#
# Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that all month
# choices are valid.
def date_select(object_name, method, options = {})
InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_date_select_tag(options)
end
@ -122,6 +125,9 @@ def date_select(object_name, method, options = {})
# time_select("post", "start_time", :include_seconds => true)
#
# The selects are prepared for multi-parameter assignment to an Active Record object.
#
# Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that all month
# choices are valid.
def time_select(object_name, method, options = {})
InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_time_select_tag(options)
end
@ -350,7 +356,7 @@ class InstanceTag #:nodoc:
include DateHelper
def to_date_select_tag(options = {})
date_or_time_select options.merge(:discard_hour => true)
date_or_time_select(options.merge(:discard_hour => true))
end
def to_time_select_tag(options = {})
@ -381,8 +387,15 @@ def date_or_time_select(options)
discard[:minute] = true if options[:discard_minute] or discard[:hour]
discard[:second] = true unless options[:include_seconds] && !discard[:minute]
# 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 discard[:day] && !discard[:month]
datetime = datetime.change(:day => 1)
end
# Maintain valid dates by including hidden fields for discarded elements
[:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
# Ensure proper ordering of :hour, :minute and :second
[:hour, :minute, :second].each { |o| order.delete(o); order.push(o) }

@ -784,6 +784,23 @@ def test_date_select
assert_equal expected, date_select("post", "written_on")
end
def test_date_select_without_day
@post = Post.new
@post.written_on = Date.new(2004, 6, 15)
expected = "<input type=\"hidden\" id=\"post_written_on_3i\" name=\"post[written_on(3i)]\" value=\"1\" />\n"
expected << %{<select id="post_written_on_2i" name="post[written_on(2i)]">\n}
expected << %{<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n}
expected << "</select>\n"
expected << %{<select id="post_written_on_1i" name="post[written_on(1i)]">\n}
expected << %{<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n}
expected << "</select>\n"
assert_equal expected, date_select("post", "written_on", :order => [ :month, :year ])
end
def test_date_select_within_fields_for
@post = Post.new
@post.written_on = Date.new(2004, 6, 15)