From b4d26d62f58c9fe8c300b1ce1a1cb4dc67e9d105 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