Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
fdc0d934
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,发现更多精彩内容 >>
提交
fdc0d934
编写于
2月 05, 2015
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'rm-use-active-model-correctly'
Closes #18646
上级
80838d9b
4b4e2281
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
103 addition
and
28 deletion
+103
-28
actionview/lib/action_view/helpers/tags.rb
actionview/lib/action_view/helpers/tags.rb
+1
-0
actionview/lib/action_view/helpers/tags/label.rb
actionview/lib/action_view/helpers/tags/label.rb
+3
-13
actionview/lib/action_view/helpers/tags/placeholderable.rb
actionview/lib/action_view/helpers/tags/placeholderable.rb
+3
-15
actionview/lib/action_view/helpers/tags/translator.rb
actionview/lib/action_view/helpers/tags/translator.rb
+38
-0
actionview/test/lib/controller/fake_models.rb
actionview/test/lib/controller/fake_models.rb
+16
-0
actionview/test/template/form_helper_test.rb
actionview/test/template/form_helper_test.rb
+42
-0
未找到文件。
actionview/lib/action_view/helpers/tags.rb
浏览文件 @
fdc0d934
...
...
@@ -5,6 +5,7 @@ module Tags #:nodoc:
eager_autoload
do
autoload
:Base
autoload
:Translator
autoload
:CheckBox
autoload
:CollectionCheckBoxes
autoload
:CollectionRadioButtons
...
...
actionview/lib/action_view/helpers/tags/label.rb
浏览文件 @
fdc0d934
...
...
@@ -15,20 +15,10 @@ def initialize(template_object, object_name, method_name, object, tag_value)
def
translation
method_and_value
=
@tag_value
.
present?
?
"
#{
@method_name
}
.
#{
@tag_value
}
"
:
@method_name
@object_name
.
gsub!
(
/\[(.*)_attributes\]\[\d+\]/
,
'.\1'
)
if
object
.
respond_to?
(
:to_model
)
key
=
object
.
model_name
.
i18n_key
i18n_default
=
[
"
#{
key
}
.
#{
method_and_value
}
"
.
to_sym
,
""
]
end
i18n_default
||=
""
content
=
I18n
.
t
(
"
#{
@object_name
}
.
#{
method_and_value
}
"
,
:default
=>
i18n_default
,
:scope
=>
"helpers.label"
).
presence
content
||=
if
object
&&
object
.
class
.
respond_to?
(
:human_attribute_name
)
object
.
class
.
human_attribute_name
(
method_and_value
)
end
content
||=
Translator
.
new
(
object
,
@object_name
,
method_and_value
,
scope:
"helpers.label"
)
.
translate
content
||=
@method_name
.
humanize
content
...
...
actionview/lib/action_view/helpers/tags/placeholderable.rb
浏览文件 @
fdc0d934
...
...
@@ -7,24 +7,12 @@ def initialize(*)
if
tag_value
=
@options
[
:placeholder
]
placeholder
=
tag_value
if
tag_value
.
is_a?
(
String
)
object_name
=
@object_name
.
gsub
(
/\[(.*)_attributes\]\[\d+\]/
,
'.\1'
)
method_and_value
=
tag_value
.
is_a?
(
TrueClass
)
?
@method_name
:
"
#{
@method_name
}
.
#{
tag_value
}
"
if
object
.
respond_to?
(
:to_model
)
key
=
object
.
class
.
model_name
.
i18n_key
i18n_default
=
[
"
#{
key
}
.
#{
method_and_value
}
"
.
to_sym
,
""
]
end
i18n_default
||=
""
placeholder
||=
I18n
.
t
(
"
#{
object_name
}
.
#{
method_and_value
}
"
,
:default
=>
i18n_default
,
:scope
=>
"helpers.placeholder"
).
presence
placeholder
||=
if
object
&&
object
.
class
.
respond_to?
(
:human_attribute_name
)
object
.
class
.
human_attribute_name
(
method_and_value
)
end
placeholder
||=
Tags
::
Translator
.
new
(
object
,
@object_name
,
method_and_value
,
scope:
"helpers.placeholder"
)
.
translate
placeholder
||=
@method_name
.
humanize
@options
[
:placeholder
]
=
placeholder
end
end
...
...
actionview/lib/action_view/helpers/tags/translator.rb
0 → 100644
浏览文件 @
fdc0d934
module
ActionView
module
Helpers
module
Tags
# :nodoc:
class
Translator
# :nodoc:
def
initialize
(
object
,
object_name
,
method_and_value
,
scope
:)
@object_name
=
object_name
.
gsub
(
/\[(.*)_attributes\]\[\d+\]/
,
'.\1'
)
@method_and_value
=
method_and_value
@scope
=
scope
@model
=
object
.
respond_to?
(
:to_model
)
?
object
.
to_model
:
object
end
def
translate
translated_attribute
=
I18n
.
t
(
"
#{
object_name
}
.
#{
method_and_value
}
"
,
default:
i18n_default
,
scope:
scope
).
presence
translated_attribute
||
human_attribute_name
end
private
attr_reader
:object_name
,
:method_and_value
,
:scope
,
:model
def
i18n_default
if
model
key
=
model
.
model_name
.
i18n_key
[
"
#{
key
}
.
#{
method_and_value
}
"
.
to_sym
,
""
]
else
""
end
end
def
human_attribute_name
if
model
&&
model
.
class
.
respond_to?
(
:human_attribute_name
)
model
.
class
.
human_attribute_name
(
method_and_value
)
end
end
end
end
end
end
actionview/test/lib/controller/fake_models.rb
浏览文件 @
fdc0d934
...
...
@@ -54,6 +54,22 @@ def comments_attributes=(attributes); end
def
tags_attributes
=
(
attributes
);
end
end
class
PostDelegator
<
Post
def
to_model
PostDelegate
.
new
end
end
class
PostDelegate
<
Post
def
self
.
human_attribute_name
(
attribute
)
"Delegate
#{
super
}
"
end
def
model_name
ActiveModel
::
Name
.
new
(
self
.
class
)
end
end
class
Comment
extend
ActiveModel
::
Naming
include
ActiveModel
::
Conversion
...
...
actionview/test/template/form_helper_test.rb
浏览文件 @
fdc0d934
...
...
@@ -40,6 +40,9 @@ def form_for(*)
},
tag:
{
value:
"Tag"
},
post_delegate:
{
title:
'Delegate model_name title'
}
}
}
...
...
@@ -81,6 +84,9 @@ def form_for(*)
body:
"Write body here"
}
},
post_delegate:
{
title:
'Delegate model_name title'
},
tag:
{
value:
"Tag"
}
...
...
@@ -117,6 +123,10 @@ def @post.to_param; '123'; end
@post
.
tags
=
[]
@post
.
tags
<<
Tag
.
new
@post_delegator
=
PostDelegator
.
new
@post_delegator
.
title
=
'Hello World'
@car
=
Car
.
new
(
"#000FFF"
)
end
...
...
@@ -337,6 +347,22 @@ def test_label_with_block_in_erb
)
end
def
test_label_with_to_model
assert_dom_equal
(
%{<label for="post_delegator_title">Delegate Title</label>}
,
label
(
:post_delegator
,
:title
)
)
end
def
test_label_with_to_model_and_overriden_model_name
with_locale
:label
do
assert_dom_equal
(
%{<label for="post_delegator_title">Delegate model_name title</label>}
,
label
(
:post_delegator
,
:title
)
)
end
end
def
test_text_field_placeholder_without_locales
with_locale
:placeholder
do
assert_dom_equal
(
'<input id="post_body" name="post[body]" placeholder="Body" type="text" value="Back to the hill and over it again!" />'
,
text_field
(
:post
,
:body
,
placeholder:
true
))
...
...
@@ -349,12 +375,28 @@ def test_text_field_placeholder_with_locales
end
end
def
test_text_field_placeholder_with_locales_and_to_model
with_locale
:placeholder
do
assert_dom_equal
(
'<input id="post_delegator_title" name="post_delegator[title]" placeholder="Delegate model_name title" type="text" value="Hello World" />'
,
text_field
(
:post_delegator
,
:title
,
placeholder:
true
)
)
end
end
def
test_text_field_placeholder_with_human_attribute_name
with_locale
:placeholder
do
assert_dom_equal
(
'<input id="post_cost" name="post[cost]" placeholder="Total cost" type="text" />'
,
text_field
(
:post
,
:cost
,
placeholder:
true
))
end
end
def
test_text_field_placeholder_with_human_attribute_name_and_to_model
assert_dom_equal
(
'<input id="post_delegator_title" name="post_delegator[title]" placeholder="Delegate Title" type="text" value="Hello World" />'
,
text_field
(
:post_delegator
,
:title
,
placeholder:
true
)
)
end
def
test_text_field_placeholder_with_string_value
with_locale
:placeholder
do
assert_dom_equal
(
'<input id="post_cost" name="post[cost]" placeholder="HOW MUCH?" type="text" />'
,
text_field
(
:post
,
:cost
,
placeholder:
"HOW MUCH?"
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录