Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
5c245b91
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,发现更多精彩内容 >>
提交
5c245b91
编写于
5月 10, 2010
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make sure valid? preceives the context as in ActiveModel API (ht: Carlos Antonio)
上级
ce5827ea
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
45 addition
and
26 deletion
+45
-26
activerecord/lib/active_record/attribute_methods/primary_key.rb
...record/lib/active_record/attribute_methods/primary_key.rb
+1
-1
activerecord/lib/active_record/validations.rb
activerecord/lib/active_record/validations.rb
+22
-25
activerecord/test/cases/validations_test.rb
activerecord/test/cases/validations_test.rb
+17
-0
activerecord/test/models/reply.rb
activerecord/test/models/reply.rb
+5
-0
未找到文件。
activerecord/lib/active_record/attribute_methods/primary_key.rb
浏览文件 @
5c245b91
...
...
@@ -6,7 +6,7 @@ module PrimaryKey
# Returns this record's primary key value wrapped in an Array
# or nil if the record is a new_record?
def
to_key
new_record?
?
nil
:
[
send
(
self
.
class
.
primary_key
)
]
new_record?
?
nil
:
[
id
]
end
module
ClassMethods
...
...
activerecord/lib/active_record/validations.rb
浏览文件 @
5c245b91
...
...
@@ -36,8 +36,8 @@ def create!(attributes = nil, &block)
# The validation process on save can be skipped by passing false. The regular Base#save method is
# replaced with this when the validations module is mixed in, which it is by default.
def
save
(
options
=
nil
)
return
super
if
valid?
(
options
)
def
save
(
options
=
{}
)
return
super
if
perform_validations
(
options
)
false
end
...
...
@@ -47,38 +47,35 @@ def save_without_validation!
# Attempts to save the record just like Base#save but will raise a RecordInvalid exception instead of returning false
# if the record is not valid.
def
save!
(
options
=
nil
)
return
super
if
valid?
(
options
)
def
save!
(
options
=
{}
)
return
super
if
perform_validations
(
options
)
raise
RecordInvalid
.
new
(
self
)
end
# Runs all the specified validations and returns true if no errors were added otherwise false.
def
valid?
(
options
=
nil
)
perform_validation
=
case
options
when
NilClass
true
when
Hash
options
[
:validate
]
!=
false
else
ActiveSupport
::
Deprecation
.
warn
"save(
#{
options
}
) is deprecated, please give save(:validate =>
#{
options
}
) instead"
,
caller
options
end
def
valid?
(
context
=
nil
)
context
||=
(
new_record?
?
:create
:
:update
)
super
(
context
)
if
perform_validation
errors
.
clear
deprecated_callback_method
(
:validate
)
deprecated_callback_method
(
:"validate_on_
#{
context
}
"
)
self
.
validation_context
=
new_record?
?
:create
:
:update
_run_validate_callbacks
errors
.
empty?
end
deprecated_callback_method
(
:validate
)
protected
if
new_record?
deprecated_callback_method
(
:validate_on_create
)
else
deprecated_callback_method
(
:validate_on_update
)
end
def
perform_validations
(
options
=
{})
perform_validation
=
case
options
when
Hash
options
[
:validate
]
!=
false
else
ActiveSupport
::
Deprecation
.
warn
"save(
#{
options
}
) is deprecated, please give save(:validate =>
#{
options
}
) instead"
,
caller
options
end
errors
.
empty?
if
perform_validation
valid?
(
options
.
is_a?
(
Hash
)
?
options
[
:context
]
:
nil
)
else
true
end
...
...
activerecord/test/cases/validations_test.rb
浏览文件 @
5c245b91
...
...
@@ -62,6 +62,23 @@ def test_error_on_update
assert_equal
[
"is Wrong Update"
],
r
.
errors
[
:title
],
"A reply with a bad content should contain an error"
end
def
test_error_on_given_context
r
=
WrongReply
.
new
assert
!
r
.
valid?
(
:special_case
)
assert
"Invalid"
,
r
.
errors
[
:title
].
join
r
.
title
=
"secret"
r
.
content
=
"Good"
assert
r
.
valid?
(
:special_case
)
r
.
title
=
nil
assert
!
r
.
save
(
:context
=>
:special_case
)
assert
"Invalid"
,
r
.
errors
[
:title
].
join
r
.
title
=
"secret"
assert
r
.
save
(
:context
=>
:special_case
)
end
def
test_invalid_record_exception
assert_raise
(
ActiveRecord
::
RecordInvalid
)
{
WrongReply
.
create!
}
assert_raise
(
ActiveRecord
::
RecordInvalid
)
{
WrongReply
.
new
.
save!
}
...
...
activerecord/test/models/reply.rb
浏览文件 @
5c245b91
...
...
@@ -17,6 +17,7 @@ class WrongReply < Reply
validate
:check_empty_title
validate
:check_content_mismatch
,
:on
=>
:create
validate
:check_wrong_update
,
:on
=>
:update
validate
:check_title_is_secret
,
:on
=>
:special_case
def
check_empty_title
errors
[
:title
]
<<
"Empty"
unless
attribute_present?
(
"title"
)
...
...
@@ -39,6 +40,10 @@ def title_is_wrong_create
def
check_wrong_update
errors
[
:title
]
<<
"is Wrong Update"
if
attribute_present?
(
"title"
)
&&
title
==
"Wrong Update"
end
def
check_title_is_secret
errors
[
:title
]
<<
"Invalid"
unless
title
==
"secret"
end
end
class
SillyReply
<
Reply
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录