aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorSantiago Pastorino <santiago@wyeworks.com>2011-03-12 21:10:19 -0200
committerSantiago Pastorino <santiago@wyeworks.com>2011-03-12 21:12:44 -0200
commit157c37f55854a17fcb50e77b7636fab01773df3c (patch)
tree2a09e280afd4ff8a31b95e37c0198ec041a787fc /activemodel
parentf48d3d4df6c8ff6ab544f5fe384af2a0552984d7 (diff)
downloadrails-157c37f55854a17fcb50e77b7636fab01773df3c.tar.gz
rails-157c37f55854a17fcb50e77b7636fab01773df3c.tar.bz2
rails-157c37f55854a17fcb50e77b7636fab01773df3c.zip
Refactor length validation
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/validations/length.rb6
-rw-r--r--activemodel/test/cases/validations/length_validation_test.rb6
2 files changed, 6 insertions, 6 deletions
diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb
index 3e6f6e62e6..72735cfb89 100644
--- a/activemodel/lib/active_model/validations/length.rb
+++ b/activemodel/lib/active_model/validations/length.rb
@@ -42,9 +42,9 @@ module ActiveModel
next unless check_value = options[key]
value ||= [] if key == :maximum
-
- next if value.kind_of?(Fixnum) && value.to_s.size.send(validity_check, check_value)
- 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/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb
index 5e4f4f5cae..f02514639b 100644
--- a/activemodel/test/cases/validations/length_validation_test.rb
+++ b/activemodel/test/cases/validations/length_validation_test.rb
@@ -341,14 +341,14 @@ class LengthValidationTest < ActiveModel::TestCase
assert t.errors[:content].any?
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