Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #11517 from nashby/optgroup-html-attributes

add support for html attributes to grouped_options_for_select
  • Loading branch information...
commit c43db5564a48bc41ae55cead400af066337e63b6 2 parents 2c1ddd8 + cadfe4b
@drogus drogus authored
View
10 actionview/CHANGELOG.md
@@ -1,3 +1,13 @@
+* Element of the `grouped_options_for_select` can
+ optionally contain html attributes as the last element of the array.
+
+ Example:
+ grouped_options_for_select(
+ [["North America", [['United States','US'],"Canada"], :data => { :foo => 'bar' }]]
+ )
+
+ *Vasiliy Ermolovich*
+
* Fix default rendered format problem when calling `render` without :content_type option.
It should return :html. Fix #11393.
View
6 actionview/lib/action_view/helpers/form_options_helper.rb
@@ -520,12 +520,16 @@ def grouped_options_for_select(grouped_options, selected_key = nil, options = {}
end
grouped_options.each do |container|
+ html_attributes = option_html_attributes(container)
+
if divider
label = divider
else
label, container = container
end
- body.safe_concat content_tag(:optgroup, options_for_select(container, selected_key), :label => label)
+
+ html_attributes = { :label => label }.merge(html_attributes)
+ body.safe_concat content_tag(:optgroup, options_for_select(container, selected_key), html_attributes)
end
body
View
10 actionview/test/template/form_options_helper_test.rb
@@ -302,6 +302,16 @@ def test_grouped_options_for_select_with_array
)
end
+ def test_grouped_options_for_select_with_array_and_html_attributes
+ assert_dom_equal(
+ "<optgroup label=\"North America\" data-foo=\"bar\"><option value=\"US\">United States</option>\n<option value=\"Canada\">Canada</option></optgroup><optgroup label=\"Europe\" disabled=\"disabled\"><option value=\"GB\">Great Britain</option>\n<option value=\"Germany\">Germany</option></optgroup>",
+ grouped_options_for_select([
+ ["North America", [['United States','US'],"Canada"], :data => { :foo => 'bar' }],
+ ["Europe", [["Great Britain","GB"], "Germany"], :disabled => 'disabled']
+ ])
+ )
+ end
+
def test_grouped_options_for_select_with_optional_divider
assert_dom_equal(
"<optgroup label=\"----------\"><option value=\"US\">US</option>\n<option value=\"Canada\">Canada</option></optgroup><optgroup label=\"----------\"><option value=\"GB\">GB</option>\n<option value=\"Germany\">Germany</option></optgroup>",
Please sign in to comment.
Something went wrong with that request. Please try again.