Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
93898b38
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
93898b38
编写于
1月 08, 2010
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Ensure new validates works with uniqueness validator.
上级
d4589e96
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
85 addition
and
90 deletion
+85
-90
activerecord/lib/active_record/validations/associated.rb
activerecord/lib/active_record/validations/associated.rb
+1
-2
activerecord/lib/active_record/validations/uniqueness.rb
activerecord/lib/active_record/validations/uniqueness.rb
+6
-3
activerecord/test/cases/validations/association_validation_test.rb
...ord/test/cases/validations/association_validation_test.rb
+43
-55
activerecord/test/cases/validations/uniqueness_validation_test.rb
...cord/test/cases/validations/uniqueness_validation_test.rb
+35
-30
未找到文件。
activerecord/lib/active_record/validations/associated.rb
浏览文件 @
93898b38
...
...
@@ -40,8 +40,7 @@ module ClassMethods
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
def
validates_associated
(
*
attr_names
)
options
=
attr_names
.
extract_options!
validates_with
AssociatedValidator
,
options
.
merge
(
:attributes
=>
attr_names
)
validates_with
AssociatedValidator
,
_merge_attributes
(
attr_names
)
end
end
end
...
...
activerecord/lib/active_record/validations/uniqueness.rb
浏览文件 @
93898b38
...
...
@@ -2,10 +2,14 @@ module ActiveRecord
module
Validations
class
UniquenessValidator
<
ActiveModel
::
EachValidator
def
initialize
(
options
)
@klass
=
options
.
delete
(
:klass
)
super
(
options
.
reverse_merge
(
:case_sensitive
=>
true
))
end
# Unfortunately, we have to tie Uniqueness validators to a class.
def
setup
(
klass
)
@klass
=
klass
end
def
validate_each
(
record
,
attribute
,
value
)
finder_class
=
find_finder_class_for
(
record
)
table
=
finder_class
.
active_relation
...
...
@@ -170,8 +174,7 @@ module ClassMethods
# such a case.
#
def
validates_uniqueness_of
(
*
attr_names
)
options
=
attr_names
.
extract_options!
validates_with
UniquenessValidator
,
options
.
merge
(
:attributes
=>
attr_names
,
:klass
=>
self
)
validates_with
UniquenessValidator
,
_merge_attributes
(
attr_names
)
end
end
end
...
...
activerecord/test/cases/validations/association_validation_test.rb
浏览文件 @
93898b38
...
...
@@ -10,33 +10,29 @@
class
AssociationValidationTest
<
ActiveRecord
::
TestCase
fixtures
:topics
,
:owners
repair_validations
(
Topic
,
Reply
)
repair_validations
(
Topic
,
Reply
,
Owner
)
def
test_validates_size_of_association
repair_validations
(
Owner
)
do
assert_nothing_raised
{
Owner
.
validates_size_of
:pets
,
:minimum
=>
1
}
o
=
Owner
.
new
(
'name'
=>
'nopets'
)
assert
!
o
.
save
assert
o
.
errors
[
:pets
].
any?
pet
=
o
.
pets
.
build
(
'name'
=>
'apet'
)
assert
o
.
valid?
end
assert_nothing_raised
{
Owner
.
validates_size_of
:pets
,
:minimum
=>
1
}
o
=
Owner
.
new
(
'name'
=>
'nopets'
)
assert
!
o
.
save
assert
o
.
errors
[
:pets
].
any?
pet
=
o
.
pets
.
build
(
'name'
=>
'apet'
)
assert
o
.
valid?
end
def
test_validates_size_of_association_using_within
repair_validations
(
Owner
)
do
assert_nothing_raised
{
Owner
.
validates_size_of
:pets
,
:within
=>
1
..
2
}
o
=
Owner
.
new
(
'name'
=>
'nopets'
)
assert
!
o
.
save
assert
o
.
errors
[
:pets
].
any?
assert_nothing_raised
{
Owner
.
validates_size_of
:pets
,
:within
=>
1
..
2
}
o
=
Owner
.
new
(
'name'
=>
'nopets'
)
assert
!
o
.
save
assert
o
.
errors
[
:pets
].
any?
pet
=
o
.
pets
.
build
(
'name'
=>
'apet'
)
assert
o
.
valid?
pet
=
o
.
pets
.
build
(
'name'
=>
'apet'
)
assert
o
.
valid?
2
.
times
{
o
.
pets
.
build
(
'name'
=>
'apet'
)
}
assert
!
o
.
save
assert
o
.
errors
[
:pets
].
any?
end
2
.
times
{
o
.
pets
.
build
(
'name'
=>
'apet'
)
}
assert
!
o
.
save
assert
o
.
errors
[
:pets
].
any?
end
def
test_validates_associated_many
...
...
@@ -55,51 +51,43 @@ def test_validates_associated_many
end
def
test_validates_associated_one
repair_validations
(
Reply
)
do
Reply
.
validates_associated
(
:topic
)
Topic
.
validates_presence_of
(
:content
)
r
=
Reply
.
new
(
"title"
=>
"A reply"
,
"content"
=>
"with content!"
)
r
.
topic
=
Topic
.
create
(
"title"
=>
"uhohuhoh"
)
assert
!
r
.
valid?
assert
r
.
errors
[
:topic
].
any?
r
.
topic
.
content
=
"non-empty"
assert
r
.
valid?
end
Reply
.
validates
:topic
,
:associated
=>
true
Topic
.
validates_presence_of
(
:content
)
r
=
Reply
.
new
(
"title"
=>
"A reply"
,
"content"
=>
"with content!"
)
r
.
topic
=
Topic
.
create
(
"title"
=>
"uhohuhoh"
)
assert
!
r
.
valid?
assert
r
.
errors
[
:topic
].
any?
r
.
topic
.
content
=
"non-empty"
assert
r
.
valid?
end
def
test_validates_associated_with_custom_message_using_quotes
repair_validations
(
Reply
)
do
Reply
.
validates_associated
:topic
,
:message
=>
"This string contains 'single' and
\"
double
\"
quotes"
Topic
.
validates_presence_of
:content
r
=
Reply
.
create
(
"title"
=>
"A reply"
,
"content"
=>
"with content!"
)
r
.
topic
=
Topic
.
create
(
"title"
=>
"uhohuhoh"
)
assert
!
r
.
valid?
assert_equal
[
"This string contains 'single' and
\"
double
\"
quotes"
],
r
.
errors
[
:topic
]
end
Reply
.
validates_associated
:topic
,
:message
=>
"This string contains 'single' and
\"
double
\"
quotes"
Topic
.
validates_presence_of
:content
r
=
Reply
.
create
(
"title"
=>
"A reply"
,
"content"
=>
"with content!"
)
r
.
topic
=
Topic
.
create
(
"title"
=>
"uhohuhoh"
)
assert
!
r
.
valid?
assert_equal
[
"This string contains 'single' and
\"
double
\"
quotes"
],
r
.
errors
[
:topic
]
end
def
test_validates_associated_missing
repair_validations
(
Reply
)
do
Reply
.
validates_presence_of
(
:topic
)
r
=
Reply
.
create
(
"title"
=>
"A reply"
,
"content"
=>
"with content!"
)
assert
!
r
.
valid?
assert
r
.
errors
[
:topic
].
any?
Reply
.
validates_presence_of
(
:topic
)
r
=
Reply
.
create
(
"title"
=>
"A reply"
,
"content"
=>
"with content!"
)
assert
!
r
.
valid?
assert
r
.
errors
[
:topic
].
any?
r
.
topic
=
Topic
.
find
:first
assert
r
.
valid?
end
r
.
topic
=
Topic
.
find
:first
assert
r
.
valid?
end
def
test_validates_size_of_association_utf8
repair_validations
(
Owner
)
do
with_kcode
(
'UTF8'
)
do
assert_nothing_raised
{
Owner
.
validates_size_of
:pets
,
:minimum
=>
1
}
o
=
Owner
.
new
(
'name'
=>
'あいうえおかきくけこ'
)
assert
!
o
.
save
assert
o
.
errors
[
:pets
].
any?
o
.
pets
.
build
(
'name'
=>
'あいうえおかきくけこ'
)
assert
o
.
valid?
end
with_kcode
(
'UTF8'
)
do
assert_nothing_raised
{
Owner
.
validates_size_of
:pets
,
:minimum
=>
1
}
o
=
Owner
.
new
(
'name'
=>
'あいうえおかきくけこ'
)
assert
!
o
.
save
assert
o
.
errors
[
:pets
].
any?
o
.
pets
.
build
(
'name'
=>
'あいうえおかきくけこ'
)
assert
o
.
valid?
end
end
...
...
activerecord/test/cases/validations/uniqueness_validation_test.rb
浏览文件 @
93898b38
...
...
@@ -38,7 +38,7 @@ class Thaumaturgist < IneptWizard
class
UniquenessValidationTest
<
ActiveRecord
::
TestCase
fixtures
:topics
,
'warehouse-things'
,
:developers
repair_validations
(
Topic
)
repair_validations
(
Topic
,
Reply
)
def
test_validate_uniqueness
Topic
.
validates_uniqueness_of
(
:title
)
...
...
@@ -58,6 +58,15 @@ def test_validate_uniqueness
assert
t2
.
save
,
"Should now save t2 as unique"
end
def
test_validates_uniqueness_with_validates
Topic
.
validates
:title
,
:uniqueness
=>
true
t
=
Topic
.
create!
(
'title'
=>
'abc'
)
t2
=
Topic
.
new
(
'title'
=>
'abc'
)
assert
!
t2
.
valid?
assert
t2
.
errors
[
:title
]
end
def
test_validates_uniqueness_with_newline_chars
Topic
.
validates_uniqueness_of
(
:title
,
:case_sensitive
=>
false
)
...
...
@@ -66,24 +75,22 @@ def test_validates_uniqueness_with_newline_chars
end
def
test_validate_uniqueness_with_scope
repair_validations
(
Reply
)
do
Reply
.
validates_uniqueness_of
(
:content
,
:scope
=>
"parent_id"
)
Reply
.
validates_uniqueness_of
(
:content
,
:scope
=>
"parent_id"
)
t
=
Topic
.
create
(
"title"
=>
"I'm unique!"
)
t
=
Topic
.
create
(
"title"
=>
"I'm unique!"
)
r1
=
t
.
replies
.
create
"title"
=>
"r1"
,
"content"
=>
"hello world"
assert
r1
.
valid?
,
"Saving r1"
r1
=
t
.
replies
.
create
"title"
=>
"r1"
,
"content"
=>
"hello world"
assert
r1
.
valid?
,
"Saving r1"
r2
=
t
.
replies
.
create
"title"
=>
"r2"
,
"content"
=>
"hello world"
assert
!
r2
.
valid?
,
"Saving r2 first time"
r2
=
t
.
replies
.
create
"title"
=>
"r2"
,
"content"
=>
"hello world"
assert
!
r2
.
valid?
,
"Saving r2 first time"
r2
.
content
=
"something else"
assert
r2
.
save
,
"Saving r2 second time"
r2
.
content
=
"something else"
assert
r2
.
save
,
"Saving r2 second time"
t2
=
Topic
.
create
(
"title"
=>
"I'm unique too!"
)
r3
=
t2
.
replies
.
create
"title"
=>
"r3"
,
"content"
=>
"hello world"
assert
r3
.
valid?
,
"Saving r3"
end
t2
=
Topic
.
create
(
"title"
=>
"I'm unique too!"
)
r3
=
t2
.
replies
.
create
"title"
=>
"r3"
,
"content"
=>
"hello world"
assert
r3
.
valid?
,
"Saving r3"
end
def
test_validate_uniqueness_scoped_to_defining_class
...
...
@@ -102,29 +109,27 @@ def test_validate_uniqueness_scoped_to_defining_class
end
def
test_validate_uniqueness_with_scope_array
repair_validations
(
Reply
)
do
Reply
.
validates_uniqueness_of
(
:author_name
,
:scope
=>
[
:author_email_address
,
:parent_id
])
Reply
.
validates_uniqueness_of
(
:author_name
,
:scope
=>
[
:author_email_address
,
:parent_id
])
t
=
Topic
.
create
(
"title"
=>
"The earth is actually flat!"
)
t
=
Topic
.
create
(
"title"
=>
"The earth is actually flat!"
)
r1
=
t
.
replies
.
create
"author_name"
=>
"jeremy"
,
"author_email_address"
=>
"jeremy@rubyonrails.com"
,
"title"
=>
"You're crazy!"
,
"content"
=>
"Crazy reply"
assert
r1
.
valid?
,
"Saving r1"
r1
=
t
.
replies
.
create
"author_name"
=>
"jeremy"
,
"author_email_address"
=>
"jeremy@rubyonrails.com"
,
"title"
=>
"You're crazy!"
,
"content"
=>
"Crazy reply"
assert
r1
.
valid?
,
"Saving r1"
r2
=
t
.
replies
.
create
"author_name"
=>
"jeremy"
,
"author_email_address"
=>
"jeremy@rubyonrails.com"
,
"title"
=>
"You're crazy!"
,
"content"
=>
"Crazy reply again..."
assert
!
r2
.
valid?
,
"Saving r2. Double reply by same author."
r2
=
t
.
replies
.
create
"author_name"
=>
"jeremy"
,
"author_email_address"
=>
"jeremy@rubyonrails.com"
,
"title"
=>
"You're crazy!"
,
"content"
=>
"Crazy reply again..."
assert
!
r2
.
valid?
,
"Saving r2. Double reply by same author."
r2
.
author_email_address
=
"jeremy_alt_email@rubyonrails.com"
assert
r2
.
save
,
"Saving r2 the second time."
r2
.
author_email_address
=
"jeremy_alt_email@rubyonrails.com"
assert
r2
.
save
,
"Saving r2 the second time."
r3
=
t
.
replies
.
create
"author_name"
=>
"jeremy"
,
"author_email_address"
=>
"jeremy_alt_email@rubyonrails.com"
,
"title"
=>
"You're wrong"
,
"content"
=>
"It's cubic"
assert
!
r3
.
valid?
,
"Saving r3"
r3
=
t
.
replies
.
create
"author_name"
=>
"jeremy"
,
"author_email_address"
=>
"jeremy_alt_email@rubyonrails.com"
,
"title"
=>
"You're wrong"
,
"content"
=>
"It's cubic"
assert
!
r3
.
valid?
,
"Saving r3"
r3
.
author_name
=
"jj"
assert
r3
.
save
,
"Saving r3 the second time."
r3
.
author_name
=
"jj"
assert
r3
.
save
,
"Saving r3 the second time."
r3
.
author_name
=
"jeremy"
assert
!
r3
.
save
,
"Saving r3 the third time."
end
r3
.
author_name
=
"jeremy"
assert
!
r3
.
save
,
"Saving r3 the third time."
end
def
test_validate_case_insensitive_uniqueness
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录