/
date_time_select.rb
35 lines (29 loc) · 1.29 KB
/
date_time_select.rb
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
module ValidatesTimeliness
module Extensions
module TimelinessDateTimeSelect
# Intercepts the date and time select helpers to reuse the values from
# the params rather than the parsed value. This allows invalid date/time
# values to be redisplayed instead of blanks to aid correction by the user.
# It's a minor usability improvement which is rarely an issue for the user.
attr_accessor :object_name, :method_name, :template_object, :options, :html_options
POSITION = {
:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6
}.freeze
def initialize(object_name, method_name, template_object, options, html_options)
@object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup
@template_object, @options, @html_options = template_object, options, html_options
end
def value(object)
return super unless @template_object.params[@object_name]
pairs = @template_object.params[@object_name].select {|k,v| k =~ /^#{@method_name}\(/ }
return super if pairs.empty?
values = {}
pairs.each_pair do |key, value|
position = key[/\((\d+)\w+\)/, 1]
values[POSITION.key(position.to_i)] = value.to_i
end
values
end
end
end
end