Skip to content

Commit

Permalink
Merge pull request heartcombo#1775 from cgunther/grouped-collection-s…
Browse files Browse the repository at this point in the history
…elect-group-method-proc

Allow proc as group_method for grouped collection select
  • Loading branch information
nashby authored Jul 8, 2022
2 parents 2320520 + adea6b5 commit 3978722
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Add support for Rails 7.0 and Ruby 3.1 (no changes required)
* Fix escaping issue on boolean input with `include_hidden: false` and custom wrapper.
* Update Bootstrap install generator version 5. [@mhw](https://github.com/mhw)
* Accept proc as `group_method` for grouped collection select

## 5.1.0

Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/inputs/grouped_collection_select_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def grouped_collection

# Sample collection
def collection
@collection ||= grouped_collection.map { |collection| collection.try(:send, group_method) }.detect(&:present?) || []
@collection ||= grouped_collection.map { |collection| group_method.respond_to?(:call) ? group_method.call(collection) : collection.try(:send, group_method) }.detect(&:present?) || []
end

def group_method
Expand Down
13 changes: 13 additions & 0 deletions test/inputs/grouped_collection_select_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ class GroupedCollectionSelectInputTest < ActionView::TestCase
end
end

test 'grouped collection allows overriding group_method using a lambda' do
with_input_for @user, :tag_ids, :grouped_select,
collection: { Authors: %w[Jose Carlos] },
group_method: ->(i) { i.last }

assert_select 'select.grouped_select#user_tag_ids' do
assert_select 'optgroup[label=Authors]' do
assert_select 'option[value=Jose]', 'Jose'
assert_select 'option[value=Carlos]', 'Carlos'
end
end
end

test 'grouped collection accepts group_label_method option' do
with_input_for @user, :tag_ids, :grouped_select,
collection: { %w[Jose Carlos] => 'Authors' },
Expand Down

0 comments on commit 3978722

Please sign in to comment.