From 202d5c0751ac9efde2235102fb2099f4cbe93fd6 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 19 May 2005 17:40:02 +0000 Subject: 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 --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_view/helpers/date_helper.rb | 16 ++++++++++------ actionpack/test/template/date_helper_test.rb | 9 +++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) (limited to 'actionpack') 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 :start_year and :end_year keys in the +options+. The date can also be substituted - # for a year given as a number. Example: + # can be changed using the :start_year and :end_year keys in the +options+. Both ascending and descending year + # lists are supported by making :start_year less than or greater than :end_year. The date 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) ? "\n" : "\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 = %(\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 = %(