refactored restriction validation conversion_method case statement into restriction_type_cast_method method

This commit is contained in:
Adam Meehan 2008-08-03 14:28:02 +10:00
parent a666c35ccd
commit dff45bc6ec

View File

@ -122,21 +122,25 @@ module ValidatesTimeliness
end end
end end
def restriction_type_cast_method(type)
case type
when :time then :to_dummy_time
when :date then :to_date
when :datetime then :to_time
end
end
# Validate value against the temporal restrictions. Restriction values # Validate value against the temporal restrictions. Restriction values
# maybe of mixed type, so they are evaluated as a common type, which may # maybe of mixed type, so they are evaluated as a common type, which may
# require conversion. The type used is defined by validation type. # require conversion. The type used is defined by validation type.
def validate_timeliness_restrictions(record, attr_name, value, configuration) def validate_timeliness_restrictions(record, attr_name, value, configuration)
restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='} restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='}
conversion_method = case configuration[:type] type_cast_method = restriction_type_cast_method(configuration[:type])
when :time then :to_dummy_time
when :date then :to_date
when :datetime then :to_time
end
display = ActiveRecord::Errors.date_time_error_value_formats[configuration[:type]] display = ActiveRecord::Errors.date_time_error_value_formats[configuration[:type]]
value = value.send(conversion_method) value = value.send(type_cast_method)
restriction_methods.each do |option, method| restriction_methods.each do |option, method|
next unless restriction = configuration[option] next unless restriction = configuration[option]
@ -145,7 +149,7 @@ module ValidatesTimeliness
next if compare.nil? next if compare.nil?
compare = compare.send(conversion_method) compare = compare.send(type_cast_method)
record.errors.add(attr_name, configuration["#{option}_message".to_sym] % compare.strftime(display)) unless value.send(method, compare) record.errors.add(attr_name, configuration["#{option}_message".to_sym] % compare.strftime(display)) unless value.send(method, compare)
rescue rescue
record.errors.add(attr_name, "restriction '#{option}' value was invalid") unless self.ignore_datetime_restriction_errors record.errors.add(attr_name, "restriction '#{option}' value was invalid") unless self.ignore_datetime_restriction_errors