From 3edc98a1cc6c3a701ea1ca7d4525623c2d3b4259 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 11 Mar 2007 13:14:53 +0000 Subject: form_options_helper refactoring for clarity. Closes #7787. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6377 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_view/helpers/form_options_helper.rb | 43 +++++++++++++--------- 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 4dafc471f8..6fc582ff65 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -112,17 +112,9 @@ module ActionView container = container.to_a if Hash === container options_for_select = container.inject([]) do |options, element| - if !element.is_a?(String) and element.respond_to?(:first) and element.respond_to?(:last) - is_selected = ( (selected.respond_to?(:include?) && !selected.is_a?(String) ? selected.include?(element.last) : element.last == selected) ) - if is_selected - options << "" - else - options << "" - end - else - is_selected = ( (selected.respond_to?(:include?) && !selected.is_a?(String) ? selected.include?(element) : element == selected) ) - options << ((is_selected) ? "" : "") - end + text, value = option_text_and_value(element) + selected_attribute = ' selected="selected"' if option_value_selected?(value, selected) + options << %() end options_for_select.join("\n") @@ -130,18 +122,18 @@ module ActionView # Returns a string of option tags that have been compiled by iterating over the +collection+ and assigning the # the result of a call to the +value_method+ as the option value and the +text_method+ as the option text. - # If +selected_value+ is specified, the element returning a match on +value_method+ will get the selected option tag. + # If +selected+ is specified, the element returning a match on +value_method+ will get the selected option tag. # # Example (call, result). Imagine a loop iterating over each +person+ in @project.people to generate an input tag: # options_from_collection_for_select(@project.people, "id", "name") # # # NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag. - def options_from_collection_for_select(collection, value_method, text_method, selected_value = nil) - options_for_select( - collection.inject([]) { |options, object| options << [ object.send(text_method), object.send(value_method) ] }, - selected_value - ) + def options_from_collection_for_select(collection, value_method, text_method, selected = nil) + options = collection.map do |element| + [element.send(text_method), element.send(value_method)] + end + options_for_select(options, selected) end # Returns a string of option tags, like options_from_collection_for_select, but surrounds them with tags. @@ -244,6 +236,23 @@ module ActionView end 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] + else + [option, option] + end + end + + def option_value_selected?(value, selected) + if selected.respond_to?(:include?) && !selected.is_a?(String) + selected.include? value + else + value == selected + end + end + # All the countries included in the country_options output. COUNTRIES = [ "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua And Barbuda", "Argentina", "Armenia", "Aruba", "Australia", -- cgit v1.2.3