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:
parent
af7dafff81
commit
60dad828ae
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user