From 75db1227149abb4198986008f241ecd4b569a90f 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 --- activemodel/lib/active_model/validations/length.rb | 6 ++++-- .../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 db0439d..32f80a9 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.method(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 4a2f72f..8cf800e 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -51,6 +51,13 @@ class LengthValidationTest < ActiveModel::TestCase assert t.errors[:title].any? 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 -- 1.5.6.4