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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -14,16 +14,24 @@ def initialize(object_name, method_name, template_object, collection, value_meth | |||||||
end | ||||||||
|
||||||||
def render | ||||||||
option_tags_options = { | ||||||||
selected: @options.fetch(:selected) { value }, | ||||||||
disabled: @options[:disabled] | ||||||||
} | ||||||||
|
||||||||
select_content_tag( | ||||||||
options_from_collection_for_select(@collection, @value_method, @text_method, option_tags_options), | ||||||||
@options, @html_options | ||||||||
) | ||||||||
select_content_tag(option_tags, @options, @html_options) | ||||||||
end | ||||||||
|
||||||||
private | ||||||||
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 | ||||||||
Comment on lines
+21
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is almost equivalent to the implementation for rails/actionview/lib/action_view/helpers/tags/select.rb Lines 36 to 38 in 436207a
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! The reason I limited this behavior to |
||||||||
|
||||||||
def option_tags | ||||||||
html_options = { selected: @options.fetch(:selected) { value }, disabled: @options[:disabled] } | ||||||||
|
||||||||
if collection_grouped? | ||||||||
option_groups_from_collection_for_select(@collection, :last, :first, @value_method, @text_method, html_options) | ||||||||
else | ||||||||
options_from_collection_for_select(@collection, @value_method, @text_method, html_options) | ||||||||
end | ||||||||
end | ||||||||
end | ||||||||
end | ||||||||
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.
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
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. (SoSelect
would look like this, andCollectionSelect
would match.) I was planning to merge that PR first, and then rebase this PR on top of it. Any objections?