mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
using date and time string convert methods from validates_date_time plugin which handles different array lengths
This commit is contained in:
parent
55333d6ca3
commit
69ac9e678d
@ -4,17 +4,6 @@ module ValidatesTimeliness
|
||||
def self.included(base)
|
||||
base.alias_method_chain :execute_callstack_for_multiparameter_attributes, :timeliness
|
||||
end
|
||||
|
||||
def time_array_to_string(time_array, type)
|
||||
case type
|
||||
when :time
|
||||
"%02d:%02d:%02d" % time_array[3..5]
|
||||
when :date
|
||||
"%04d-%02d-%02d" % time_array[0..2]
|
||||
when :datetime
|
||||
"%04d-%02d-%02d %02d:%02d:%02d" % time_array
|
||||
end
|
||||
end
|
||||
|
||||
# Overrides AR method to store multiparameter time and dates as string
|
||||
# allowing validation later.
|
||||
@ -28,7 +17,7 @@ module ValidatesTimeliness
|
||||
column = column_for_attribute(name)
|
||||
begin
|
||||
value = if [:date, :time, :datetime].include?(column.type)
|
||||
time_array_to_string(values, column.type)
|
||||
time_array_to_string(values, column.type)
|
||||
else
|
||||
klass.new(*values)
|
||||
end
|
||||
@ -39,10 +28,31 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
unless errors.empty?
|
||||
puts errors.inspect
|
||||
raise ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
|
||||
end
|
||||
end
|
||||
|
||||
def time_array_to_string(values, type)
|
||||
values = values.map(&:to_s)
|
||||
|
||||
case type
|
||||
when :date
|
||||
extract_date_from_multiparameter_attributes(values)
|
||||
when :time
|
||||
extract_time_from_multiparameter_attributes(values)
|
||||
when :datetime
|
||||
date_values, time_values = values.slice!(0, 3), values
|
||||
extract_date_from_multiparameter_attributes(date_values) + " " + extract_time_from_multiparameter_attributes(time_values)
|
||||
end
|
||||
end
|
||||
|
||||
def extract_date_from_multiparameter_attributes(values)
|
||||
[values[0], *values.slice(1, 2).map { |s| s.rjust(2, "0") }].join("-")
|
||||
end
|
||||
|
||||
def extract_time_from_multiparameter_attributes(values)
|
||||
values.last(3).map { |s| s.rjust(2, "0") }.join(":")
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user