提交 5f295aeb 编写于 作者: X Xavier Noria

implements new option :month_format_string for date select helpers [Closes #13618]

上级 bfc776f7
* Date select helpers accept a format string for the months selector via the
new option `:month_format_string`.
When rendered, the format string gets passed keys `:number` (integer), and
`:name` (string), in order to be able to interpolate them as in
'%{name} (%<number>02d)'
for example.
This option is motivated by #13618.
*Xavier Noria*
* Added `config.action_view.raise_on_missing_translations` to define whether an * Added `config.action_view.raise_on_missing_translations` to define whether an
error should be raised for missing translations. error should be raised for missing translations.
......
...@@ -169,6 +169,9 @@ def time_ago_in_words(from_time, include_seconds_or_options = {}) ...@@ -169,6 +169,9 @@ def time_ago_in_words(from_time, include_seconds_or_options = {})
# "2 - February" instead of "February"). # "2 - February" instead of "February").
# * <tt>:use_month_names</tt> - Set to an array with 12 month names if you want to customize month names. # * <tt>:use_month_names</tt> - Set to an array with 12 month names if you want to customize month names.
# Note: You can also use Rails' i18n functionality for this. # Note: You can also use Rails' i18n functionality for this.
# * <tt>:month_format_string</tt> - Set to a format string. The string gets passed keys +:number+ (integer)
# and +:name+ (string). A format string would be something like "%{name} (%<number>02d)" for example.
# See <tt>Kernel.sprintf</tt> for documentation on format sequences.
# * <tt>:date_separator</tt> - Specifies a string to separate the date fields. Default is "" (i.e. nothing). # * <tt>:date_separator</tt> - Specifies a string to separate the date fields. Default is "" (i.e. nothing).
# * <tt>:start_year</tt> - Set the start year for the year select. Default is <tt>Date.today.year - 5</tt>if # * <tt>:start_year</tt> - Set the start year for the year select. Default is <tt>Date.today.year - 5</tt>if
# you are creating new record. While editing existing record, <tt>:start_year</tt> defaults to # you are creating new record. While editing existing record, <tt>:start_year</tt> defaults to
...@@ -850,24 +853,36 @@ def translated_month_names ...@@ -850,24 +853,36 @@ def translated_month_names
I18n.translate(key, :locale => @options[:locale]) I18n.translate(key, :locale => @options[:locale])
end end
# Lookup month name for number. # Looks up month names by number (1-based):
# month_name(1) => "January"
# #
# If <tt>:use_month_numbers</tt> option is passed # month_name(1) # => "January"
# month_name(1) => 1
# #
# If <tt>:use_two_month_numbers</tt> option is passed # If the <tt>:use_month_numbers</tt> option is passed:
# month_name(1) => '01'
# #
# If <tt>:add_month_numbers</tt> option is passed # month_name(1) # => 1
# month_name(1) => "1 - January" #
# If the <tt>:use_two_month_numbers</tt> option is passed:
#
# month_name(1) # => '01'
#
# If the <tt>:add_month_numbers</tt> option is passed:
#
# month_name(1) # => "1 - January"
#
# If the <tt>:month_format_string</tt> option is passed:
#
# month_name(1) # => "January (01)"
#
# depending on the format string.
def month_name(number) def month_name(number)
if @options[:use_month_numbers] if @options[:use_month_numbers]
number number
elsif @options[:use_two_digit_numbers] elsif @options[:use_two_digit_numbers]
sprintf "%02d", number '%02d' % number
elsif @options[:add_month_numbers] elsif @options[:add_month_numbers]
"#{number} - #{month_names[number]}" "#{number} - #{month_names[number]}"
elsif format_string = @options[:month_format_string]
format_string % {number: number, name: month_names[number]}
else else
month_names[number] month_names[number]
end end
......
...@@ -326,6 +326,16 @@ def test_select_month_with_numbers_and_names ...@@ -326,6 +326,16 @@ def test_select_month_with_numbers_and_names
assert_dom_equal expected, select_month(8, :add_month_numbers => true) assert_dom_equal expected, select_month(8, :add_month_numbers => true)
end end
def test_select_month_with_format_string
expected = %(<select id="date_month" name="date[month]">\n)
expected << %(<option value="1">January (01)</option>\n<option value="2">February (02)</option>\n<option value="3">March (03)</option>\n<option value="4">April (04)</option>\n<option value="5">May (05)</option>\n<option value="6">June (06)</option>\n<option value="7">July (07)</option>\n<option value="8" selected="selected">August (08)</option>\n<option value="9">September (09)</option>\n<option value="10">October (10)</option>\n<option value="11">November (11)</option>\n<option value="12">December (12)</option>\n)
expected << "</select>\n"
format_string = '%{name} (%<number>02d)'
assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :month_format_string => format_string)
assert_dom_equal expected, select_month(8, :month_format_string => format_string)
end
def test_select_month_with_numbers_and_names_with_abbv def test_select_month_with_numbers_and_names_with_abbv
expected = %(<select id="date_month" name="date[month]">\n) expected = %(<select id="date_month" name="date[month]">\n)
expected << %(<option value="1">1 - Jan</option>\n<option value="2">2 - Feb</option>\n<option value="3">3 - Mar</option>\n<option value="4">4 - Apr</option>\n<option value="5">5 - May</option>\n<option value="6">6 - Jun</option>\n<option value="7">7 - Jul</option>\n<option value="8" selected="selected">8 - Aug</option>\n<option value="9">9 - Sep</option>\n<option value="10">10 - Oct</option>\n<option value="11">11 - Nov</option>\n<option value="12">12 - Dec</option>\n) expected << %(<option value="1">1 - Jan</option>\n<option value="2">2 - Feb</option>\n<option value="3">3 - Mar</option>\n<option value="4">4 - Apr</option>\n<option value="5">5 - May</option>\n<option value="6">6 - Jun</option>\n<option value="7">7 - Jul</option>\n<option value="8" selected="selected">8 - Aug</option>\n<option value="9">9 - Sep</option>\n<option value="10">10 - Oct</option>\n<option value="11">11 - Nov</option>\n<option value="12">12 - Dec</option>\n)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册