refactored restrictions in method

This commit is contained in:
Adam Meehan 2008-07-07 15:19:32 +10:00
parent bb0e64857a
commit 3f286b9aee

View File

@ -23,9 +23,13 @@ module ValidatesTimeliness
# Override this method to use any date parsing algorithm you like such as
# Chronic. Just return nil for an invalid value and a Time object for a
# valid parsed value.
#
# Remember, if the date portion is pre the Unix epoch the return object
# will need to be a datatime. But luckily Rails, since version 2, will
# automatically handle the fall back to a DateTime when you create a time
# with Time.new or #to_time.
def timeliness_date_time_parse(raw_value)
begin
time_array = ParseDate.parsedate(raw_value)
time_array = ParseDate.parsedate(raw_value, true)
# checks if date part is valid, enforcing days in a month unlike Time
Date.new(*time_array[0..2])
@ -35,7 +39,6 @@ module ValidatesTimeliness
rescue
nil
end
end
def validates_timeliness_of(*attr_names)
configuration = { :on => :save, :type => :time, :allow_nil => false, :allow_blank => false }
@ -105,13 +108,11 @@ module ValidatesTimeliness
time = value.send(conversion_method)
restriction_methods.each do |option, method|
if restriction = configuration[option]
next unless restriction = configuration[option]
begin
compare = case restriction
when Date
when Time, Date, DateTime
restriction
when Time, DateTime
restriction.respond_to?(:in_time_zone) ? restriction.in_time_zone : restriction
when Symbol
record.send(restriction)
when Proc
@ -121,15 +122,14 @@ module ValidatesTimeliness
end
next if compare.nil?
compare = compare.send(conversion_method) if compare
compare = compare.send(conversion_method)
record.errors.add(attr_name, configuration["#{option}_message".to_sym] % compare) unless time.send(method, compare)
rescue
record.errors.add(attr_name, "restriction '#{option}' value was invalid")
end
end
end
end
def timeliness_default_error_messages
defaults = ActiveRecord::Errors.default_error_messages.slice(:blank, :invalid_date, :before, :on_or_before, :after, :on_or_after)