diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-06-21 17:00:45 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-06-21 17:01:35 +0900 |
commit | 8f6c85f0364b099b7be36f430198ea9bdb648ab5 (patch) | |
tree | d94883d4bdfd32d5b07e4bc5899005fef289c6b1 | |
parent | f84a4fd1d59761e3d0308ea17dcd2332440f648e (diff) | |
parent | 8f46a23d378cd807ff6c4a3b8e35723d4ef81432 (diff) | |
download | rails-8f6c85f0364b099b7be36f430198ea9bdb648ab5.tar.gz rails-8f6c85f0364b099b7be36f430198ea9bdb648ab5.tar.bz2 rails-8f6c85f0364b099b7be36f430198ea9bdb648ab5.zip |
Merge pull request #32190 from liwii/use_year_names
Add `use_year_names` option to date_select tag
-rw-r--r-- | actionview/CHANGELOG.md | 18 | ||||
-rw-r--r-- | actionview/lib/action_view/helpers/date_helper.rb | 45 | ||||
-rw-r--r-- | actionview/test/template/date_helper_test.rb | 9 |
3 files changed, 71 insertions, 1 deletions
diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 2c1ca12043..26170dfa77 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,21 @@ +* 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. diff --git a/actionview/lib/action_view/helpers/date_helper.rb b/actionview/lib/action_view/helpers/date_helper.rb index 620e1e9f21..1622b9f680 100644 --- a/actionview/lib/action_view/helpers/date_helper.rb +++ b/actionview/lib/action_view/helpers/date_helper.rb @@ -850,7 +850,7 @@ module ActionView 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 @@ module ActionView 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 @@ module ActionView (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)]"> diff --git a/actionview/test/template/date_helper_test.rb b/actionview/test/template/date_helper_test.rb index 4b4939d705..701e6c86fd 100644 --- a/actionview/test/template/date_helper_test.rb +++ b/actionview/test/template/date_helper_test.rb @@ -560,6 +560,15 @@ class DateHelperTest < ActionView::TestCase 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) |