Skip to content

Conversation

@DRBragg
Copy link
Contributor

@DRBragg DRBragg commented Aug 9, 2021

Summary

In a majority of the Rails apps I've built I've had to add my own helpers for a weekday select. Given how great Rails is at providing most of the helpers you could ever want, this omission has always been a little surprising to me. Since I just had to work on this again, and another dev shared my surprise at the omission, I thought it was time for a Rails PR.

This PR adds 2 new FormOptionHelper methods, a FormBuilder method, and a new Tags::WeekdaySelect class.

weekday_options_for_select
# => "<option value=\"Sunday\">Sunday</option>\n<option value=\"Monday\">Monday</option>\n
# <option value=\"Tuesday\">Tuesday</option>\n<option value=\"Wednesday\">Wednesday</option>\n
# <option value=\"Thursday\">Thursday</option>\n<option value=\"Friday\">Friday</option>\n
# <option value=\"Saturday\">Saturday</option>"

In addition to accepting a selected value, weekday_options_for_select can have 2 options, :index_as_value and/or :day_format.

weekday_options_for_select(nil, day_format: :abbr_day_names)
# => "<option value=\"Sun\">Sun</option>\n<option value=\"Mon\">Mon</option>\n
# <option value=\"Tue\">Tue</option>\n<option value=\"Wed\">Wed</option>\n
# <option value=\"Thu\">Thu</option>\n<option value=\"Fri\">Fri</option>\n
# <option value=\"Sat\">Sat</option>"

weekday_options_for_select(nil, index_as_value: true)
# => "<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>"

weekday_options_for_select is really just a helper method for:

weekday_select(:model, :weekday)
# => "<select name=\"model[weekday]\" id=\"model_weekday\"><option value=\"Sunday\">Sunday</option>\n
# <option value=\"Monday\">Monday</option>\n<option value=\"Tuesday\">Tuesday</option>\n
# <option value=\"Wednesday\">Wednesday</option>\n<option value=\"Thursday\">Thursday</option>\n
# <option value=\"Friday\">Friday</option>\n<option value=\"Saturday\">Saturday</option></select>"

weekday_select is also used in the FormBuilder so can use it like this:

<%= form_for @digest do |f| %>
  <%= f.weekday_select :weekday %>
  <%= f.submit %>
<% end %>

to produce the following HTML:

<select name="digest[weekday]" id="digest_weekday">
  <option value="Sunday">Sunday</option>
  <option value="Monday">Monday</option>
  <option value="Tuesday">Tuesday</option>
  <option value="Wednesday">Wednesday</option>
  <option value="Thursday">Thursday</option>
  <option value="Friday">Friday</option>
  <option value="Saturday">Saturday</option>
</select>

@rails-bot rails-bot bot added the actionview label Aug 9, 2021
Copy link
Contributor

@kaspth kaspth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Some more comments and then squash the commits down to 1 and we're good to go.

@DRBragg DRBragg force-pushed the drbragg/add-weekday-select branch from bbeeb1f to 2633211 Compare August 16, 2021 16:53
Add `weekday_select` method

Create `Tags::WeekdaySelect` class

Add `weekday_select` to `FromBuilder`

Add Documentation

Allow `WeekdaySelect` to use selected option if value is nil

Doc fix

Add tests

Use kwrd args

Update CHANGELOG

Fix `Tags::WeekdaySelect` for updated kwrd args

Update CHANGELOG format

Condense `weekday_options_for_select` method

Update tests for kwargs
@DRBragg DRBragg force-pushed the drbragg/add-weekday-select branch from 2633211 to 592570f Compare August 16, 2021 17:05
@kaspth kaspth merged commit 13a714f into rails:main Aug 16, 2021
@kaspth
Copy link
Contributor

kaspth commented Aug 16, 2021

Sweet, thanks!

@acetinick
Copy link

Hi, very handy PR. I think it’s more of a US thing to have Sunday start of week, could we extend this to allow Monday to be the first index?

@DRBragg
Copy link
Contributor Author

DRBragg commented Aug 17, 2021

Hi, very handy PR. I think it’s more of a US thing to have Sunday start of week, could we extend this to allow Monday to be the first index?

Since this leverages the arrays in active_support/locale/en.yml, would you find it reasonable to reorder those arrays?

ie:

en:
  date:
    day_names: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
    abbr_day_names: [Mon, Tue, Wed, Thu, Fri, Sat, Sun]

@acetinick
Copy link

This would work for if all users where from same region, but in our application for example we have users globally, so formats are dynamic based on regional settings

@DRBragg
Copy link
Contributor Author

DRBragg commented Aug 17, 2021

This would work for if all users where from same region, but in our application for example we have users globally, so formats are dynamic based on regional settings

Ah, I see what you're saying. Actually, I don't think my suggestion would have worked anyway because in Ruby "Sunday" is always at 0...

@acetinick
Copy link

Ahh yep that too :)

@DRBragg
Copy link
Contributor Author

DRBragg commented Aug 17, 2021

@acetinick What do you think about this? We can add another option to weekday_options_for_select called beginning_of_week which would default to :sunday. If you pass in :monday it will rotate the array of day names so that "Monday" is the first day in the list. If you are using indexes as values Sundays value will remain 0 for consistency with Ruby and the I18n day names array.

@acetinick
Copy link

acetinick commented Aug 17, 2021

Hi @DRBragg yep I think that would work great, and will cover other edge case scenarios. Default of Sunday makes sense :)

@DRBragg
Copy link
Contributor Author

DRBragg commented Aug 21, 2021

@acetinick how's this #43037 work for you?

@spacerobotTR
Copy link

Is there a way to select multiple values with this? If using select2 or other similar plugins? I tried multiple: true but it doesn't seem to be working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants