Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
51dd2588
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,发现更多精彩内容 >>
提交
51dd2588
编写于
9月 07, 2015
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #21069 from dmitry/feature/validate-multiple-contexts-at-once
Validate multiple contexts on `valid?` and `invalid?` at once
上级
ecb4e4b2
86e3b047
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
50 addition
and
2 deletion
+50
-2
activemodel/CHANGELOG.md
activemodel/CHANGELOG.md
+18
-0
activemodel/lib/active_model/validations.rb
activemodel/lib/active_model/validations.rb
+1
-1
activemodel/test/cases/validations/validations_context_test.rb
...emodel/test/cases/validations/validations_context_test.rb
+19
-0
activerecord/lib/active_record/validations.rb
activerecord/lib/active_record/validations.rb
+5
-1
activerecord/test/cases/validations_test.rb
activerecord/test/cases/validations_test.rb
+7
-0
未找到文件。
activemodel/CHANGELOG.md
浏览文件 @
51dd2588
*
Validate multiple contexts on
`valid?`
and
`invalid?`
at once.
Example:
class Person
include ActiveModel::Validations
attr_reader :name, :title
validates_presence_of :name, on: :create
validates_presence_of :title, on: :update
end
person = Person.new
person.valid?([:create, :update]) # => true
person.errors.messages # => {:name=>["can't be blank"], :title=>["can't be blank"]}
*Dmitry Polushkin*
*
Add case_sensitive option for confirmation validator in models.
*Akshat Sharma*
...
...
activemodel/lib/active_model/validations.rb
浏览文件 @
51dd2588
...
...
@@ -162,7 +162,7 @@ def validate(*args, &block)
options
=
options
.
dup
options
[
:if
]
=
Array
(
options
[
:if
])
options
[
:if
].
unshift
->
(
o
)
{
Array
(
options
[
:on
]).
include?
(
o
.
validation_context
)
!
(
Array
(
options
[
:on
])
&
Array
(
o
.
validation_context
)).
empty?
}
end
...
...
activemodel/test/cases/validations/validations_context_test.rb
浏览文件 @
51dd2588
...
...
@@ -8,6 +8,7 @@ def teardown
end
ERROR_MESSAGE
=
"Validation error from validator"
ANOTHER_ERROR_MESSAGE
=
"Another validation error from validator"
class
ValidatorThatAddsErrors
<
ActiveModel
::
Validator
def
validate
(
record
)
...
...
@@ -15,6 +16,12 @@ def validate(record)
end
end
class
AnotherValidatorThatAddsErrors
<
ActiveModel
::
Validator
def
validate
(
record
)
record
.
errors
[
:base
]
<<
ANOTHER_ERROR_MESSAGE
end
end
test
"with a class that adds errors on create and validating a new model with no arguments"
do
Topic
.
validates_with
(
ValidatorThatAddsErrors
,
on: :create
)
topic
=
Topic
.
new
...
...
@@ -46,4 +53,16 @@ def validate(record)
assert
topic
.
invalid?
(
:context2
),
"Validation did not run on context2 when 'on' is set to context1 and context2"
assert
topic
.
errors
[
:base
].
include?
(
ERROR_MESSAGE
)
end
test
"with a class that validating a model for a multiple contexts"
do
Topic
.
validates_with
(
ValidatorThatAddsErrors
,
on: :context1
)
Topic
.
validates_with
(
AnotherValidatorThatAddsErrors
,
on: :context2
)
topic
=
Topic
.
new
assert
topic
.
valid?
,
"Validation ran with no context given when 'on' is set to context1 and context2"
assert
topic
.
invalid?
([
:context1
,
:context2
]),
"Validation did not run on context1 when 'on' is set to context1 and context2"
assert
topic
.
errors
[
:base
].
include?
(
ERROR_MESSAGE
)
assert
topic
.
errors
[
:base
].
include?
(
ANOTHER_ERROR_MESSAGE
)
end
end
activerecord/lib/active_record/validations.rb
浏览文件 @
51dd2588
...
...
@@ -54,7 +54,7 @@ def save!(options={})
# Validations with no <tt>:on</tt> option will run no matter the context. Validations with
# some <tt>:on</tt> option will only run in the specified context.
def
valid?
(
context
=
nil
)
context
||=
(
new_record?
?
:create
:
:update
)
context
||=
default_validation_context
output
=
super
(
context
)
errors
.
empty?
&&
output
end
...
...
@@ -63,6 +63,10 @@ def valid?(context = nil)
protected
def
default_validation_context
[
new_record?
?
:create
:
:update
]
end
def
raise_validation_error
raise
(
RecordInvalid
.
new
(
self
))
end
...
...
activerecord/test/cases/validations_test.rb
浏览文件 @
51dd2588
...
...
@@ -52,6 +52,13 @@ def test_valid_using_special_context
assert
r
.
valid?
(
:special_case
)
end
def
test_invalid_using_multiple_contexts
r
=
WrongReply
.
new
(
:title
=>
'Wrong Create'
)
assert
r
.
invalid?
([
:special_case
,
:create
])
assert_equal
"Invalid"
,
r
.
errors
[
:author_name
].
join
assert_equal
"is Wrong Create"
,
r
.
errors
[
:title
].
join
end
def
test_validate
r
=
WrongReply
.
new
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录