diff options
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/validations/length.rb | 3 | ||||
-rw-r--r-- | activemodel/lib/active_model/validator.rb | 4 | ||||
-rw-r--r-- | activemodel/test/cases/validations/length_validation_test.rb | 11 |
3 files changed, 15 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index 7af6c83460..72735cfb89 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -43,7 +43,8 @@ module ActiveModel value ||= [] if key == :maximum - next if value && value.size.send(validity_check, check_value) + value_length = value.respond_to?(:length) ? value.length : value.to_s.length + next if value_length.send(validity_check, check_value) errors_options = options.except(*RESERVED_OPTIONS) errors_options[:count] = check_value diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb index 1c6123eb09..c5ed8d22d3 100644 --- a/activemodel/lib/active_model/validator.rb +++ b/activemodel/lib/active_model/validator.rb @@ -120,7 +120,7 @@ module ActiveModel #:nodoc: # Override this method in subclasses with validation logic, adding errors # to the records +errors+ array where necessary. def validate(record) - raise NotImplementedError + raise NotImplementedError, "Subclasses must implement a validate(record) method." end end @@ -156,7 +156,7 @@ module ActiveModel #:nodoc: # Override this method in subclasses with the validation logic, adding # errors to the records +errors+ array where necessary. def validate_each(record, attribute, value) - raise NotImplementedError + raise NotImplementedError, "Subclasses must implement a validate_each(record, attribute, value) method" end # Hook method that gets called by the initializer allowing verification diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb index 1e6180a938..f02514639b 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -342,6 +342,17 @@ class LengthValidationTest < ActiveModel::TestCase assert_equal ["Your essay must be at least 5 words."], t.errors[:content] end + def test_validates_length_of_for_fixnum + Topic.validates_length_of(:approved, :is => 4) + + t = Topic.new("title" => "uhohuhoh", "content" => "whatever", :approved => 1) + assert t.invalid? + assert t.errors[:approved].any? + + t = Topic.new("title" => "uhohuhoh", "content" => "whatever", :approved => 1234) + assert t.valid? + end + def test_validates_length_of_for_ruby_class Person.validates_length_of :karma, :minimum => 5 |