Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
8f6c85f0
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,发现更多精彩内容 >>
提交
8f6c85f0
编写于
6月 21, 2018
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #32190 from liwii/use_year_names
Add `use_year_names` option to date_select tag
上级
f84a4fd1
8f46a23d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
71 addition
and
1 deletion
+71
-1
actionview/CHANGELOG.md
actionview/CHANGELOG.md
+18
-0
actionview/lib/action_view/helpers/date_helper.rb
actionview/lib/action_view/helpers/date_helper.rb
+44
-1
actionview/test/template/date_helper_test.rb
actionview/test/template/date_helper_test.rb
+9
-0
未找到文件。
actionview/CHANGELOG.md
浏览文件 @
8f6c85f0
*
Add
`year_format`
option to date_select tag. This option makes it possible to customize year
names. Lambda should be passed to use this option.
Example:
date_select('user_birthday', '', start_year: 1998, end_year: 2000, year_format: ->year { "Heisei #{year - 1988}" })
The HTML produced:
<select id="user_birthday__1i" name="user_birthday[(1i)]">
<option value="1998">Heisei 10</option>
<option value="1999">Heisei 11</option>
<option value="2000">Heisei 12</option>
</select>
/* The rest is omitted */
*Koki Ryu*
*
Fix JavaScript views rendering does not work with Firefox when using
Content Security Policy.
...
...
actionview/lib/action_view/helpers/date_helper.rb
浏览文件 @
8f6c85f0
...
...
@@ -850,7 +850,7 @@ def select_year
raise
ArgumentError
,
"There are too many years options to be built. Are you sure you haven't mistyped something? You can provide the :max_years_allowed parameter."
end
build_
options_and_select
(
:year
,
val
,
options
)
build_
select
(
:year
,
build_year_options
(
val
,
options
)
)
end
end
...
...
@@ -933,6 +933,21 @@ def month_name(number)
end
end
# Looks up year names by number.
#
# year_name(1998) # => 1998
#
# If the <tt>:year_format</tt> option is passed:
#
# year_name(1998) # => "Heisei 10"
def
year_name
(
number
)
if
year_format_lambda
=
@options
[
:year_format
]
year_format_lambda
.
call
(
number
)
else
number
end
end
def
date_order
@date_order
||=
@options
[
:order
]
||
translated_date_order
end
...
...
@@ -995,6 +1010,34 @@ def build_options(selected, options = {})
(
select_options
.
join
(
"
\n
"
)
+
"
\n
"
).
html_safe
end
# Build select option HTML for year.
# If <tt>year_format</tt> option is not passed
# build_year_options(1998, start: 1998, end: 2000)
# => "<option value="1998" selected="selected">1998</option>
# <option value="1999">1999</option>
# <option value="2000">2000</option>"
#
# If <tt>year_format</tt> option is passed
# build_year_options(1998, start: 1998, end: 2000, year_format: ->year { "Heisei #{ year - 1988 }" })
# => "<option value="1998" selected="selected">Heisei 10</option>
# <option value="1999">Heisei 11</option>
# <option value="2000">Heisei 12</option>"
def
build_year_options
(
selected
,
options
=
{})
start
=
options
.
delete
(
:start
)
stop
=
options
.
delete
(
:end
)
step
=
options
.
delete
(
:step
)
select_options
=
[]
start
.
step
(
stop
,
step
)
do
|
value
|
tag_options
=
{
value:
value
}
tag_options
[
:selected
]
=
"selected"
if
selected
==
value
text
=
year_name
(
value
)
select_options
<<
content_tag
(
"option"
.
freeze
,
text
,
tag_options
)
end
(
select_options
.
join
(
"
\n
"
)
+
"
\n
"
).
html_safe
end
# Builds select tag from date type and HTML select options.
# build_select(:month, "<option value="1">January</option>...")
# => "<select id="post_written_on_2i" name="post[written_on(2i)]">
...
...
actionview/test/template/date_helper_test.rb
浏览文件 @
8f6c85f0
...
...
@@ -560,6 +560,15 @@ def test_select_year_with_position
assert_dom_equal
expected
,
select_year
(
Date
.
current
,
include_position:
true
,
start_year:
2003
,
end_year:
2005
)
end
def
test_select_year_with_custom_names
year_format_lambda
=
->
year
{
"Heisei
#{
year
-
1988
}
"
}
expected
=
%(<select id="date_year" name="date[year]">\n)
.
dup
expected
<<
%(<option value="2003">Heisei 15</option>\n<option value="2004">Heisei 16</option>\n<option value="2005">Heisei 17</option>\n)
expected
<<
"</select>
\n
"
assert_dom_equal
expected
,
select_year
(
nil
,
start_year:
2003
,
end_year:
2005
,
year_format:
year_format_lambda
)
end
def
test_select_hour
expected
=
%(<select id="date_hour" name="date[hour]">\n)
.
dup
expected
<<
%(<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08" selected="selected">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录