Grouped collection input #382

Merged
merged 3 commits into from Jan 24, 2012

3 participants

@semaperepelitsa

I wanted to add grouped collection input so that you could do f.association :category, :collection => Category.roots, :group_method => :children. Rails has grouped_collection_select alongside with collection_select so it should be pretty similar to existing collection input.

The only thing I don't know how to deal with is how SimpleForm maps inputs to classes. Right now every input with collection option maps to select input type which maps to CollectionInput. I wish every input with group_method option map to the same select input type but also somehow map it to GroupedCollectionInput.

Do you like my idea and do you have any suggestions on the problem? Thanks.

@josevalim
Plataformatec member

Nice. I would just recommend that instead of checking :group_method, we could simply use :as to specify it:

f.input :authors, :as => :group_select, :collection => some_array

And then instead of having :group_method, we would simply use :value_method and :label_method which are already the official API used by collections.

@rafaelfranca rafaelfranca commented on an outdated diff Dec 5, 2011
lib/simple_form/inputs/grouped_collection_input.rb
@@ -0,0 +1,27 @@
+module SimpleForm
+ module Inputs
+ class GroupedCollectionInput < CollectionInput
+ def input
+ group_label_method, label_method, value_method = detect_collection_methods
+ @builder.send(:"grouped_collection_#{input_type}", attribute_name, collection,

We don't need this input_type here, neither send. You can call only

@builder.grouped_collection_select(attribute_name, collection,
                                   group_method, group_label_method, value_method, label_method,
                                   input_options, input_html_options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@semaperepelitsa

Good point, I will do that. Do you think group_select is a good name? I would call it grouped_select because you can't select groups actually and also Rails calls it grouped_collection_select.

semaperepelitsa added some commits Dec 6, 2011
@semaperepelitsa semaperepelitsa Map grouped_select, refactoring class so that method detections work
We need to detect label for group collection separately from child collections because they are probably different
96790f6
@semaperepelitsa semaperepelitsa Group label is now detected ab7cd70
@josevalim
Plataformatec member
@carlosantoniodasilva carlosantoniodasilva merged commit 5b2f067 into plataformatec:v1.5 Jan 24, 2012
@carlosantoniodasilva carlosantoniodasilva added a commit that referenced this pull request Jan 24, 2012
@carlosantoniodasilva carlosantoniodasilva Revert "Merge pull request #382 from semaperepelitsa/grouped_collection"
This reverts commit 5b2f067, reversing
changes made to 9ff998f.

This is going to be applied to master only, sorry.
c7c8461
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment