From cd707924e27acef70db1599d65972f5efc4c29c2 Mon Sep 17 00:00:00 2001 From: Jon Crawford Date: Fri, 5 Sep 2008 01:50:38 -0500 Subject: [PATCH] Added optgroups_with_options_for_select helper to form_options_helper for wrapping option tags in optgroups. --- .../lib/action_view/helpers/form_options_helper.rb | 46 +++++++++++++++++++- .../test/template/form_options_helper_test.rb | 29 ++++++++++++ 2 files changed, 74 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 9aae945..2873d08 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -268,7 +268,51 @@ module ActionView options_for_select += '' end end - + + # Returns a string of tags, like options_from_collection_for_select, but + # wraps them with tags. + # + # Parameters: + # * +labels+ - An array of strings representing the text. + # * +options+ - An array of arrays each containing the options for a corresponding optgroup. + # * +selected_key+ - A value equal to the +value+ attribute for one of the tags, + # which will have the +selected+ attribute set. Corresponds to the return value of one of the calls + # to +option_key_method+. If +nil+, no selection is made. + # * +prompt+ - set to true or a prompt string. When the select element doesn’t have a value yet, this + # prepends an option with a generic prompt — "Please select" — or the given prompt string. + # + # + # Sample usage: + # labels = ['North America','Europe'] + # groups = [['United States','Canada'],['Denmark','Germany','France']] + # optgroups_with_options_for_select(labels,groups) + # + # Possible output: + # + # + # + # + # + # + # + # + # + # + # Note: Only the and tags are returned, so you still have to + # wrap the output in an appropriate tag. - def optgroups_with_options_for_select(labels, groups, selected_key = nil, prompt = nil) - return "" if labels.size != groups.size + def grouped_options_for_select(grouped_options, selected_key = nil, prompt = nil) str = String.new unless prompt.nil? prompt.kind_of?(String) ? prompt : 'Please select' str += content_tag :option, prompt, :value => "" end - (0..labels.size-1).each do |i| - str += content_tag :optgroup, options_for_select(groups[i], selected_key), :label => labels[i] + grouped_options = grouped_options.sort if grouped_options.is_a? Hash + for group in grouped_options + str += content_tag :optgroup, options_for_select(group[1], selected_key), :label => group[0] end str end diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index b784e7c..609b248 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -142,42 +142,27 @@ uses_mocha "FormOptionsHelperTest" do ) end - def test_optgroups_with_options_for_select + def test_grouped_options_for_select_with_array assert_dom_equal( "\n", - optgroups_with_options_for_select(["Hats"], [["Baseball Cap","Cowboy Hat"]]) + grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]]) ) end - def test_optgroups_with_options_for_select_with_selected + def test_grouped_options_for_select_with_selected_and_prompt assert_dom_equal( - "\n", - optgroups_with_options_for_select(["Hats"], [["Baseball Cap","Cowboy Hat"]], "Cowboy Hat") + "\n", + grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]], "Cowboy Hat", "Choose a product...") ) end - - def test_optgroups_with_options_for_select_with_prompt - assert_dom_equal( - "\n", - optgroups_with_options_for_select(["Hats"], [["Baseball Cap","Cowboy Hat"]], nil, "Choose a product...") - ) - end - - def test_optgroups_with_options_for_select_bad_arguments - assert_dom_equal( - "", - optgroups_with_options_for_select(["Hats"], ["Baseball Cap","Cowboy Hat"]) - ) - - end - def test_optgroups_with_options_for_select_with_multiple_selected - assert_dom_equal( - "\n\n", - optgroups_with_options_for_select(["Hats","Boots"], [["Baseball Cap","Cowboy"],["Snow","Cowboy"]],"Cowboy") - ) + def test_optgroups_with_with_options_with_hash + assert_dom_equal( + "\n\n", + grouped_options_for_select({'North America' => ['United States','Canada'], 'Europe' => ['Denmark','Germany']}) + ) end - + def test_time_zone_options_no_parms opts = time_zone_options_for_select assert_dom_equal "\n" + -- 1.5.4.5 From 92709f41f9d72295759158b470aa37c57cc67d77 Mon Sep 17 00:00:00 2001 From: Jon Crawford Date: Mon, 8 Sep 2008 19:40:09 -0500 Subject: [PATCH] Updated API to explain nested arrays in options. Improved tests to include deep-nested arrays. --- .../lib/action_view/helpers/form_options_helper.rb | 11 ++++++----- .../test/template/form_options_helper_test.rb | 9 +++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 88d3dde..ebc419d 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -275,18 +275,19 @@ module ActionView # Parameters: # * +grouped_options+ - Accepts a nested array or hash of strings. The first value serves as the # label while the second value must be an array of options. The second value can be a - # nested array of text-value pairs. See options_for_select for more info. + # nested array of text-value pairs. See options_for_select for more info. + # Ex. ["North America",[["United States","US"],["Canada","CA"]]] # * +selected_key+ - A value equal to the +value+ attribute for one of the tags, # which will have the +selected+ attribute set. Note: It is possible for this value to match multiple options # as you might have the same option in multiple groups. Each will then get selected="selected". # * +prompt+ - set to true or a prompt string. When the select element doesn’t have a value yet, this - # prepends an option with a generic prompt — "Please select" — or the given prompt string. + # prepends an option with a generic prompt — "Please select" — or the given prompt string. # # # Sample usage (Array): # grouped_options = [ # ['North America', - # ['United States','Canada']], + # [['United States','US'],'Canada']], # ['Europe', # ['Denmark','Germany','France']] # ] @@ -294,7 +295,7 @@ module ActionView # # Sample usage (Hash): # grouped_options = { - # 'North America' => ['United States', 'Canada'], + # 'North America' => [['United States','US], 'Canada'], # 'Europe' => ['Denmark','Germany','France'] # } # grouped_options_for_select(grouped_options) @@ -306,7 +307,7 @@ module ActionView # # # - # + # # # # diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 609b248..5e5dddf 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -144,8 +144,13 @@ uses_mocha "FormOptionsHelperTest" do def test_grouped_options_for_select_with_array assert_dom_equal( - "\n", - grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]]) + "\n\n", + grouped_options_for_select([ + ["North America", + [['United States','US'],"Canada"]], + ["Europe", + [["Great Britain","GB"], "Germany"]] + ]) ) end -- 1.5.4.5