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
Support grouped collection for collection_select
#41562
base: main
Are you sure you want to change the base?
Support grouped collection for collection_select
#41562
Conversation
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
🦄 |
0787276
to
5467523
Compare
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
5467523
to
483d5ed
Compare
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
This adds support for grouped collections to the `collection_select` helper, much like the `select` helper: ```ruby collection_select :country_id, @countries.group_by(&:continent_name), :id, :name ``` This differs from `grouped_collection_select` in that it does not require a secondary model to group options: ```ruby grouped_collection_select :country_id, @continents, :countries, :name, :id, :name ``` Of course, a secondary model can be used as well: ```ruby collection_select :country_id, @countries.group_by { |country| country.continent.name }, :id, :name ```
483d5ed
to
400c1c3
Compare
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
collection_select
def collection_grouped? | ||
entry = @collection.first if @collection.is_a?(Hash) || @collection.is_a?(Array) | ||
entry.respond_to?(:last) && entry.last.is_a?(Array) | ||
end |
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.
This is almost equivalent to the implementation for select
:
rails/actionview/lib/action_view/helpers/tags/select.rb
Lines 36 to 38 in 436207a
def grouped_choices? | |
!@choices.blank? && @choices.first.respond_to?(:last) && Array === @choices.first.last | |
end |
What do you think of matching it exactly? My concern is consistent edge-case behaviour, e.g. passing a set of arrays of arrays will work for select
, but won't work 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.
Sure! The reason I limited this behavior to Hash
and Array
was because I did not realize I could use blank?
to ensure that an AR relation was loaded. I had assumed blank?
would execute a limited query, just like empty?
. It looks like this behavior was explicitly implemented in #5461 (and tests were added in #5469), so I will change this method to mirror Select
. 👍
option_tags_options = { | ||
selected: @options.fetch(:selected) { value }, | ||
disabled: @options[:disabled] | ||
} |
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.
These lines are currently (almost) identical to their counterparts in the Select
tag helper class:
rails/actionview/lib/action_view/helpers/tags/select.rb
Lines 17 to 20 in 436207a
option_tags_options = { | |
selected: @options.fetch(:selected) { value.nil? ? "" : value }, | |
disabled: @options[:disabled] | |
} |
I don't think we should refactor them, and hide the equivalence, without a good reason.
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.
Yes, I noticed the same pattern in GroupedCollectionSelect
, too.
In #44335, I factored this out into a SelectRenderer
module. (So Select
would look like this, and CollectionSelect
would match.) I was planning to merge that PR first, and then rebase this PR on top of it. Any objections?
This adds support for grouped collections to the
collection_select
helper, much like theselect
helper:This differs from
grouped_collection_select
in that it does not require a secondary model to group options:Of course, a secondary model can be used as well:
This finishes #40338, which can't be reopened because
master
branch has been deleted.