mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 15:22:58 +00:00
refactored action view extension to use params
this is instead of value before type to avoid issues with partial value parsing will be extracted in v3
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
# TODO remove this from the plugin for v3.
|
||||
module ValidatesTimeliness
|
||||
|
||||
def self.enable_datetime_select_invalid_value_extension!
|
||||
@@ -6,13 +7,10 @@ module ValidatesTimeliness
|
||||
|
||||
module ActionView
|
||||
|
||||
# Intercepts the date and time select helpers to allow the
|
||||
# attribute value before type cast to be used as in the select helpers.
|
||||
# This means that an invalid date or time will be redisplayed rather than the
|
||||
# type cast value which would be nil if invalid.
|
||||
#
|
||||
# Its a minor user experience improvement to be able to see original value
|
||||
# entered to aid correction.
|
||||
# Intercepts the date and time select helpers to reuse the values from the
|
||||
# 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.
|
||||
# Its a minor usability improvement which is rarely an issue for the user.
|
||||
#
|
||||
module InstanceTag
|
||||
|
||||
@@ -33,19 +31,19 @@ module ValidatesTimeliness
|
||||
end
|
||||
|
||||
def value_with_timeliness(object)
|
||||
return value_without_timeliness(object) unless @timeliness_date_or_time_tag
|
||||
|
||||
raw_value = value_before_type_cast(object)
|
||||
|
||||
if raw_value.nil? || raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
||||
unless @timeliness_date_or_time_tag && @template_object.params[@object_name]
|
||||
return value_without_timeliness(object)
|
||||
end
|
||||
|
||||
date, time = raw_value.split(' ')
|
||||
date_array = date.split('-')
|
||||
time_array = time.split(':')
|
||||
pairs = @template_object.params[@object_name].select {|k,v| k =~ /^#{@method_name}\(/ }
|
||||
return value_without_timeliness(object) if pairs.empty?
|
||||
|
||||
TimelinessDateTime.new(*(date_array + time_array).map {|v| v.blank? ? nil : v.to_i})
|
||||
values = pairs.map do |(param, value)|
|
||||
position = param.scan(/\(([0-9]*).*\)/).first.first
|
||||
[position, value]
|
||||
end.sort {|a,b| a[0] <=> b[0] }.map {|v| v[1] }
|
||||
|
||||
TimelinessDateTime.new(*values)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user