aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-05-19 17:40:02 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-05-19 17:40:02 +0000
commit202d5c0751ac9efde2235102fb2099f4cbe93fd6 (patch)
treef201bb35e9d8b3e175387fdafbccaf7f7017411f /actionpack
parentf9103e1fe20e66315f4ea73e14df8850eec2f8e3 (diff)
downloadrails-202d5c0751ac9efde2235102fb2099f4cbe93fd6.tar.gz
rails-202d5c0751ac9efde2235102fb2099f4cbe93fd6.tar.bz2
rails-202d5c0751ac9efde2235102fb2099f4cbe93fd6.zip
Added support for descending year values in DateHelper#select_year, like select_year(Date.today, :start_year => 2005, :end_year => 1900), which would count down from 2005 to 1900 instead of the other way #1274 [nwoods@mail.com]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1320 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG2
-rwxr-xr-xactionpack/lib/action_view/helpers/date_helper.rb16
-rwxr-xr-xactionpack/test/template/date_helper_test.rb9
3 files changed, 21 insertions, 6 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 80e7c72bfb..28912cba3e 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added support for descending year values in DateHelper#select_year, like select_year(Date.today, :start_year => 2005, :end_year => 1900), which would count down from 2005 to 1900 instead of the other way #1274 [nwoods@mail.com]
+
* Fixed that FormHelper#checkbox should return a checked checkbox if the value is the same as checked_value #1286 [Florian Weber]
* Fixed Form.disable in Prototype #1317 [Wintermute]
diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb
index 9b9fcae5aa..53bba0b217 100755
--- a/actionpack/lib/action_view/helpers/date_helper.rb
+++ b/actionpack/lib/action_view/helpers/date_helper.rb
@@ -190,16 +190,20 @@ module ActionView
end
# Returns a select tag with options for each of the five years on each side of the current, which is selected. The five year radius
- # can be changed using the <tt>:start_year</tt> and <tt>:end_year</tt> keys in the +options+. The <tt>date</tt> can also be substituted
- # for a year given as a number. Example:
+ # can be changed using the <tt>:start_year</tt> and <tt>:end_year</tt> keys in the +options+. Both ascending and descending year
+ # lists are supported by making <tt>:start_year</tt> less than or greater than <tt>:end_year</tt>. The <tt>date</tt> can also be
+ # substituted for a year given as a number. Example:
#
- # select_year(Date.today, :start_year => 1992, :end_year => 2007)
+ # select_year(Date.today, :start_year => 1992, :end_year => 2007) # ascending year values
+ # select_year(Date.today, :start_year => 2005, :end_year => 1900) # descending year values
def select_year(date, options = {})
year_options = []
y = date ? (date.kind_of?(Fixnum) ? (y = (date == 0) ? Date.today.year : date) : date.year) : Date.today.year
- default_start_year, default_end_year = y-5, y+5
- (options[:start_year] || default_start_year).upto(options[:end_year] || default_end_year) do |year|
+ start_year, end_year = (options[:start_year] || y-5), (options[:end_year] || y+5)
+ step_val = start_year < end_year ? 1 : -1
+
+ start_year.step(end_year, step_val) do |year|
year_options << ((date && (date.kind_of?(Fixnum) ? date : date.year) == year) ?
"<option value=\"#{year}\" selected=\"selected\">#{year}</option>\n" :
"<option value=\"#{year}\">#{year}</option>\n"
@@ -269,4 +273,4 @@ module ActionView
end
end
end
-end \ No newline at end of file
+end
diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb
index d33ce160d8..43fccbdbdf 100755
--- a/actionpack/test/template/date_helper_test.rb
+++ b/actionpack/test/template/date_helper_test.rb
@@ -124,6 +124,15 @@ class DateHelperTest < Test::Unit::TestCase
2003, :prefix => "date_year", :discard_type => true, :start_year => 2003, :end_year => 2005)
end
+ def test_select_year_descending
+ expected = %(<select name="date[year]">\n)
+ expected << %(<option value="2005" selected="selected">2005</option>\n<option value="2004">2004</option>\n<option value="2003">2003</option>\n)
+ expected << "</select>\n"
+
+ assert_equal expected, select_year(Time.mktime(2005, 8, 16), :start_year => 2005, :end_year => 2003)
+ assert_equal expected, select_year(2005, :start_year => 2005, :end_year => 2003)
+ end
+
def test_select_hour
expected = %(<select name="date[hour]">\n)
expected << %(<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08" selected="selected">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n)