Skip to content
This repository
Browse code

Improve select helpers by allowing a selected value of false. This is…

… useful when using a select helper with a boolean attribute, and the attribute is false. (e.g. f.select :allow_comments)
  • Loading branch information...
commit 9ab3cebe9fd60a38c7f8238e74ea377f17212789 1 parent 9bc338e
John Allison jrallison authored jeremy committed
2  actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -534,7 +534,7 @@ def extract_selected_and_disabled(selected)
534 534 else
535 535 selected = Array.wrap(selected)
536 536 options = selected.extract_options!.symbolize_keys
537   - [ options[:selected] || selected , options[:disabled] ]
  537 + [ options.include?(:selected) ? options[:selected] : selected, options[:disabled] ]
538 538 end
539 539 end
540 540
32 actionpack/test/template/form_options_helper_test.rb
@@ -7,7 +7,7 @@ class FormOptionsHelperTest < ActionView::TestCase
7 7 tests ActionView::Helpers::FormOptionsHelper
8 8
9 9 silence_warnings do
10   - Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin)
  10 + Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin, :allow_comments)
11 11 Continent = Struct.new('Continent', :continent_name, :countries)
12 12 Country = Struct.new('Country', :country_id, :country_name)
13 13 Firm = Struct.new('Firm', :time_zone)
@@ -130,6 +130,13 @@ def test_array_options_for_select_with_selection_and_disabled_value
130 130 )
131 131 end
132 132
  133 + def test_boolean_array_options_for_select_with_selection_and_disabled_value
  134 + assert_dom_equal(
  135 + "<option value=\"true\">true</option>\n<option value=\"false\" selected=\"selected\">false</option>",
  136 + options_for_select([ true, false ], :selected => false, :disabled => nil)
  137 + )
  138 + end
  139 +
133 140 def test_array_options_for_string_include_in_other_string_bug_fix
134 141 assert_dom_equal(
135 142 "<option value=\"ruby\">ruby</option>\n<option value=\"rubyonrails\" selected=\"selected\">rubyonrails</option>",
@@ -177,7 +184,7 @@ def test_ducktyped_options_for_select
177 184 end
178 185
179 186 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")]
  187 + albums = [ Album.new(1, "first","rap"), Album.new(2, "second","pop")]
181 188 assert_dom_equal(
182 189 %(<option selected="selected" value="1">rap</option>\n<option value="2">pop</option>),
183 190 options_from_collection_for_select(albums, "id", "genre", :selected => "1")
@@ -185,7 +192,7 @@ def test_collection_options_with_preselected_value_as_string_and_option_value_is
185 192 end
186 193
187 194 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")]
  195 + albums = [ Album.new("1", "first","rap"), Album.new("2", "second","pop")]
189 196
190 197 assert_dom_equal(
191 198 %(<option selected="selected" value="1">rap</option>\n<option value="2">pop</option>),
@@ -194,7 +201,7 @@ def test_collection_options_with_preselected_value_as_integer_and_option_value_i
194 201 end
195 202
196 203 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")]
  204 + albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")]
198 205
199 206 assert_dom_equal(
200 207 %(<option value="1.0">rap</option>\n<option value="2.0" selected="selected">pop</option>),
@@ -203,7 +210,7 @@ def test_collection_options_with_preselected_value_as_string_and_option_value_is
203 210 end
204 211
205 212 def test_collection_options_with_preselected_value_as_nil
206   - albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")]
  213 + albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")]
207 214
208 215 assert_dom_equal(
209 216 %(<option value="1.0">rap</option>\n<option value="2.0">pop</option>),
@@ -212,7 +219,7 @@ def test_collection_options_with_preselected_value_as_nil
212 219 end
213 220
214 221 def test_collection_options_with_disabled_value_as_nil
215   - albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")]
  222 + albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")]
216 223
217 224 assert_dom_equal(
218 225 %(<option value="1.0">rap</option>\n<option value="2.0">pop</option>),
@@ -221,7 +228,7 @@ def test_collection_options_with_disabled_value_as_nil
221 228 end
222 229
223 230 def test_collection_options_with_disabled_value_as_array
224   - albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")]
  231 + albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")]
225 232
226 233 assert_dom_equal(
227 234 %(<option disabled="disabled" value="1.0">rap</option>\n<option disabled="disabled" value="2.0">pop</option>),
@@ -230,7 +237,7 @@ def test_collection_options_with_disabled_value_as_array
230 237 end
231 238
232 239 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") ]
  240 + albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop"), Album.new(3.0, "third","country") ]
234 241
235 242 assert_dom_equal(
236 243 %(<option value="1.0" selected="selected">rap</option>\n<option value="2.0">pop</option>\n<option value="3.0" selected="selected">country</option>),
@@ -364,6 +371,15 @@ def test_select
364 371 )
365 372 end
366 373
  374 + def test_select_with_boolean_method
  375 + @post = Post.new
  376 + @post.allow_comments = false
  377 + assert_dom_equal(
  378 + "<select id=\"post_allow_comments\" name=\"post[allow_comments]\"><option value=\"true\">true</option>\n<option value=\"false\" selected=\"selected\">false</option></select>",
  379 + select("post", "allow_comments", %w( true false ))
  380 + )
  381 + end
  382 +
367 383 def test_select_under_fields_for
368 384 @post = Post.new
369 385 @post.category = "<mus>"

0 comments on commit 9ab3ceb

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