From 8c6e25b26be64e2a3e5b3da828c8b9803b8d9b1c Mon Sep 17 00:00:00 2001 From: Eddie Morgan Date: Mon, 16 Jun 2008 03:57:50 +0100 Subject: [PATCH] Added optional html class handling for options_for_select --- .../lib/action_view/helpers/form_options_helper.rb | 21 ++++++++++++++----- .../test/template/form_options_helper_test.rb | 12 +++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index b3f8e63..b99f8dd 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -173,11 +173,15 @@ module ActionView # where the elements respond to first and last (such as a two-element array), the "lasts" serve as option values and # the "firsts" as option text. Hashes are turned into this form automatically, so the keys become "firsts" and values # become lasts. If +selected+ is specified, the matching "last" or element will get the selected option-tag. +selected+ - # may also be an array of values to be selected when using a multiple select. + # may also be an array of values to be selected when using a multiple select. Optionally, specify a third array element + # to include a class name string with each option. # # Examples (call, result): # options_for_select([["Dollar", "$"], ["Kroner", "DKK"]]) # \n + # + # options_for_select([["Dollar", "$", "american"], ["Kroner", "DKK", "danish"]]) + # \n # # options_for_select([ "VISA", "MasterCard" ], "MasterCard") # \n @@ -193,9 +197,10 @@ module ActionView container = container.to_a if Hash === container options_for_select = container.inject([]) do |options, element| - text, value = option_text_and_value(element) + text, value, class_name = option_text_and_value(element) selected_attribute = ' selected="selected"' if option_value_selected?(value, selected) - options << %() + class_attribute = " class=\"#{class_name}\"" unless class_name.nil? + options << %() end options_for_select.join("\n") @@ -324,10 +329,14 @@ module ActionView private def option_text_and_value(option) # Options are [text, value] pairs or strings used for both. - if !option.is_a?(String) and option.respond_to?(:first) and option.respond_to?(:last) - [option.first, option.last] + if !option.is_a?(String) and option.respond_to?(:first) + if option.length == 3 + [option[0], option[1], option[2]] + else + [option.first, option.last, nil] + end else - [option, option] + [option, option] end end diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 3f89a5e..8bc4499 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -1327,4 +1327,16 @@ COUNTRIES html end + def test_options_for_collection_select_with_class_names + html = options_for_select([["Zero", 0], ["One", 1, "first"], ["Two", 2, "second"], ["i", "i", "imaginary"], ["x"]]) + assert_dom_equal( + "\n" + + "\n" + + "\n" + + "\n" + + "", + html + ) + end + end -- 1.5.5