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

Support grouped collection for collection_select #41562

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jonathanhefner
Copy link
Member

This adds support for grouped collections to the collection_select helper, much like the select helper:

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:

grouped_collection_select :country_id, @continents, :countries, :name, :id, :name

Of course, a secondary model can be used as well:

collection_select :country_id, @countries.group_by { |country| country.continent.name }, :id, :name

This finishes #40338, which can't be reopened because master branch has been deleted.

@rails-bot rails-bot bot added the actionview label Feb 26, 2021
@rails-bot
Copy link

rails-bot bot commented May 27, 2021

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.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label May 27, 2021
@zzak
Copy link
Member

zzak commented May 27, 2021

🦄

@rails-bot rails-bot bot removed the stale label May 27, 2021
@jonathanhefner jonathanhefner force-pushed the collection_select-grouped-collection branch from 0787276 to 5467523 Compare July 19, 2021 16:45
@rails-bot
Copy link

rails-bot bot commented Oct 17, 2021

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.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Oct 17, 2021
@jonathanhefner jonathanhefner force-pushed the collection_select-grouped-collection branch from 5467523 to 483d5ed Compare October 18, 2021 14:46
@rails-bot rails-bot bot removed the stale label Oct 18, 2021
@rails-bot
Copy link

rails-bot bot commented Jan 16, 2022

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.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Jan 16, 2022
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
```
@jonathanhefner jonathanhefner force-pushed the collection_select-grouped-collection branch from 483d5ed to 400c1c3 Compare January 20, 2022 21:59
@rails-bot rails-bot bot removed the stale label Jan 20, 2022
@rails-bot
Copy link

rails-bot bot commented Apr 20, 2022

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.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Apr 20, 2022
@rails-bot rails-bot bot closed this Apr 27, 2022
@rails-bot rails-bot bot removed the stale label Apr 28, 2022
@jonathanhefner jonathanhefner changed the title Support grouped collection for collection_select Support grouped collection for collection_select Apr 28, 2022
Comment on lines +21 to +24
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
Copy link
Member

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:

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.

Copy link
Member Author

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. 👍

Comment on lines -17 to -20
option_tags_options = {
selected: @options.fetch(:selected) { value },
disabled: @options[:disabled]
}
Copy link
Member

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:

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.

Copy link
Member Author

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?

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.

None yet

3 participants