mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9129 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
3748d7a0f2
commit
f34d57e2d2
3 changed files with 21 additions and 7 deletions
|
@ -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 @@ 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,14 +570,11 @@ 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
|
||||
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
|
||||
end
|
||||
|
||||
alias_method :validates_size_of, :validates_length_of
|
||||
|
||||
|
|
|
@ -807,6 +807,20 @@ class ValidationsTest < ActiveRecord::TestCase
|
|||
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) }
|
||||
assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :maximum=>9) }
|
||||
|
|
Loading…
Reference in a new issue