The Date Helper primarily creates select/option tags for different kinds of dates and date elements. All of the select-type methods share a number of common options that are as follows:
- :prefix - overwrites the default prefix of "date" used for the select names. So specifying "birthday" would give birthday[month] instead of date[month] if passed to the select_month method.
- :include_blank - set to true if it should be possible to set an empty date.
- :discard_type - set to true if you want to discard the type part of the select name. If set to true, the select_month method would use simply "date" (which can be overwritten using :prefix) instead of "date[month]".
- date_select
- datetime_select
- distance_of_time_in_words
- leading_zero_on_single_digits
- select_date
- select_datetime
- select_day
- select_hour
- select_html
- select_minute
- select_month
- select_year
| DEFAULT_PREFIX | = | "date" unless const_defined?("DEFAULT_PREFIX") |
Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute (identified by method) on an object assigned to the template (identified by object). Examples:
date_select("post", "written_on")
date_select("post", "written_on", :start_year => 1995)
The selects are prepared for multi-parameter assignment to an Active Record object.
[ show source ]
# File lib/action_view/helpers/date_helper.rb, line 37
37: def date_select(object, method, options = {})
38: InstanceTag.new(object, method, binding).to_date_select_tag(options)
39: end
Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based attribute (identified by method) on an object assigned to the template (identified by object). Examples:
datetime_select("post", "written_on")
datetime_select("post", "written_on", :start_year => 1995)
The selects are prepared for multi-parameter assignment to an Active Record object.
[ show source ]
# File lib/action_view/helpers/date_helper.rb, line 48
48: def datetime_select(object, method, options = {})
49: InstanceTag.new(object, method, binding).to_datetime_select_tag(options)
50: end
Reports the approximate distance in time between to Time objects. For example, if the distance is 47 minutes, it’ll return "about 1 hour". See the source for the complete wording list.
[ show source ]
# File lib/action_view/helpers/date_helper.rb, line 18
18: def distance_of_time_in_words(from_time, to_time)
19: distance_in_minutes = ((to_time - from_time) / 60).round
20:
21: case distance_in_minutes
22: when 0..45 then "#{distance_in_minutes} minutes"
23: when 46..90 then "about 1 hour"
24: when 90..1440 then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
25: when 1441..2880 then "1 day"
26: else "#{(distance_in_minutes / 1440).round} days"
27: end
28: end
Returns a set of html select-tags (one for year, month, and day) pre-selected with the date.
[ show source ]
# File lib/action_view/helpers/date_helper.rb, line 53
53: def select_date(date = Date.today, options = {})
54: select_day(date, options) + select_month(date, options) + select_year(date, options)
55: end
Returns a set of html select-tags (one for year, month, day, hour, and minute) preselected the datetime.
[ show source ]
# File lib/action_view/helpers/date_helper.rb, line 58
58: def select_datetime(datetime = Time.now, options = {})
59: select_year(datetime, options) + select_month(datetime, options) + select_day(datetime, options) +
60: select_hour(datetime, options) + select_minute(datetime, options)
61: end
Returns a select tag with options for each of the days 1 through 31 with the current day selected. The date can also be substituted for a hour number.
[ show source ]
# File lib/action_view/helpers/date_helper.rb, line 95
95: def select_day(date, options = {})
96: day_options = []
97:
98: 1.upto(31) do |day|
99: day_options << ((date.kind_of?(Fixnum) ? date : date.day) == day ?
100: "<option selected>#{day}</option>\n" :
101: "<option>#{day}</option>\n"
102: )
103: end
104:
105: select_html("day", day_options, options[:prefix], options[:include_blank], options[:discard_type])
106: end
Returns a select tag with options for each of the hours 0 through 23 with the current hour selected. The hour can also be substituted for a hour number.
[ show source ]
# File lib/action_view/helpers/date_helper.rb, line 80
80: def select_hour(datetime, options = {})
81: hour_options = []
82:
83: 0.upto(23) do |hour|
84: hour_options << ((datetime.kind_of?(Fixnum) ? datetime : datetime.hour) == hour ?
85: "<option selected>#{leading_zero_on_single_digits(hour)}</option>\n" :
86: "<option>#{leading_zero_on_single_digits(hour)}</option>\n"
87: )
88: end
89:
90: select_html("hour", hour_options, options[:prefix], options[:include_blank], options[:discard_type])
91: end
Returns a select tag with options for each of the minutes 0 through 59 with the current minute selected. The minute can also be substituted for a minute number.
[ show source ]
# File lib/action_view/helpers/date_helper.rb, line 65
65: def select_minute(datetime, options = {})
66: minute_options = []
67:
68: 0.upto(59) do |minute|
69: minute_options << ((datetime.kind_of?(Fixnum) ? datetime : datetime.min) == minute ?
70: "<option selected>#{leading_zero_on_single_digits(minute)}</option>\n" :
71: "<option>#{leading_zero_on_single_digits(minute)}</option>\n"
72: )
73: end
74:
75: select_html("minute", minute_options, options[:prefix], options[:include_blank], options[:discard_type])
76: end
Returns a select tag with options for each of the months January through December with the current month selected. The month names are presented as keys (what’s shown to the user) and the month numbers (1-12) are used as values (what’s submitted to the server). It’s also possible to use month numbers for the presentation instead of names — set the :use_month_numbers key in options to true for this to happen. The date can also be substituted for a month number. Example:
select_month(Date.today, :use_month_numbers => true)
[ show source ]
# File lib/action_view/helpers/date_helper.rb, line 115
115: def select_month(date, options = {})
116: month_options = []
117:
118: 1.upto(12) do |month_number|
119: month_name = options[:use_month_numbers] ? month_number : Date::MONTHNAMES[month_number]
120: month_options << ((date.kind_of?(Fixnum) ? date : date.month) == month_number ?
121: "<option value='#{month_number}' selected>#{month_name}</option>\n" :
122: "<option value='#{month_number}'>#{month_name}</option>\n"
123: )
124: end
125:
126: select_html("month", month_options, options[:prefix], options[:include_blank], options[:discard_type])
127: 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:
select_year(Date.today, :start_year => 1992, :end_year => 2007)
[ show source ]
# File lib/action_view/helpers/date_helper.rb, line 134
134: def select_year(date, options = {})
135: year_options = []
136: unless date.kind_of?(Fixnum) then default_start_year, default_end_year = date.year - 5, date.year + 5 end
137:
138: (options[:start_year] || default_start_year).upto(options[:end_year] || default_end_year) do |year|
139: year_options << ((date.kind_of?(Fixnum) ? date : date.year) == year ?
140: "<option selected>#{year}</option>\n" :
141: "<option>#{year}</option>\n"
142: )
143: end
144:
145: select_html("year", year_options, options[:prefix], options[:include_blank], options[:discard_type])
146: end
[ show source ]
# File lib/action_view/helpers/date_helper.rb, line 160
160: def leading_zero_on_single_digits(number)
161: number > 9 ? number : "0#{number}"
162: end
[ show source ]
# File lib/action_view/helpers/date_helper.rb, line 149
149: def select_html(type, options, prefix = nil, include_blank = false, discard_type = false)
150: select_html = "<select name='#{prefix || DEFAULT_PREFIX}"
151: select_html << "[#{type}]" unless discard_type
152: select_html << "'>\n"
153: select_html << "<option></option>\n" if include_blank
154: select_html << options.to_s
155: select_html << "</select>\n"
156:
157: return select_html
158: end