diff options
author | Niels Ganser <niels@herimedia.com> | 2012-02-06 12:56:38 +0100 |
---|---|---|
committer | Niels Ganser <niels@herimedia.com> | 2012-02-06 12:58:32 +0100 |
commit | 60dad828aefa41703c0eee3863a7141c56caa7da (patch) | |
tree | cce2fbfd0eb84e92758f83cc847d287bd7792d02 /activemodel | |
parent | af7dafff818094a6e02505fe7d6d87add6c2a7e8 (diff) | |
download | rails-60dad828aefa41703c0eee3863a7141c56caa7da.tar.gz rails-60dad828aefa41703c0eee3863a7141c56caa7da.tar.bz2 rails-60dad828aefa41703c0eee3863a7141c56caa7da.zip |
To infinity… and beyond!
Allow infinite values for validates_length_of. Particularly useful
for prettily defining an open ended range such as
validates_length_of :human_stupidity, :within => 0..Float::INFINITY
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/validations/length.rb | 4 | ||||
-rw-r--r-- | activemodel/test/cases/validations/length_validation_test.rb | 18 |
2 files changed, 20 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index 0eba241333..0bbd81a984 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -29,8 +29,8 @@ module ActiveModel keys.each do |key| value = options[key] - unless value.is_a?(Integer) && value >= 0 - raise ArgumentError, ":#{key} must be a nonnegative Integer" + unless value.is_a?(Integer) && value >= 0 or value == Float::INFINITY + raise ArgumentError, ":#{key} must be a nonnegative Integer or Infinity" end end end diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb index aa86d9d959..113bfd6337 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -357,4 +357,22 @@ class LengthValidationTest < ActiveModel::TestCase ensure Person.reset_callbacks(:validate) end + + def test_validates_length_of_for_infinite_maxima + Topic.validates_length_of(:title, :within => 5..Float::INFINITY) + + t = Topic.new("title" => "1234") + assert t.invalid? + assert t.errors[:title].any? + + t.title = "12345" + assert t.valid? + + Topic.validates_length_of(:author_name, :maximum => Float::INFINITY) + + assert t.valid? + + t.author_name = "A very long author name that should still be valid." * 100 + assert t.valid? + end end |