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
This commit is contained in:
Niels Ganser 2012-02-06 12:56:38 +01:00
parent af7dafff81
commit 60dad828ae
2 changed files with 20 additions and 2 deletions

@ -29,8 +29,8 @@ def check_validity!
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

@ -357,4 +357,22 @@ def test_validates_length_of_for_ruby_class
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