Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Remove leading divider from grouped_options_for_select when divider option is passed #7360

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+10 −2
Split
@@ -500,7 +500,7 @@ def option_groups_from_collection_for_select(collection, group_method, group_lab
# grouped_options_for_select(grouped_options, nil, divider: '---------')
#
# Possible output:
- # <optgroup label="---------">
+ # <optgroup>
# <option value="Denmark">Denmark</option>
# <option value="Germany">Germany</option>
# <option value="France">France</option>
@@ -530,6 +530,14 @@ def grouped_options_for_select(grouped_options, selected_key = nil, options = {}
grouped_options = grouped_options.sort if grouped_options.is_a?(Hash)
+ container = grouped_options.shift
+ if divider
+ label = nil
+ else
+ label, container = container
+ end
+ body.safe_concat content_tag(:optgroup, options_for_select(container, selected_key), :label => label)
+
grouped_options.each do |container|
if divider
label = divider
@@ -297,7 +297,7 @@ def test_grouped_options_for_select_with_array
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>",
+ "<optgroup><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>",
grouped_options_for_select([['US',"Canada"] , ["GB", "Germany"]], nil, divider: "----------")
)