diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 30dbc20f184d9b2f0d4df07f705c792c994bfcc4..a0f298a6b102bb95718786b5ab1916f2b76aafad 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,17 @@ +* 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} (%02d)' + + for example. + + This option is motivated by #13618. + + *Xavier Noria* + * Added `config.action_view.raise_on_missing_translations` to define whether an error should be raised for missing translations. diff --git a/actionview/lib/action_view/helpers/date_helper.rb b/actionview/lib/action_view/helpers/date_helper.rb index 3d091c4a001641c8858679df75bef7f531c8b960..698f0ca31c1ed9236f1ebd12091c371463399c70 100644 --- a/actionview/lib/action_view/helpers/date_helper.rb +++ b/actionview/lib/action_view/helpers/date_helper.rb @@ -169,6 +169,9 @@ def time_ago_in_words(from_time, include_seconds_or_options = {}) # "2 - February" instead of "February"). # * :use_month_names - 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. + # * :month_format_string - Set to a format string. The string gets passed keys +:number+ (integer) + # and +:name+ (string). A format string would be something like "%{name} (%02d)" for example. + # See Kernel.sprintf for documentation on format sequences. # * :date_separator - Specifies a string to separate the date fields. Default is "" (i.e. nothing). # * :start_year - Set the start year for the year select. Default is Date.today.year - 5if # you are creating new record. While editing existing record, :start_year defaults to @@ -850,24 +853,36 @@ def translated_month_names I18n.translate(key, :locale => @options[:locale]) end - # Lookup month name for number. - # month_name(1) => "January" + # Looks up month names by number (1-based): # - # If :use_month_numbers option is passed - # month_name(1) => 1 + # month_name(1) # => "January" # - # If :use_two_month_numbers option is passed - # month_name(1) => '01' + # If the :use_month_numbers option is passed: # - # If :add_month_numbers option is passed - # month_name(1) => "1 - January" + # month_name(1) # => 1 + # + # If the :use_two_month_numbers option is passed: + # + # month_name(1) # => '01' + # + # If the :add_month_numbers option is passed: + # + # month_name(1) # => "1 - January" + # + # If the :month_format_string option is passed: + # + # month_name(1) # => "January (01)" + # + # depending on the format string. def month_name(number) if @options[:use_month_numbers] number elsif @options[:use_two_digit_numbers] - sprintf "%02d", number + '%02d' % number elsif @options[:add_month_numbers] "#{number} - #{month_names[number]}" + elsif format_string = @options[:month_format_string] + format_string % {number: number, name: month_names[number]} else month_names[number] end diff --git a/actionview/test/template/date_helper_test.rb b/actionview/test/template/date_helper_test.rb index 5f09aef249ff8cea0ab970a84a88ee88fa86e2b4..6f77c3c99d180006ad5dcd961d47b5cc6a01ee09 100644 --- a/actionview/test/template/date_helper_test.rb +++ b/actionview/test/template/date_helper_test.rb @@ -326,6 +326,16 @@ def test_select_month_with_numbers_and_names assert_dom_equal expected, select_month(8, :add_month_numbers => true) end + def test_select_month_with_format_string + expected = %(\n" + + format_string = '%{name} (%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 expected = %(