mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
version 2.1
This commit is contained in:
@@ -6,12 +6,12 @@ module ValidatesTimeliness
|
||||
|
||||
module ActiveRecord
|
||||
|
||||
# Overrides write method for date, time and datetime columns
|
||||
# to use plugin parser. Also adds mechanism to store value
|
||||
# before type cast.
|
||||
#
|
||||
module AttributeMethods
|
||||
|
||||
# Overrides write method for date, time and datetime columns
|
||||
# to use plugin parser. Also adds mechanism to store value
|
||||
# before type cast.
|
||||
#
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.class_eval do
|
||||
@@ -44,8 +44,9 @@ module ValidatesTimeliness
|
||||
|
||||
def define_attribute_methods_with_timeliness
|
||||
return if generated_methods?
|
||||
columns_hash.each do |name, column|
|
||||
timeliness_methods = []
|
||||
|
||||
columns_hash.each do |name, column|
|
||||
if [:date, :time, :datetime].include?(column.type)
|
||||
time_zone_aware = create_time_zone_conversion_attribute?(name, column) rescue false
|
||||
|
||||
@@ -54,10 +55,12 @@ module ValidatesTimeliness
|
||||
write_date_time_attribute('#{name}', value, #{column.type.inspect}, #{time_zone_aware})
|
||||
end
|
||||
EOV
|
||||
timeliness_methods << name
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
define_attribute_methods_without_timeliness
|
||||
@generated_methods += timeliness_methods
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -10,9 +10,9 @@ module ValidatesTimeliness
|
||||
def self.included(base)
|
||||
base.alias_method_chain :execute_callstack_for_multiparameter_attributes, :timeliness
|
||||
end
|
||||
|
||||
# Overrides AR method to store multiparameter time and dates as string
|
||||
# allowing validation later.
|
||||
|
||||
# Assign dates and times as formatted strings to force the use of the plugin parser
|
||||
# and store a before_type_cast value for attribute
|
||||
def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack)
|
||||
errors = []
|
||||
callstack.each do |name, values|
|
||||
@@ -46,18 +46,17 @@ module ValidatesTimeliness
|
||||
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)
|
||||
extract_date_from_multiparameter_attributes(values) + " " + extract_time_from_multiparameter_attributes(values)
|
||||
end
|
||||
end
|
||||
|
||||
def extract_date_from_multiparameter_attributes(values)
|
||||
[values[0], *values.slice(1, 2).map { |s| s.rjust(2, "0") }].join("-")
|
||||
year = ValidatesTimeliness::Formats.unambiguous_year(values[0].rjust(2, "0"))
|
||||
[year, *values.slice(1, 2).map { |s| s.rjust(2, "0") }].join("-")
|
||||
end
|
||||
|
||||
def extract_time_from_multiparameter_attributes(values)
|
||||
values = values.size > 3 ? values[3..5] : values
|
||||
values.map { |s| s.rjust(2, "0") }.join(":")
|
||||
values[3..5].map { |s| s.rjust(2, "0") }.join(":")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -314,7 +314,12 @@ module ValidatesTimeliness
|
||||
args = order.invert.sort.map {|p| arg_map[p[1]][1] }
|
||||
arr = [nil] * 7
|
||||
order.keys.each {|k| i = arg_map[k][0]; arr[i] = arg_map[k][2] unless i.nil? }
|
||||
proc_string = "lambda {|#{args.join(',')}| md||=nil; [#{arr.map {|i| i.nil? ? 'nil' : i }.join(',')}].map {|i| i.is_a?(Float) ? i : i.to_i } }"
|
||||
proc_string = <<-EOL
|
||||
lambda {|#{args.join(',')}|
|
||||
md ||= nil
|
||||
[#{arr.map {|i| i.nil? ? 'nil' : i }.join(',')}].map {|i| i.is_a?(Float) ? i : i.to_i }
|
||||
}
|
||||
EOL
|
||||
eval proc_string
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user