aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers
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/lib/action_view/helpers
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/lib/action_view/helpers')
-rwxr-xr-xactionpack/lib/action_view/helpers/date_helper.rb16
1 files changed, 10 insertions, 6 deletions
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