Use the first non-empty object to detect the label and value method for

grouped select

Fixes #978
This commit is contained in:
Rafael Mendonça França 2014-04-02 15:38:40 -03:00
parent 44e807feaa
commit 33418a9d03
3 changed files with 20 additions and 1 deletions

View File

@ -21,6 +21,7 @@
* Collection input that uses automatic collection translation properly sets checked values.
Closes [#971](https://github.com/plataformatec/simple_form/issues/971) [@nashby](https://github.com/nashby)
* Collection input generates `required` attribute if it has `prompt` option. [@nashby](https://github.com/nashby)
* Grouped collection uses the first non-empty object to detect label and value methods.
## deprecation
* Methods on custom inputs now accept a required argument with the wrapper options.

View File

@ -22,7 +22,7 @@ module SimpleForm
# Sample collection
def collection
@collection ||= grouped_collection.first.try(:send, group_method) || []
@collection ||= grouped_collection.map { |collection| collection.try(:send, group_method) }.detect(&:present?) || []
end
def group_method

View File

@ -97,6 +97,24 @@ class GroupedCollectionSelectInputTest < ActionView::TestCase
end
end
test 'grouped collections finds the default label method from the first non-empty object' do
Agent = Struct.new(:id, :name)
agents = [["First", []], ["Second", [Agent.new(7, 'Bond'), Agent.new(47, 'Hitman')]]]
with_input_for @user, :tag_ids, :grouped_select,
collection: agents,
group_label_method: :first,
group_method: :last,
include_blank: false
assert_select 'select.grouped_select#user_tag_ids' do
assert_select 'optgroup[label=Second]' do
assert_select 'option[value=7]', 'Bond'
assert_select 'option[value=47]', 'Hitman'
end
end
end
test 'grouped collection accepts label and value methods options' do
with_input_for @user, :tag_ids, :grouped_select,
collection: { 'Authors' => ['Jose', 'Carlos'] },