diff options
author | Emilio Tagua <miloops@gmail.com> | 2009-08-10 10:40:11 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2009-08-10 10:40:11 -0300 |
commit | eb3ae44ccaff1dc63eb31bf86d8db07c88ddc413 (patch) | |
tree | 9997bd3739e30fad0102a22e8feee40c4c4c835c /activemodel | |
parent | 952014315926d370f2a0b681cb765948bf2e6883 (diff) | |
parent | 5786395760f1e1906c878df4023cac3741e66e87 (diff) | |
download | rails-eb3ae44ccaff1dc63eb31bf86d8db07c88ddc413.tar.gz rails-eb3ae44ccaff1dc63eb31bf86d8db07c88ddc413.tar.bz2 rails-eb3ae44ccaff1dc63eb31bf86d8db07c88ddc413.zip |
Merge commit 'rails/master'
Conflicts:
activerecord/lib/active_record/migration.rb
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/validations/length.rb | 9 | ||||
-rw-r--r-- | activemodel/test/cases/validations/length_validation_test.rb | 9 |
2 files changed, 13 insertions, 5 deletions
diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index db0439d447..3e76796355 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -80,9 +80,14 @@ 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) + + valid_value = if option == :maximum + value.nil? || value.size.send(validity_checks[option], option_value) + else + value && value.size.send(validity_checks[option], option_value) end + + record.errors.add(attr, key, :default => custom_message, :count => option_value) unless valid_value 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..499f6a5e31 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -52,6 +52,12 @@ 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 + assert t.valid? + end + def test_optionally_validates_length_of_using_minimum Topic.validates_length_of :title, :minimum => 5, :allow_nil => true @@ -75,9 +81,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 |