diff --git a/actionview/lib/action_view/helpers/form_options_helper.rb b/actionview/lib/action_view/helpers/form_options_helper.rb index d1936cb227..de2e36373d 100644 --- a/actionview/lib/action_view/helpers/form_options_helper.rb +++ b/actionview/lib/action_view/helpers/form_options_helper.rb @@ -597,15 +597,18 @@ module ActionView # Returns a string of option tags for the days of the week. # # Options: - # * :index_as_value - Defaults to false, set to true to use the index of the weekday as the value. + # * :index_as_value - Defaults to false, set to true to use the indexes from + # `I18n.translate("date.day_names")` as the values. By default, Sunday is always 0. # * :day_format - The I18n key of the array to use for the weekday options. # Defaults to :day_names, set to :abbr_day_names for abbreviations. + # * :beginning_of_week - Defaults to Date.beginning_of_week. # # NOTE: Only the option tags are returned, you have to wrap this call in # a regular HTML select tag. - def weekday_options_for_select(selected = nil, index_as_value: false, day_format: :day_names) + def weekday_options_for_select(selected = nil, index_as_value: false, day_format: :day_names, beginning_of_week: Date.beginning_of_week) day_names = I18n.translate("date.#{day_format}") - day_names = day_names.map.with_index.to_h if index_as_value + day_names = day_names.map.with_index.to_a if index_as_value + day_names = day_names.rotate(Date::DAYS_INTO_WEEK.fetch(beginning_of_week)) options_for_select(day_names, selected) end diff --git a/actionview/lib/action_view/helpers/tags/weekday_select.rb b/actionview/lib/action_view/helpers/tags/weekday_select.rb index cb7279c429..f7efe25e27 100644 --- a/actionview/lib/action_view/helpers/tags/weekday_select.rb +++ b/actionview/lib/action_view/helpers/tags/weekday_select.rb @@ -15,7 +15,8 @@ module ActionView weekday_options_for_select( value || @options[:selected], index_as_value: @options.fetch(:index_as_value, false), - day_format: @options.fetch(:day_format, :day_names) + day_format: @options.fetch(:day_format, :day_names), + beginning_of_week: @options.fetch(:beginning_of_week, Date.beginning_of_week) ), @options, @html_options diff --git a/actionview/test/template/form_options_helper_test.rb b/actionview/test/template/form_options_helper_test.rb index 93e6efce3e..52c49aba69 100644 --- a/actionview/test/template/form_options_helper_test.rb +++ b/actionview/test/template/form_options_helper_test.rb @@ -1510,42 +1510,65 @@ class FormOptionsHelperTest < ActionView::TestCase def test_weekday_options_for_select_with_no_params assert_dom_equal( - "\n\n\n\n\n\n", + "\n\n\n\n\n\n", weekday_options_for_select ) end def test_weekday_options_for_select_with_index_as_value assert_dom_equal( - "\n\n\n\n\n\n", + "\n\n\n\n\n\n", weekday_options_for_select(index_as_value: true) ) end def test_weekday_options_for_select_with_abberviated_day_names assert_dom_equal( - "\n\n\n\n\n\n", + "\n\n\n\n\n\n", weekday_options_for_select(day_format: :abbr_day_names) ) end + def test_weekday_options_for_select_with_beginning_of_week_set_to_sunday + assert_dom_equal( + "\n\n\n\n\n\n", + weekday_options_for_select(beginning_of_week: :sunday) + ) + end + + def test_weekday_options_for_select_with_beginning_of_week_set_to_saturday + assert_dom_equal( + "\n\n\n\n\n\n", + weekday_options_for_select(beginning_of_week: :saturday) + ) + end + + def test_weekday_options_for_select_with_beginning_of_week_set_elsewhere + Date.beginning_of_week = :sunday + assert_dom_equal( + "\n\n\n\n\n\n", + weekday_options_for_select + ) + Date.beginning_of_week = :monday + end + def test_weekday_options_for_select_with_selected_value assert_dom_equal( - "\n\n\n\n\n\n", + "\n\n\n\n\n\n", weekday_options_for_select("Friday") ) end def test_weekday_select assert_dom_equal( - "", + "", weekday_select(:model, :weekday) ) end def test_weekday_select_with_selected_value assert_dom_equal( - "", + "", weekday_select(:model, :weekday, selected: "Friday") ) end @@ -1558,7 +1581,7 @@ class FormOptionsHelperTest < ActionView::TestCase end assert_dom_equal( - "", + "", output_buffer ) end @@ -1572,7 +1595,7 @@ class FormOptionsHelperTest < ActionView::TestCase end assert_dom_equal( - "", + "", output_buffer ) end