From 949294e0f79fbc0d7fca509f7da81ebf2df89cfc Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 17 Apr 2005 06:34:13 +0000 Subject: Cleaned up validates_length_of by calling existing validations on the common stuff #1108 [caleb@aei-tech.com] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1178 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/validations.rb | 46 ++++++++++----------------- 1 file changed, 17 insertions(+), 29 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 850d012e4d..ad951b9c25 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -354,12 +354,12 @@ module ActiveRecord # Ensure that one and only one range option is specified. range_options = ALL_RANGE_OPTIONS & options.keys case range_options.size - when 0 - raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.' - when 1 - # Valid number of options; do nothing. - else - raise ArgumentError, 'Too many range options specified. Choose only one.' + when 0 + raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.' + when 1 + # Valid number of options; do nothing. + else + raise ArgumentError, 'Too many range options specified. Choose only one.' end # Get range option and value. @@ -367,33 +367,21 @@ module ActiveRecord option_value = options[range_options.first] # Declare different validations per option. - case range_options.first + + validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" } + message_options = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long } + + case option when :within, :in raise ArgumentError, ':within must be a Range' unless option_value.is_a?(Range) # ' - validates_each(attrs, options) do |record, attr| - next if record.send(attr).nil? and options[:allow_nil] - record.errors.add_on_boundary_breaking(attr, option_value, options[:too_long], options[:too_short]) - end - when :is - raise ArgumentError, ':is must be a nonnegative Integer' unless option_value.is_a?(Integer) and option_value >= 0 # ' - message = options[:message] || options[:wrong_length] - message = (message % option_value) rescue message - validates_each(attrs, options) do |record, attr, value| - record.errors.add(attr, message) if value.nil? or value.size != option_value - end - when :minimum - raise ArgumentError, ':minimum must be a nonnegative Integer' unless option_value.is_a?(Integer) and option_value >= 0 # ' - message = options[:message] || options[:too_short] - message = (message % option_value) rescue message - validates_each(attrs, options) do |record, attr, value| - record.errors.add(attr, message) if value.nil? or value.size < option_value - end - when :maximum - raise ArgumentError, ':maximum must be a nonnegative Integer' unless option_value.is_a?(Integer) and option_value >= 0 # ' - message = options[:message] || options[:too_long] + validates_length_of attrs, :minimum => option_value.begin, :allow_nil => options[:allow_nil] + validates_length_of attrs, :maximum => option_value.end, :allow_nil => options[:allow_nil] + when :is, :minimum, :maximum + raise ArgumentError, ":#{option} must be a nonnegative Integer" unless option_value.is_a?(Integer) and option_value >= 0 # ' + message = options[:message] || options[message_options[option]] message = (message % option_value) rescue message validates_each(attrs, options) do |record, attr, value| - record.errors.add(attr, message) if value.nil? or value.size > option_value + record.errors.add(attr, message) if value.nil? or !value.size.method(validity_checks[option])[option_value] end end end -- cgit v1.2.3