reverted parser method as not working with raised exception

This commit is contained in:
Adam Meehan 2008-05-14 09:42:34 +10:00
parent 2284844426
commit b019f87625

View File

@ -10,24 +10,6 @@ module ValidatesTimeliness
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!)
@ -47,7 +29,17 @@ module ValidatesTimeliness
column = record.column_for_attribute(attr_name) column = record.column_for_attribute(attr_name)
begin begin
time = timeliness_date_time_parser(raw_value) time = 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
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)
@ -75,7 +67,7 @@ module ValidatesTimeliness
end end
end end
end end
rescue ValidatesTimeliness::DateTimeInvalid rescue
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