aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-04-17 06:34:13 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-04-17 06:34:13 +0000
commit949294e0f79fbc0d7fca509f7da81ebf2df89cfc (patch)
tree6769776a7630b0f6fd933c9767caf0db86c67623 /activerecord
parent5e9dc49744bc16e7d6c591ee6837eae26c6aa12f (diff)
downloadrails-949294e0f79fbc0d7fca509f7da81ebf2df89cfc.tar.gz
rails-949294e0f79fbc0d7fca509f7da81ebf2df89cfc.tar.bz2
rails-949294e0f79fbc0d7fca509f7da81ebf2df89cfc.zip
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
Diffstat (limited to 'activerecord')
-rwxr-xr-xactiverecord/lib/active_record/validations.rb46
1 files changed, 17 insertions, 29 deletions
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