def options_for_select(container, selected = nil) container = container.to_a if Hash === container options_for_select = container.inject([]) do |options, element| text, value, disabled = option_text_and_value(element) selected_attribute = ' selected="selected"' if option_value_selected?(value, selected) disabled_attribute = ' disabled="disabled"' if disabled options << %() end options_for_select.join("\n") end def option_text_and_value(option) return if option.class == Fixnum # Options are [text, value, disabled] unless it's a string passed in alone. # examples of what may be passed in: # ["Cat", "cat", true] # ["Dog", "1"] # ["Zebra", true] # "Mushroom" disabled = option.detect{|o| o.is_a?(TrueClass) || o.is_a?(NilClass)} if !option.is_a?(String) [option.first, option[1] == disabled ? option.first : option[1], disabled] else [option, option, nil] end end