From 5ab94b2595836fe2de36fd632ba9577c459b1292 Mon Sep 17 00:00:00 2001 From: jzw Date: Wed, 5 Aug 2009 20:17:59 -0500 Subject: [PATCH] validates_length_of with maximum should allow nil [#2309 status:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activemodel/lib/active_model/validations/length.rb | 6 ++++-- .../test/cases/validations/length_validation_test.rb | 10 +++++++--- 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 db0439d4475cc..81c97238d28ce 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -80,8 +80,10 @@ def validates_length_of(*attrs) 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 4a2f72feab21e..bc24900ecfa65 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -52,6 +52,13 @@ def test_validates_length_of_using_minimum 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 @@ def test_validates_length_of_using_maximum t.title = "" assert t.valid? - - t.title = nil - assert !t.valid? end def test_optionally_validates_length_of_using_maximum