Merge pull request #1016 from plataformatec/rm-fix-grouped-select
Fix grouped collection value/label method automatic detection
This commit is contained in:
commit
762386912a
|
@ -22,6 +22,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.
|
||||
|
|
|
@ -70,13 +70,17 @@ module SimpleForm
|
|||
elsif collection_includes_basic_objects?(collection_classes)
|
||||
{ label: :to_s, value: :to_s }
|
||||
else
|
||||
sample = collection.first || collection.last
|
||||
|
||||
{ label: SimpleForm.collection_label_methods.find { |m| sample.respond_to?(m) },
|
||||
value: SimpleForm.collection_value_methods.find { |m| sample.respond_to?(m) } }
|
||||
detect_method_from_class(collection_classes)
|
||||
end
|
||||
end
|
||||
|
||||
def detect_method_from_class(collection_classes)
|
||||
sample = collection.first || collection.last
|
||||
|
||||
{ label: SimpleForm.collection_label_methods.find { |m| sample.respond_to?(m) },
|
||||
value: SimpleForm.collection_value_methods.find { |m| sample.respond_to?(m) } }
|
||||
end
|
||||
|
||||
def detect_collection_classes(some_collection = collection)
|
||||
some_collection.map { |e| e.class }.uniq
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
@ -39,6 +39,15 @@ module SimpleForm
|
|||
|
||||
label
|
||||
end
|
||||
|
||||
def detect_method_from_class(collection_classes)
|
||||
return {} if collection_classes.empty?
|
||||
|
||||
sample = collection_classes.first
|
||||
|
||||
{ label: SimpleForm.collection_label_methods.find { |m| sample.instance_methods.include?(m) },
|
||||
value: SimpleForm.collection_value_methods.find { |m| sample.instance_methods.include?(m) } }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -79,6 +79,42 @@ class GroupedCollectionSelectInputTest < ActionView::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
test 'grouped collection finds default label methods on the group objects' do
|
||||
option_list = ['Jose', 'Carlos']
|
||||
|
||||
GroupedClass = Struct.new(:to_label, :options)
|
||||
group = GroupedClass.new("Authors", option_list)
|
||||
|
||||
with_input_for @user, :tag_ids, :grouped_select,
|
||||
collection: [group],
|
||||
group_method: :options
|
||||
|
||||
assert_select 'select.grouped_select#user_tag_ids' do
|
||||
assert_select 'optgroup[label=Authors]' do
|
||||
assert_select 'option', 'Jose'
|
||||
assert_select 'option', 'Carlos'
|
||||
end
|
||||
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'] },
|
||||
|
|
Loading…
Reference in New Issue