Skip to content
This repository
Browse code

select tags coerce the :selected option, options to strings before co…

…mparison [#5056 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit dbe5ae488eebee660cd97e59a2e15c4752fbe15b 1 parent 1091a6e
Subba Rao Pasupuleti authored August 14, 2010 josevalim committed August 15, 2010
21  actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -298,17 +298,18 @@ def options_for_select(container, selected = nil)
298 298
         return container if String === container
299 299
 
300 300
         container = container.to_a if Hash === container
301  
-        selected, disabled = extract_selected_and_disabled(selected)
  301
+        selected, disabled = extract_selected_and_disabled(selected).map do | r |
  302
+           Array.wrap(r).map(&:to_s)
  303
+        end
302 304
 
303  
-        options_for_select = container.map do |element|
  305
+        container.map do |element|
304 306
           html_attributes = option_html_attributes(element)
305  
-          text, value = option_text_and_value(element)
  307
+          text, value = option_text_and_value(element).map(&:to_s)
306 308
           selected_attribute = ' selected="selected"' if option_value_selected?(value, selected)
307 309
           disabled_attribute = ' disabled="disabled"' if disabled && option_value_selected?(value, disabled)
308  
-          %(<option value="#{html_escape(value.to_s)}"#{selected_attribute}#{disabled_attribute}#{html_attributes}>#{html_escape(text.to_s)}</option>)
309  
-        end
  310
+          %(<option value="#{html_escape(value)}"#{selected_attribute}#{disabled_attribute}#{html_attributes}>#{html_escape(text)}</option>)
  311
+        end.join("\n").html_safe
310 312
 
311  
-        options_for_select.join("\n").html_safe
312 313
       end
313 314
 
314 315
       # Returns a string of option tags that have been compiled by iterating over the +collection+ and assigning the
@@ -528,10 +529,12 @@ def option_value_selected?(value, selected)
528 529
         end
529 530
 
530 531
         def extract_selected_and_disabled(selected)
531  
-          if selected.is_a?(Hash)
532  
-            [selected[:selected], selected[:disabled]]
  532
+          if selected.is_a?(Proc)
  533
+            [ selected, nil ]
533 534
           else
534  
-            [selected, nil]
  535
+            selected = Array.wrap(selected)
  536
+            options = selected.extract_options!.symbolize_keys
  537
+            [ options[:selected] || selected , options[:disabled] ]
535 538
           end
536 539
         end
537 540
 
62  actionpack/test/template/form_options_helper_test.rb
@@ -176,6 +176,68 @@ def test_ducktyped_options_for_select
176 176
     )
177 177
   end
178 178
 
  179
+  def test_collection_options_with_preselected_value_as_string_and_option_value_is_integer
  180
+    albums = [ Album.new(1, "first","rap"), Album.new(2, "second","pop")] 
  181
+    assert_dom_equal(
  182
+    %(<option selected="selected" value="1">rap</option>\n<option value="2">pop</option>),
  183
+    options_from_collection_for_select(albums, "id", "genre", :selected => "1")
  184
+    )
  185
+  end
  186
+
  187
+  def test_collection_options_with_preselected_value_as_integer_and_option_value_is_string
  188
+    albums = [ Album.new("1", "first","rap"), Album.new("2", "second","pop")] 
  189
+
  190
+    assert_dom_equal(
  191
+    %(<option selected="selected" value="1">rap</option>\n<option value="2">pop</option>),
  192
+    options_from_collection_for_select(albums, "id", "genre", :selected => 1)
  193
+    )
  194
+  end
  195
+
  196
+  def test_collection_options_with_preselected_value_as_string_and_option_value_is_float
  197
+    albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")] 
  198
+
  199
+    assert_dom_equal(
  200
+    %(<option value="1.0">rap</option>\n<option value="2.0" selected="selected">pop</option>),
  201
+    options_from_collection_for_select(albums, "id", "genre", :selected => "2.0")
  202
+    )
  203
+  end
  204
+
  205
+  def test_collection_options_with_preselected_value_as_nil
  206
+    albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")] 
  207
+
  208
+    assert_dom_equal(
  209
+    %(<option value="1.0">rap</option>\n<option value="2.0">pop</option>),
  210
+    options_from_collection_for_select(albums, "id", "genre", :selected => nil)
  211
+    )
  212
+  end
  213
+
  214
+  def test_collection_options_with_disabled_value_as_nil
  215
+    albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")] 
  216
+
  217
+    assert_dom_equal(
  218
+    %(<option value="1.0">rap</option>\n<option value="2.0">pop</option>),
  219
+    options_from_collection_for_select(albums, "id", "genre", :disabled => nil)
  220
+    )
  221
+  end
  222
+
  223
+  def test_collection_options_with_disabled_value_as_array
  224
+    albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")] 
  225
+
  226
+    assert_dom_equal(
  227
+    %(<option disabled="disabled" value="1.0">rap</option>\n<option disabled="disabled" value="2.0">pop</option>),
  228
+    options_from_collection_for_select(albums, "id", "genre", :disabled => ["1.0", 2.0])
  229
+    )
  230
+  end
  231
+
  232
+  def test_collection_options_with_preselected_values_as_string_array_and_option_value_is_float
  233
+    albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop"), Album.new(3.0, "third","country") ] 
  234
+
  235
+    assert_dom_equal(
  236
+    %(<option value="1.0" selected="selected">rap</option>\n<option value="2.0">pop</option>\n<option value="3.0" selected="selected">country</option>),
  237
+    options_from_collection_for_select(albums, "id", "genre", ["1.0","3.0"])
  238
+    )
  239
+  end
  240
+
179 241
   def test_option_groups_from_collection_for_select
180 242
     assert_dom_equal(
181 243
       "<optgroup label=\"&lt;Africa&gt;\"><option value=\"&lt;sa&gt;\">&lt;South Africa&gt;</option>\n<option value=\"so\">Somalia</option></optgroup><optgroup label=\"Europe\"><option value=\"dk\" selected=\"selected\">Denmark</option>\n<option value=\"ie\">Ireland</option></optgroup>",

0 notes on commit dbe5ae4

Please sign in to comment.
Something went wrong with that request. Please try again.