added type to parse method for dummy time check mainly

added default datetime type to call to parse from strict type cast
This commit is contained in:
Adam Meehan 2008-07-08 10:42:02 +10:00
parent 3c934efd0d
commit ef68b3d0f9
2 changed files with 15 additions and 12 deletions

View File

@ -35,7 +35,7 @@ module ValidatesTimeliness
# Does strict time type cast checking for Rails 2.1 timezone handling # Does strict time type cast checking for Rails 2.1 timezone handling
def strict_time_type_cast(time) def strict_time_type_cast(time)
time = self.class.timeliness_date_time_parse(time) time = self.class.timeliness_date_time_parse(time, :datetime)
time_in_time_zone(time) time_in_time_zone(time)
end end

View File

@ -28,15 +28,18 @@ module ValidatesTimeliness
# will need to be a datetime. But luckily Rails, since version 2, will # will need to be a datetime. But luckily Rails, since version 2, will
# automatically handle the fall back to a DateTime when you create a time # automatically handle the fall back to a DateTime when you create a time
# which is out of range. # which is out of range.
def timeliness_date_time_parse(raw_value) def timeliness_date_time_parse(raw_value, type)
return raw_value.to_time if raw_value.acts_like?(:time) || raw_value.is_a?(Date) return raw_value.to_time if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
time_array = ParseDate.parsedate(raw_value, true) time_array = ParseDate.parsedate(raw_value, true)
# checks if date part is valid, enforcing days in a month unlike Time if type == :time
Date.new(*time_array[0..2]) # Rails dummy time date part is 2000-01-01
time_array[0..2] = 2000, 1, 1
# checks if time part is valid and returns object else
# Date.new enforces days per month, unlike Time
Date.new(*time_array[0..2])
end
Time.mktime(*time_array) Time.mktime(*time_array)
rescue rescue
nil nil
@ -47,7 +50,7 @@ module ValidatesTimeliness
configuration.update(timeliness_default_error_messages) configuration.update(timeliness_default_error_messages)
configuration.update(attr_names.extract_options!) configuration.update(attr_names.extract_options!)
# we need to check raw value for blank or nil in cases when an invalid value returns nil # we need to check raw value for blank or nil
allow_nil = configuration.delete(:allow_nil) allow_nil = configuration.delete(:allow_nil)
allow_blank = configuration.delete(:allow_blank) allow_blank = configuration.delete(:allow_blank)
@ -60,14 +63,14 @@ module ValidatesTimeliness
column = record.column_for_attribute(attr_name) column = record.column_for_attribute(attr_name)
begin begin
unless time = timeliness_date_time_parse(raw_value) unless time = timeliness_date_time_parse(raw_value, configuration[:type])
record.send("#{attr_name}=", nil) record.send("#{attr_name}=", nil)
record.errors.add(attr_name, configuration[:invalid_date_message] % configuration[:type]) record.errors.add(attr_name, configuration[:invalid_date_message] % configuration[:type])
next next
end end
validate_timeliness_restrictions(record, attr_name, time, configuration) validate_timeliness_restrictions(record, attr_name, time, configuration)
rescue rescue Exception => e
record.send("#{attr_name}=", nil) record.send("#{attr_name}=", nil)
record.errors.add(attr_name, configuration[:invalid_date_message] % configuration[:type]) record.errors.add(attr_name, configuration[:invalid_date_message] % configuration[:type])
next next
@ -108,7 +111,7 @@ module ValidatesTimeliness
when :datetime then :to_time when :datetime then :to_time
end end
time = value.send(conversion_method) value = value.send(conversion_method)
restriction_methods.each do |option, method| restriction_methods.each do |option, method|
next unless restriction = configuration[option] next unless restriction = configuration[option]
@ -121,13 +124,13 @@ module ValidatesTimeliness
when Proc when Proc
restriction.call(record) restriction.call(record)
else else
timeliness_date_time_parse(restriction) timeliness_date_time_parse(restriction, configuration[:type])
end end
next if compare.nil? next if compare.nil?
compare = compare.send(conversion_method) compare = compare.send(conversion_method)
record.errors.add(attr_name, configuration["#{option}_message".to_sym] % compare) unless time.send(method, compare) record.errors.add(attr_name, configuration["#{option}_message".to_sym] % compare) unless value.send(method, compare)
rescue rescue
record.errors.add(attr_name, "restriction '#{option}' value was invalid") record.errors.add(attr_name, "restriction '#{option}' value was invalid")
end end