This project is archived and is in readonly mode.
Added grouped_options_for_select helper to form_options_helper for wrapping option tags in optgroups.
-
Mislav
+1
I would like to see better naming:
groups = ['North America','Europe'] options = [, ] grouped_options_for_select(groups, options) # "option_groups_for_select" is also a good method nameAnd how about this:
grouped_options_for_select( 'North America' => ['United States','Canada'], 'Europe' => ['Denmark','Germany','France'] ) # => iterates over keys in alphabetical order ("Europe" would come first) -
Mislav
I ruined the 2nd line of my example by using cut instead of copy :/
-
Jon
I replied to some of Mislav's thoughts in the IRC channel but I'll repeat here.
I avoided the
optionsargument name because it is often used in many other helpers to mean an option set of arguments that may be passed. And I used thelabelsargument name because of the attribute in<optgroup label="Europe''>. I could easily budge on thelabelsone, but I personally think theoptionsshould be avoided. Note:options_for_selectusescontainer.And to your point about using a hash and iterating in alphabetical order: In my very first implementation of this helper, I needed the groups in a specific order ("Mens, Womens, Children, Other"). Using alphabetical sorting means you lose a good deal of control (although it's much cleaner on first blush).
-
Eloy Duran
+1
-
Mislav
Jon,
Re: hash argument and alphabetical order---I've only suggested that this could be an alternative to passing 2 arrays. Array arguments should be used when the user is in need of custom ordering.
-
Tom Lea
The two arrays required to be equal length is a bit of a no no for me, how about accepting:
labels = ['North America',] groups = [ ['North America', ['United States','Canada']], ['Europe', ['Denmark','Germany','France']] ] grouped_options_for_select(labels,groups)This accepts things in order and will keep them in order, ala Jon's requirements.
Code designed for this should be able to handle a hash too (
[[:a, :b]].each{|c| break c} == {:a => :b}.each{|c| break c}), as per Mislav's ideas.This also avoids the slightly icky
Error: Sizes don't match, as this is now an impossible state to reach.There is no basic issue with the idea, it's good, but needs some more work on the method signature, just does not feel railsy enough.
Any chance of an updated patch Jon?
-
Tom Lea
1's example is wrong... should be
grouped_options = [ ['North America', ['United States','Canada']], ['Europe', ['Denmark','Germany','France']] ] grouped_options_for_select(grouped_options) -
Jon
Tom-
I made the changes you suggested. I never liked that error at the beginning, but I didn't want to raise an exception either. Maybe I'm too nice? :-D
It now accepts either a nested Array or a Hash.
I also changed the helper name to
grouped_options_for_select.Let me know if you guys have any other suggestions.
-
Jon
- Title changed from [PATCH] Added optgroups_with_options_for_select helper to form_options_helper for wrapping option tags in optgroups. to [PATCH] Added grouped_options_for_select helper to form_options_helper for wrapping option tags in optgroups.
Further updated patch including better testing and improved documentation.
-
grosser
- Tag changed from actionpack, enhancement, helper, patch, tested to actionpack, enhancement, helper, patch, tested
What i am using atm:
continents = [['Africa',['Norway','NO']]] select_tag('country',option_groups_for_select(continents,'NO'))
#continentes = [['Africa',['NA','Namibia'],['XX']...],...] def option_groups_for_select(collection,selected = nil)
collection.inject('') do |options_for_select,group| group_label_string = group[0] options_for_select = "<optgroup label=\"#{html_escape(group_label_string)}\">" options_for_select += options_for_select(group[1..-1],selected) options_for_select += '</optgroup>' endend
-
grosser
once again(weird formattting)...
#continentes = [['Africa',['NA','Namibia'],['XX']...],...] def option_groups_for_select(collection,selected = nil) collection.inject('') do |options_for_select,group| group_label_string = group[0] options_for_select = "<optgroup label=\"#{html_escape(group_label_string)}\">" options_for_select += options_for_select(group[1..-1],selected) options_for_select += '</optgroup>' end end -
Sean Cribbs
+1
-
grosser.michael+lighthouse (at gmail)
There was a small errror in my above post:
options_for_select = "<optgroup label=\"#{html_escape(group_label_string)}\">"should be
options_for_select += "<optgroup label=\"#{html_escape(group_label_string)}\">" -
Pratik
- Assigned user set to Pratik
- Title changed from [PATCH] Added grouped_options_for_select helper to form_options_helper for wrapping option tags in optgroups. to Added grouped_options_for_select helper to form_options_helper for wrapping option tags in optgroups.
-
Pratik
- Milestone cleared.
-
Repository
- State changed from new to resolved
(from [8761663a68bd7ddd918f78fb3def4697784024f2]) Added grouped_options_for_select helper method for wrapping option tags in optgroups. [#977 Added grouped_options_for_select helper to form_options_helper for wrapping o... state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
