Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ce9b6ad5
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,发现更多精彩内容 >>
提交
ce9b6ad5
编写于
11月 05, 2012
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #8118 from nashby/activemodel-errors
use Array() instead of flatten
上级
b8b100a8
70ecf6c5
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
41 addition
and
23 deletion
+41
-23
activemodel/lib/active_model/errors.rb
activemodel/lib/active_model/errors.rb
+2
-2
activemodel/test/cases/errors_test.rb
activemodel/test/cases/errors_test.rb
+39
-1
activemodel/test/cases/validations/i18n_validation_test.rb
activemodel/test/cases/validations/i18n_validation_test.rb
+0
-20
未找到文件。
activemodel/lib/active_model/errors.rb
浏览文件 @
ce9b6ad5
...
...
@@ -308,7 +308,7 @@ def add(attribute, message = nil, options = {})
# person.errors.messages
# # => {:name=>["can't be empty"]}
def
add_on_empty
(
attributes
,
options
=
{})
[
attributes
].
flatten
.
each
do
|
attribute
|
Array
(
attributes
)
.
each
do
|
attribute
|
value
=
@base
.
send
(
:read_attribute_for_validation
,
attribute
)
is_empty
=
value
.
respond_to?
(
:empty?
)
?
value
.
empty?
:
false
add
(
attribute
,
:empty
,
options
)
if
value
.
nil?
||
is_empty
...
...
@@ -322,7 +322,7 @@ def add_on_empty(attributes, options = {})
# person.errors.messages
# # => {:name=>["can't be blank"]}
def
add_on_blank
(
attributes
,
options
=
{})
[
attributes
].
flatten
.
each
do
|
attribute
|
Array
(
attributes
)
.
each
do
|
attribute
|
value
=
@base
.
send
(
:read_attribute_for_validation
,
attribute
)
add
(
attribute
,
:blank
,
options
)
if
value
.
blank?
end
...
...
activemodel/test/cases/errors_test.rb
浏览文件 @
ce9b6ad5
...
...
@@ -7,7 +7,7 @@ def initialize
@errors
=
ActiveModel
::
Errors
.
new
(
self
)
end
attr_accessor
:name
attr_accessor
:name
,
:age
attr_reader
:errors
def
validate!
...
...
@@ -201,5 +201,43 @@ def test_has_key?
person
.
errors
.
generate_message
(
:name
,
:blank
)
}
end
test
"add_on_empty generates message"
do
person
=
Person
.
new
person
.
errors
.
expects
(
:generate_message
).
with
(
:name
,
:empty
,
{})
person
.
errors
.
add_on_empty
:name
end
test
"add_on_empty generates message for multiple attributes"
do
person
=
Person
.
new
person
.
errors
.
expects
(
:generate_message
).
with
(
:name
,
:empty
,
{})
person
.
errors
.
expects
(
:generate_message
).
with
(
:age
,
:empty
,
{})
person
.
errors
.
add_on_empty
[
:name
,
:age
]
end
test
"add_on_empty generates message with custom default message"
do
person
=
Person
.
new
person
.
errors
.
expects
(
:generate_message
).
with
(
:name
,
:empty
,
{
:message
=>
'custom'
})
person
.
errors
.
add_on_empty
:name
,
:message
=>
'custom'
end
test
"add_on_blank generates message"
do
person
=
Person
.
new
person
.
errors
.
expects
(
:generate_message
).
with
(
:name
,
:blank
,
{})
person
.
errors
.
add_on_blank
:name
end
test
"add_on_blank generates message for multiple attributes"
do
person
=
Person
.
new
person
.
errors
.
expects
(
:generate_message
).
with
(
:name
,
:blank
,
{})
person
.
errors
.
expects
(
:generate_message
).
with
(
:age
,
:blank
,
{})
person
.
errors
.
add_on_blank
[
:name
,
:age
]
end
test
"add_on_blank generates message with custom default message"
do
person
=
Person
.
new
person
.
errors
.
expects
(
:generate_message
).
with
(
:name
,
:blank
,
{
:message
=>
'custom'
})
person
.
errors
.
add_on_blank
:name
,
:message
=>
'custom'
end
end
activemodel/test/cases/validations/i18n_validation_test.rb
浏览文件 @
ce9b6ad5
...
...
@@ -21,26 +21,6 @@ def teardown
I18n
.
backend
=
@old_backend
end
def
test_errors_add_on_empty_generates_message
@person
.
errors
.
expects
(
:generate_message
).
with
(
:title
,
:empty
,
{})
@person
.
errors
.
add_on_empty
:title
end
def
test_errors_add_on_empty_generates_message_with_custom_default_message
@person
.
errors
.
expects
(
:generate_message
).
with
(
:title
,
:empty
,
{
:message
=>
'custom'
})
@person
.
errors
.
add_on_empty
:title
,
:message
=>
'custom'
end
def
test_errors_add_on_blank_generates_message
@person
.
errors
.
expects
(
:generate_message
).
with
(
:title
,
:blank
,
{})
@person
.
errors
.
add_on_blank
:title
end
def
test_errors_add_on_blank_generates_message_with_custom_default_message
@person
.
errors
.
expects
(
:generate_message
).
with
(
:title
,
:blank
,
{
:message
=>
'custom'
})
@person
.
errors
.
add_on_blank
:title
,
:message
=>
'custom'
end
def
test_full_message_encoding
I18n
.
backend
.
store_translations
(
'en'
,
:errors
=>
{
:messages
=>
{
:too_short
=>
'猫舌'
}})
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录