aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorjzw <zac@zacwilliams.com>2009-08-05 20:17:59 -0500
committerPratik Naik <pratiknaik@gmail.com>2009-08-08 19:08:13 +0100
commit5ab94b2595836fe2de36fd632ba9577c459b1292 (patch)
tree6de1b8be18c5869c5ba524606ad3227a4dcb7d36 /activemodel
parentc34d6279a06c80c51eefc3d345c24eab9a1e44fe (diff)
downloadrails-5ab94b2595836fe2de36fd632ba9577c459b1292.tar.gz
rails-5ab94b2595836fe2de36fd632ba9577c459b1292.tar.bz2
rails-5ab94b2595836fe2de36fd632ba9577c459b1292.zip
validates_length_of with maximum should allow nil [#2309 status:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/validations/length.rb6
-rw-r--r--activemodel/test/cases/validations/length_validation_test.rb10
2 files changed, 11 insertions, 5 deletions
diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb
index db0439d447..81c97238d2 100644
--- a/activemodel/lib/active_model/validations/length.rb
+++ b/activemodel/lib/active_model/validations/length.rb
@@ -80,8 +80,10 @@ module ActiveModel
validates_each(attrs, options) do |record, attr, value|
value = options[:tokenizer].call(value) if value.kind_of?(String)
- unless !value.nil? and value.size.method(validity_checks[option])[option_value]
- record.errors.add(attr, key, :default => custom_message, :count => option_value)
+ unless option == :maximum and value.nil?
+ unless !value.nil? and value.size.send(validity_checks[option], option_value)
+ record.errors.add(attr, key, :default => custom_message, :count => option_value)
+ end
end
end
end
diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb
index 4a2f72feab..bc24900ecf 100644
--- a/activemodel/test/cases/validations/length_validation_test.rb
+++ b/activemodel/test/cases/validations/length_validation_test.rb
@@ -52,6 +52,13 @@ class LengthValidationTest < ActiveModel::TestCase
assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"]
end
+ def test_validates_length_of_using_maximum_should_allow_nil
+ Topic.validates_length_of :title, :maximum => 10
+ t = Topic.create
+ puts t.errors
+ assert t.valid?
+ end
+
def test_optionally_validates_length_of_using_minimum
Topic.validates_length_of :title, :minimum => 5, :allow_nil => true
@@ -75,9 +82,6 @@ class LengthValidationTest < ActiveModel::TestCase
t.title = ""
assert t.valid?
-
- t.title = nil
- assert !t.valid?
end
def test_optionally_validates_length_of_using_maximum