-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Add beginning_of_week
option to weekday_options_for_select
#43037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
In Active Support Date has a beginning_of_week method. |
Yea, I had thought about using that but thought it wasn't worth having to handle all the days of the week. Maybe that isn't an issue and we should just handle/allow for any day? I just wasn't sure why you would want to do that but I also didn't think about Saturday. |
@p8 What would you think about only handling Here's basically what I'm thinking: def weekday_options_for_select(selected = nil, index_as_value: false, day_format: :day_names, beginning_of_week: Date.beginning_of_week)
rotations = { sunday: 0, monday: 1, saturday: -1 }
day_names = I18n.translate("date.#{day_format}")
day_names = day_names.map.with_index.to_a if index_as_value
day_names = day_names.rotate(rotations.fetch(beginning_of_week, 1))
options_for_select(day_names, selected)
end Or something like that |
2873ea7
to
9694317
Compare
@p8 I made the change, let me know what you think. |
11c5e62
to
8400ee8
Compare
begin_on_monday
option to weekday_options_for_select
beginning_of_week
option to weekday_options_for_select
Ping or bump (not sure what the SOP is here) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sweet, gotta have the commits squashed too! The test failures look unrelated.
# * <tt>:beginning_of_week</tt> - Defaults to Date.beginning_of_week, accepts | ||
# :sunday, :monday, or :saturday values to set the first day on the list. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We support all days now, and can just omit these specific references:
# * <tt>:beginning_of_week</tt> - Defaults to Date.beginning_of_week, accepts | |
# :sunday, :monday, or :saturday values to set the first day on the list. | |
# * <tt>:beginning_of_week</tt> - Defaults to Date.beginning_of_week. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call
Change `begin_on_monday` to `beginning_of_week` Change `begin_on_monday` to `beginning_of_week` Better handle ignored day Remove inaccurate tests Improve docs and use DAYS_INTO_WEEK to calc rotation Remove inaccurate docs
aa0a540
to
451c55e
Compare
The Action View builds passed and the failures were on main in components that this didn't touch. Thanks so much @DRBragg, dig how this turned out 🙌 |
weekday_options_for_select | ||
) | ||
end | ||
|
||
def test_weekday_options_for_select_with_index_as_value | ||
assert_dom_equal( | ||
"<option value=\"0\">Sunday</option>\n<option value=\"1\">Monday</option>\n<option value=\"2\">Tuesday</option>\n<option value=\"3\">Wednesday</option>\n<option value=\"4\">Thursday</option>\n<option value=\"5\">Friday</option>\n<option value=\"6\">Saturday</option>", | ||
"<option value=\"1\">Monday</option>\n<option value=\"2\">Tuesday</option>\n<option value=\"3\">Wednesday</option>\n<option value=\"4\">Thursday</option>\n<option value=\"5\">Friday</option>\n<option value=\"6\">Saturday</option>\n<option value=\"0\">Sunday</option>", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's is a little odd but since Date.beginning_of_week
defaults to Monday in Rails it's what works best in a Rails context. If you set config.beginning_of_week = :sunday
in your config file this will default to Sunday instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DRBragg Right, but if Date.beginning_of_week
defaults to Monday, that explains why "Monday" is the first <option>
in the select but doesn't explain why "Sunday" has the 0 index, and "Monday" is 1. Is it a bug? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sunday is 0 because I18n.translate("date.day_names")
returns an array where Sunday is the first item, Date::DAYS_INTO_WEEK
returns a hash where the value of the key :sunday
is 0, and in Ruby calling wday
on a date that is a Sunday will return 0. I'm not 100% why Date.beginning_of_week
defaults to Monday
Summary
In #42979 the suggestion was made to include an option to allow users to set "Monday" as the first day on the list.
NOTE: If you are using indexes as values Sundays value will remain 0 for consistency with Ruby and the I18n day names array.