Permalink
Browse files

Fixed that validates_size_of :within works in associations (closes #1…

…1295, #10019) [cavalle]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9129 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 3748d7a commit f34d57e2d2fcc3357e7b4076d23bed3bfbd35ce3 @dhh dhh committed Mar 29, 2008
View
@@ -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]
@@ -553,9 +553,10 @@ def validates_length_of(*attrs)
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 @@ def validates_length_of(*attrs)
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
@@ -806,6 +806,20 @@ def test_validates_size_of_association
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) }

0 comments on commit f34d57e

Please sign in to comment.