Skip to content

Commit

Permalink
Allow strings to be sent as collection to select.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#2391 state:committed]
  • Loading branch information
josevalim authored and NZKoz committed May 15, 2009
1 parent f7cb7fc commit 66ead4f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions actionpack/lib/action_view/helpers/form_options_helper.rb
Expand Up @@ -230,6 +230,8 @@ def time_zone_select(object, method, priority_zones = nil, options = {}, html_op
#
# NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
def options_for_select(container, selected = nil)
return container if String === container

container = container.to_a if Hash === container
selected, disabled = extract_selected_and_disabled(selected)

Expand Down
22 changes: 22 additions & 0 deletions actionpack/test/template/form_options_helper_test.rb
Expand Up @@ -80,6 +80,14 @@ def test_collection_options_with_proc_for_disabled
)
end

def test_string_options_for_select
options = "<option value=\"Denmark\">Denmark</option><option value=\"USA\">USA</option><option value=\"Sweden\">Sweden</option>"
assert_dom_equal(
options,
options_for_select(options)
)
end

def test_array_options_for_select
assert_dom_equal(
"<option value=\"&lt;Denmark&gt;\">&lt;Denmark&gt;</option>\n<option value=\"USA\">USA</option>\n<option value=\"Sweden\">Sweden</option>",
Expand Down Expand Up @@ -324,6 +332,20 @@ def @post.to_param; 108; end
)
end

def test_select_under_fields_for_with_string_and_given_prompt
@post = Post.new
options = "<option value=\"abe\">abe</option><option value=\"mus\">mus</option><option value=\"hest\">hest</option>"

fields_for :post, @post do |f|
concat f.select(:category, options, :prompt => 'The prompt')
end

assert_dom_equal(
"<select id=\"post_category\" name=\"post[category]\"><option value=\"\">The prompt</option>\n#{options}</select>",
output_buffer
)
end

def test_select_with_blank
@post = Post.new
@post.category = "<mus>"
Expand Down

0 comments on commit 66ead4f

Please sign in to comment.