提交 8f6c85f0 编写于 作者: R Ryuta Kamizono

Merge pull request #32190 from liwii/use_year_names

Add `use_year_names` option to date_select tag
* 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.
......
......@@ -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)]">
......
......@@ -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.
先完成此消息的编辑!
想要评论请 注册