Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
76797b44
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,发现更多精彩内容 >>
提交
76797b44
编写于
8月 31, 2008
作者:
I
Iain Hecker
提交者:
Jeremy Kemper
8月 31, 2008
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
translates when a message symbol has been set on builtin validations
Signed-off-by:
N
Jeremy Kemper
<
jeremy@bitsweat.net
>
上级
a1eb4e11
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
35 addition
and
1 deletion
+35
-1
activerecord/lib/active_record/validations.rb
activerecord/lib/active_record/validations.rb
+2
-1
activerecord/test/cases/validations_i18n_test.rb
activerecord/test/cases/validations_i18n_test.rb
+33
-0
未找到文件。
activerecord/lib/active_record/validations.rb
浏览文件 @
76797b44
...
...
@@ -87,6 +87,8 @@ def add_on_blank(attributes, custom_message = nil)
# </ol>
def
generate_message
(
attribute
,
message
=
:invalid
,
options
=
{})
message
,
options
[
:default
]
=
options
[
:default
],
message
if
options
[
:default
].
is_a?
(
Symbol
)
defaults
=
@base
.
class
.
self_and_descendents_from_active_record
.
map
do
|
klass
|
[
:"models.
#{
klass
.
name
.
underscore
}
.attributes.
#{
attribute
}
.
#{
message
}
"
,
:"models.
#{
klass
.
name
.
underscore
}
.
#{
message
}
"
]
...
...
@@ -95,7 +97,6 @@ def generate_message(attribute, message = :invalid, options = {})
defaults
<<
options
.
delete
(
:default
)
defaults
=
defaults
.
compact
.
flatten
<<
:"messages.
#{
message
}
"
model_name
=
@base
.
class
.
name
key
=
defaults
.
shift
value
=
@base
.
respond_to?
(
attribute
)
?
@base
.
send
(
attribute
)
:
nil
...
...
activerecord/test/cases/validations_i18n_test.rb
浏览文件 @
76797b44
...
...
@@ -675,6 +675,38 @@ def test_validates_associated_finds_global_default_translation
replied_topic
.
valid?
assert_equal
'global message'
,
replied_topic
.
errors
.
on
(
:replies
)
end
def
test_validations_with_message_symbol_must_translate
I18n
.
backend
.
store_translations
'en-US'
,
:activerecord
=>
{
:errors
=>
{
:messages
=>
{
:custom_error
=>
"I am a custom error"
}}}
Topic
.
validates_presence_of
:title
,
:message
=>
:custom_error
@topic
.
title
=
nil
@topic
.
valid?
assert_equal
"I am a custom error"
,
@topic
.
errors
.
on
(
:title
)
end
def
test_validates_with_message_symbol_must_translate_per_attribute
I18n
.
backend
.
store_translations
'en-US'
,
:activerecord
=>
{
:errors
=>
{
:models
=>
{
:topic
=>
{
:attributes
=>
{
:title
=>
{
:custom_error
=>
"I am a custom error"
}}}}}}
Topic
.
validates_presence_of
:title
,
:message
=>
:custom_error
@topic
.
title
=
nil
@topic
.
valid?
assert_equal
"I am a custom error"
,
@topic
.
errors
.
on
(
:title
)
end
def
test_validates_with_message_symbol_must_translate_per_model
I18n
.
backend
.
store_translations
'en-US'
,
:activerecord
=>
{
:errors
=>
{
:models
=>
{
:topic
=>
{
:custom_error
=>
"I am a custom error"
}}}}
Topic
.
validates_presence_of
:title
,
:message
=>
:custom_error
@topic
.
title
=
nil
@topic
.
valid?
assert_equal
"I am a custom error"
,
@topic
.
errors
.
on
(
:title
)
end
def
test_validates_with_message_string
Topic
.
validates_presence_of
:title
,
:message
=>
"I am a custom error"
@topic
.
title
=
nil
@topic
.
valid?
assert_equal
"I am a custom error"
,
@topic
.
errors
.
on
(
:title
)
end
end
class
ActiveRecordValidationsGenerateMessageI18nTests
<
Test
::
Unit
::
TestCase
...
...
@@ -855,4 +887,5 @@ def test_generate_message_odd_with_default_message
def
test_generate_message_even_with_default_message
assert_equal
"must be even"
,
@topic
.
errors
.
generate_message
(
:title
,
:even
,
:default
=>
nil
,
:value
=>
'title'
,
:count
=>
10
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录