aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2008-03-29 17:53:44 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2008-03-29 17:53:44 +0000
commitf34d57e2d2fcc3357e7b4076d23bed3bfbd35ce3 (patch)
tree122316b519a5612362cabe6c0399604d2eab1f48
parent3748d7a0f2a2826cfb9aa8fdde5789fa62b0de34 (diff)
downloadrails-f34d57e2d2fcc3357e7b4076d23bed3bfbd35ce3.tar.gz
rails-f34d57e2d2fcc3357e7b4076d23bed3bfbd35ce3.tar.bz2
rails-f34d57e2d2fcc3357e7b4076d23bed3bfbd35ce3.zip
Fixed that validates_size_of :within works in associations (closes #11295, #10019) [cavalle]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9129 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/validations.rb12
-rwxr-xr-xactiverecord/test/cases/validations_test.rb14
3 files changed, 21 insertions, 7 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 71491db213..eb0db9d944 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fixed that validates_size_of :within works in associations #11295, #10019 [cavalle]
+
* Track changes to unsaved attributes. [Jeremy Kemper]
* Switched to UTC-timebased version numbers for migrations and the schema. This will as good as eliminate the problem of multiple migrations getting the same version assigned in different branches. Also added rake db:migrate:up/down to apply individual migrations that may need to be run when you merge branches #11458 [jbarnette]
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 7775cf93c5..c321464335 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -553,9 +553,10 @@ module ActiveRecord
too_long = options[:too_long] % option_value.end
validates_each(attrs, options) do |record, attr, value|
- if value.nil? or value.split(//).size < option_value.begin
+ value = value.split(//) if value.kind_of?(String)
+ if value.nil? or value.size < option_value.begin
record.errors.add(attr, too_short)
- elsif value.split(//).size > option_value.end
+ elsif value.size > option_value.end
record.errors.add(attr, too_long)
end
end
@@ -569,11 +570,8 @@ module ActiveRecord
message = (options[:message] || options[message_options[option]]) % option_value
validates_each(attrs, options) do |record, attr, value|
- if value.kind_of?(String)
- record.errors.add(attr, message) unless !value.nil? and value.split(//).size.method(validity_checks[option])[option_value]
- else
- record.errors.add(attr, message) unless !value.nil? and value.size.method(validity_checks[option])[option_value]
- end
+ value = value.split(//) if value.kind_of?(String)
+ record.errors.add(attr, message) unless !value.nil? and value.size.method(validity_checks[option])[option_value]
end
end
end
diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb
index 34d55dbcf6..3d83b8d4d1 100755
--- a/activerecord/test/cases/validations_test.rb
+++ b/activerecord/test/cases/validations_test.rb
@@ -806,6 +806,20 @@ class ValidationsTest < ActiveRecord::TestCase
reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain')
assert t.valid?
end
+
+ def test_validates_size_of_association_using_within
+ assert_nothing_raised { Topic.validates_size_of :replies, :within => 1..2 }
+ t = Topic.new('title' => 'noreplies', 'content' => 'whatever')
+ assert !t.save
+ assert t.errors.on(:replies)
+
+ reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain')
+ assert t.valid?
+
+ 2.times { t.replies.build('title' => 'areply', 'content' => 'whateveragain') }
+ assert !t.save
+ assert t.errors.on(:replies)
+ end
def test_validates_length_of_nasty_params
assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>6, :maximum=>9) }