mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
added DateTimeInvalid class
rescue parse error only for DateTimeInvalid class
This commit is contained in:
parent
8e4c4098b7
commit
2284844426
@ -1,12 +1,33 @@
|
|||||||
module ValidatesTimeliness
|
module ValidatesTimeliness
|
||||||
module Validations
|
|
||||||
|
class DateTimeInvalid < StandardError; end
|
||||||
|
|
||||||
|
module Validations
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
base.extend ClassMethods
|
base.extend ClassMethods
|
||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
|
||||||
|
def timeliness_date_time_parser(time)
|
||||||
|
begin
|
||||||
|
if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
||||||
|
raw_value
|
||||||
|
else
|
||||||
|
time_array = ParseDate.parsedate(raw_value)
|
||||||
|
|
||||||
|
# checks if date is valid which enforces number of days in a month unlike Time
|
||||||
|
Date.new(*time_array[0..2])
|
||||||
|
|
||||||
|
# checks if time is valid and return object
|
||||||
|
Time.mktime(*time_array)
|
||||||
|
end
|
||||||
|
rescue
|
||||||
|
raise ValidatesTimeliness::DateTimeInvalid
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def validates_timeliness_of(*attr_names)
|
def validates_timeliness_of(*attr_names)
|
||||||
configuration = { :on => :save, :allow_nil => false, :allow_blank => false }
|
configuration = { :on => :save, :allow_nil => false, :allow_blank => false }
|
||||||
configuration.update(attr_names.extract_options!)
|
configuration.update(attr_names.extract_options!)
|
||||||
@ -26,17 +47,7 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
column = record.column_for_attribute(attr_name)
|
column = record.column_for_attribute(attr_name)
|
||||||
begin
|
begin
|
||||||
time = if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
time = timeliness_date_time_parser(raw_value)
|
||||||
raw_value
|
|
||||||
else
|
|
||||||
time_array = ParseDate.parsedate(raw_value)
|
|
||||||
|
|
||||||
# checks if date is valid which enforces number of days in a month unlike Time
|
|
||||||
Date.new(*time_array[0..2])
|
|
||||||
|
|
||||||
# checks if time is valid and return object
|
|
||||||
Time.mktime(*time_array)
|
|
||||||
end
|
|
||||||
|
|
||||||
conversion_method = column.type == :date ? :to_date : :to_time
|
conversion_method = column.type == :date ? :to_date : :to_time
|
||||||
time = time.send(conversion_method)
|
time = time.send(conversion_method)
|
||||||
@ -64,7 +75,7 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue
|
rescue ValidatesTimeliness::DateTimeInvalid
|
||||||
record.send("#{attr_name}=", nil)
|
record.send("#{attr_name}=", nil)
|
||||||
record.errors.add(attr_name, "is not a valid #{column.type}")
|
record.errors.add(attr_name, "is not a valid #{column.type}")
|
||||||
next
|
next
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user