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