1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# Copyright (c) 2005-2006 David Barri
require 'date'
module ActionView #:nodoc:
module Helpers #:nodoc:
module DateHelper
unless const_defined?(:LOCALIZED_HELPERS)
LOCALIZED_HELPERS= true
LOCALIZED_MONTHNAMES = {}
LOCALIZED_ABBR_MONTHNAMES = {}
end
# This method uses <tt>current_language</tt> to return a localized string.
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
distance_in_seconds = ((to_time - from_time).abs).round
case distance_in_minutes
when 0..1
return (distance_in_minutes==0) ? l(:actionview_datehelper_time_in_words_minute_less_than) : l(:actionview_datehelper_time_in_words_minute_single) unless include_seconds
case distance_in_seconds
when 0..5 then lwr(:actionview_datehelper_time_in_words_second_less_than, 5)
when 6..10 then lwr(:actionview_datehelper_time_in_words_second_less_than, 10)
when 11..20 then lwr(:actionview_datehelper_time_in_words_second_less_than, 20)
when 21..40 then l(:actionview_datehelper_time_in_words_minute_half)
when 41..59 then l(:actionview_datehelper_time_in_words_minute_less_than)
else l(:actionview_datehelper_time_in_words_minute)
end
when 2..45 then lwr(:actionview_datehelper_time_in_words_minute, distance_in_minutes)
when 46..90 then l(:actionview_datehelper_time_in_words_hour_about_single)
when 90..1440 then lwr(:actionview_datehelper_time_in_words_hour_about, (distance_in_minutes.to_f / 60.0).round)
when 1441..2880 then lwr(:actionview_datehelper_time_in_words_day, 1)
else lwr(:actionview_datehelper_time_in_words_day, (distance_in_minutes / 1440).round)
end
end
# This method has been modified so that a localized string can be appended to the day numbers.
def select_day(date, options = {})
day_options = []
prefix = l :actionview_datehelper_select_day_prefix
1.upto(31) do |day|
day_options << ((date && (date.kind_of?(Fixnum) ? date : date.day) == day) ?
%(<option value="#{day}" selected="selected">#{day}#{prefix}</option>\n) :
%(<option value="#{day}">#{day}#{prefix}</option>\n)
)
end
select_html(options[:field_name] || 'day', day_options, options[:prefix], options[:include_blank], options[:discard_type], options[:disabled])
end
# This method has been modified so that
# * the month names are localized.
# * it uses options: <tt>:min_date</tt>, <tt>:max_date</tt>, <tt>:start_month</tt>, <tt>:end_month</tt>
# * a localized string can be appended to the month numbers when the <tt>:use_month_numbers</tt> option is specified.
def select_month(date, options = {})
unless LOCALIZED_MONTHNAMES.has_key?(current_language)
LOCALIZED_MONTHNAMES[current_language] = [''] + l(:actionview_datehelper_select_month_names).split(',')
LOCALIZED_ABBR_MONTHNAMES[current_language] = [''] + l(:actionview_datehelper_select_month_names_abbr).split(',')
end
month_options = []
month_names = options[:use_short_month] ? LOCALIZED_ABBR_MONTHNAMES[current_language] : LOCALIZED_MONTHNAMES[current_language]
if options.has_key?(:min_date) && options.has_key?(:max_date)
if options[:min_date].year == options[:max_date].year
start_month, end_month = options[:min_date].month, options[:max_date].month
end
end
start_month = (options[:start_month] || 1) unless start_month
end_month = (options[:end_month] || 12) unless end_month
prefix = l :actionview_datehelper_select_month_prefix
start_month.upto(end_month) do |month_number|
month_name = if options[:use_month_numbers]
"#{month_number}#{prefix}"
elsif options[:add_month_numbers]
month_number.to_s + ' - ' + month_names[month_number]
else
month_names[month_number]
end
month_options << ((date && (date.kind_of?(Fixnum) ? date : date.month) == month_number) ?
%(<option value="#{month_number}" selected="selected">#{month_name}</option>\n) :
%(<option value="#{month_number}">#{month_name}</option>\n)
)
end
select_html(options[:field_name] || 'month', month_options, options[:prefix], options[:include_blank], options[:discard_type], options[:disabled])
end
# This method has been modified so that
# * it uses options: <tt>:min_date</tt>, <tt>:max_date</tt>
# * a localized string can be appended to the years numbers.
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
start_year = options.has_key?(:min_date) ? options[:min_date].year : (options[:start_year] || y-5)
end_year = options.has_key?(:max_date) ? options[:max_date].year : (options[:end_year] || y+5)
step_val = start_year < end_year ? 1 : -1
prefix = l :actionview_datehelper_select_year_prefix
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}#{prefix}</option>\n) :
%(<option value="#{year}">#{year}#{prefix}</option>\n)
)
end
select_html(options[:field_name] || 'year', year_options, options[:prefix], options[:include_blank], options[:discard_type], options[:disabled])
end
end
# The private method <tt>add_options</tt> is overridden so that "Please select" is localized.
class InstanceTag
private
def add_options(option_tags, options, value = nil)
option_tags = "<option value=\"\"></option>\n" + option_tags if options[:include_blank]
if value.blank? && options[:prompt]
("<option value=\"\">#{options[:prompt].kind_of?(String) ? options[:prompt] : l(:actionview_instancetag_blank_option)}</option>\n") + option_tags
else
option_tags
end
end
end
end
end
|