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
上级 3748d7a0
*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) }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册