Skip to content
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

Add basic select helper #260

Merged
merged 10 commits into from
Apr 10, 2021
Merged

Add basic select helper #260

merged 10 commits into from
Apr 10, 2021

Conversation

peteryates
Copy link
Member

@peteryates peteryates commented Apr 8, 2021

Currently users aren't given much flexibility by #govuk_collection_select, which wraps Rails' #collection_select. This change introduces a #govuk_select helper that wraps #select and, as a result, offers the following advantages:

  • allows the provision of options via any array
  • supports Rails' #options_for_select
  • allows grouped select boxes
  • lets users provide their options using a block.

Remaining tasks

  • Rename GOVUKDesignSystemFormbuilder::Elements::CollectionSelect to GOVUKDesignSystemFormbuilder::Elements::Select
  • Flesh out a basic helper and functionality for #govuk_select
  • Add and test block support
  • Add some more comprehensive tests (ie covering #options_for_select)
  • Create RDoc documentation
  • Add an entry to the guide

This is intended to make way for a new simpler Select class that will
implement roughly what Rails' `f.select` does. It's a bit more-flexible
than `f.collection_select`, which we're wrapping here.
The intention is to provide a more-flexible option than
`#govuk_collection_select` that will support

* setting the options via a block (at the expense of supplementary
  content)
* grouped selects
* configuring with an arbitrary array of values
* being able to utilise Rails' `#options_for_select` for more control
  over the attributes of the contained options

https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select
This isn't the nicest approach as there's some repetition, but it
confirms that dealing with blocks of options works correctly.
When building the options content the `#select` helper checks if the
contents are groupable with:

  @Choices.first.respond_to?(:last)

This failed because without ActiveSupport::CoreExtensions::String being
included, an ActiveSupport::SafeBuffer has no `#first` method and the
tests blow up.
@peteryates peteryates marked this pull request as ready for review April 10, 2021 09:59
@peteryates peteryates merged commit 15e449d into master Apr 10, 2021
@peteryates peteryates deleted the add-govuk-select-helper branch April 10, 2021 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant