Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
decff3f4
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,体验更适合开发者的 AI 搜索 >>
提交
decff3f4
编写于
2月 20, 2012
作者:
V
Vasiliy Ermolovich
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor options_for_select
上级
421025f1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
24 addition
and
23 deletion
+24
-23
actionpack/lib/action_view/helpers/form_options_helper.rb
actionpack/lib/action_view/helpers/form_options_helper.rb
+8
-7
actionpack/test/template/form_options_helper_test.rb
actionpack/test/template/form_options_helper_test.rb
+16
-16
未找到文件。
actionpack/lib/action_view/helpers/form_options_helper.rb
浏览文件 @
decff3f4
...
...
@@ -330,9 +330,12 @@ def options_for_select(container, selected = nil)
container
.
map
do
|
element
|
html_attributes
=
option_html_attributes
(
element
)
text
,
value
=
option_text_and_value
(
element
).
map
{
|
item
|
item
.
to_s
}
selected_attribute
=
' selected="selected"'
if
option_value_selected?
(
value
,
selected
)
disabled_attribute
=
' disabled="disabled"'
if
disabled
&&
option_value_selected?
(
value
,
disabled
)
%(<option value="#{ERB::Util.html_escape(value)}"#{selected_attribute}#{disabled_attribute}#{html_attributes}>#{ERB::Util.html_escape(text)}</option>)
html_attributes
[
:selected
]
=
'selected'
if
option_value_selected?
(
value
,
selected
)
html_attributes
[
:disabled
]
=
'disabled'
if
disabled
&&
option_value_selected?
(
value
,
disabled
)
html_attributes
[
:value
]
=
value
content_tag
(
:option
,
text
,
html_attributes
)
end
.
join
(
"
\n
"
).
html_safe
end
...
...
@@ -649,11 +652,9 @@ def collection_check_boxes(object, method, collection, value_method, text_method
private
def
option_html_attributes
(
element
)
return
""
unless
Array
===
element
return
{}
unless
Array
===
element
element
.
select
{
|
e
|
Hash
===
e
}.
reduce
({},
:merge
).
map
do
|
k
,
v
|
"
#{
k
}
=
\"
#{
ERB
::
Util
.
html_escape
(
v
.
to_s
)
}
\"
"
end
.
join
Hash
[
element
.
select
{
|
e
|
Hash
===
e
}.
reduce
({},
:merge
).
map
{
|
k
,
v
|
[
k
,
ERB
::
Util
.
html_escape
(
v
.
to_s
)]
}]
end
def
option_text_and_value
(
option
)
...
...
actionpack/test/template/form_options_helper_test.rb
浏览文件 @
decff3f4
...
...
@@ -182,16 +182,16 @@ def test_array_options_for_string_include_in_other_string_bug_fix
def
test_hash_options_for_select
assert_dom_equal
(
"<option value=
\"
<Kroner>
\"
><DKR></option>
\n
<option value=
\"
Dollar
\"
>$
</option>"
,
options_for_select
(
"$"
=>
"Dollar"
,
"<DKR>"
=>
"<Kroner>"
).
split
(
"
\n
"
).
sort
.
join
(
"
\n
"
)
"<option value=
\"
Dollar
\"
>$</option>
\n
<option value=
\"
<Kroner>
\"
><DKR>
</option>"
,
options_for_select
(
"$"
=>
"Dollar"
,
"<DKR>"
=>
"<Kroner>"
).
split
(
"
\n
"
).
join
(
"
\n
"
)
)
assert_dom_equal
(
"<option value=
\"
<Kroner>
\"
><DKR></option>
\n
<option value=
\"
Dollar
\"
selected=
\"
selected
\"
>$
</option>"
,
options_for_select
({
"$"
=>
"Dollar"
,
"<DKR>"
=>
"<Kroner>"
},
"Dollar"
).
split
(
"
\n
"
).
sort
.
join
(
"
\n
"
)
"<option value=
\"
Dollar
\"
selected=
\"
selected
\"
>$</option>
\n
<option value=
\"
<Kroner>
\"
><DKR>
</option>"
,
options_for_select
({
"$"
=>
"Dollar"
,
"<DKR>"
=>
"<Kroner>"
},
"Dollar"
).
split
(
"
\n
"
).
join
(
"
\n
"
)
)
assert_dom_equal
(
"<option value=
\"
<Kroner>
\"
selected=
\"
selected
\"
><DKR></option>
\n
<option value=
\"
Dollar
\"
selected=
\"
selected
\"
>$
</option>"
,
options_for_select
({
"$"
=>
"Dollar"
,
"<DKR>"
=>
"<Kroner>"
},
[
"Dollar"
,
"<Kroner>"
]).
split
(
"
\n
"
).
sort
.
join
(
"
\n
"
)
"<option value=
\"
Dollar
\"
selected=
\"
selected
\"
>$</option>
\n
<option value=
\"
<Kroner>
\"
selected=
\"
selected
\"
><DKR>
</option>"
,
options_for_select
({
"$"
=>
"Dollar"
,
"<DKR>"
=>
"<Kroner>"
},
[
"Dollar"
,
"<Kroner>"
]).
split
(
"
\n
"
).
join
(
"
\n
"
)
)
end
...
...
@@ -1056,36 +1056,36 @@ def test_options_for_select_with_element_attributes_and_selection_array
end
def
test_option_html_attributes_from_without_hash
assert_
dom_
equal
(
""
,
assert_equal
(
{}
,
option_html_attributes
([
'foo'
,
'bar'
])
)
end
def
test_option_html_attributes_with_single_element_hash
assert_
dom_
equal
(
" class=
\"
fancy
\"
"
,
assert_equal
(
{
:class
=>
'fancy'
}
,
option_html_attributes
([
'foo'
,
'bar'
,
{
:class
=>
'fancy'
}
])
)
end
def
test_option_html_attributes_with_multiple_element_hash
assert_
dom_
equal
(
" class=
\"
fancy
\"
onclick=
\"
alert('Hello World');
\"
"
,
assert_equal
(
{
:class
=>
'fancy'
,
'onclick'
=>
"alert('Hello World');"
}
,
option_html_attributes
([
'foo'
,
'bar'
,
{
:class
=>
'fancy'
,
'onclick'
=>
"alert('Hello World');"
}
])
)
end
def
test_option_html_attributes_with_multiple_hashes
assert_
dom_
equal
(
" class=
\"
fancy
\"
onclick=
\"
alert('Hello World');
\"
"
,
assert_equal
(
{
:class
=>
'fancy'
,
'onclick'
=>
"alert('Hello World');"
}
,
option_html_attributes
([
'foo'
,
'bar'
,
{
:class
=>
'fancy'
},
{
'onclick'
=>
"alert('Hello World');"
}
])
)
end
def
test_option_html_attributes_with_special_characters
assert_
dom_
equal
(
" onclick=
\"
alert("<code>")
\"
"
,
assert_equal
(
{
:onclick
=>
"alert("<code>")"
}
,
option_html_attributes
([
'foo'
,
'bar'
,
{
:onclick
=>
%(alert("<code>"))
}
])
)
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录