Skip to content

Commit

Permalink
Merge pull request #1337 from namusyaka/fix_grouped_options_of_select
Browse files Browse the repository at this point in the history
don't use block for content_tag in #select_tag
  • Loading branch information
ujifgc committed Jul 7, 2013
2 parents 99c3c13 + febd5a5 commit 5976923
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 6 deletions.
8 changes: 2 additions & 6 deletions padrino-helpers/lib/padrino-helpers/form_helpers.rb
Expand Up @@ -833,9 +833,7 @@ def grouped_options_for_select(collection, selected=nil, prompt=false)
# {:first => [[1,2,3], {:disabled => true}], :second => [4,5,6]}
attributes_hash = value.last.is_a?(Hash) ? value.pop : nil
disabled ||= attributes_hash && attributes_hash.include?(:disabled) ? attributes_hash[:disabled] : false
content_tag :optgroup, :label => key, :disabled => disabled do
options_for_select(value, selected)
end
content_tag :optgroup, options_for_select(value, selected), :label => key, :disabled => disabled
end
elsif collection.is_a?(Array)
# Array format:
Expand All @@ -845,9 +843,7 @@ def grouped_options_for_select(collection, selected=nil, prompt=false)
# the last item tells if it is disabled or not. This is keeps it backwards compatible.
collection.map do |optgroup|
disabled ||= optgroup.count > 2 ? optgroup.pop : false
content_tag :optgroup, :label => optgroup.first, :disabled => disabled do
options_for_select(optgroup.last, selected)
end
content_tag :optgroup, options_for_select(optgroup.last, selected), :label => optgroup.first, :disabled => disabled
end
end
end
Expand Down
Expand Up @@ -19,6 +19,7 @@
<%= label_tag :gender %>
<%= label_tag :color %>
<%= select_tag :color, :options => ['green', 'orange', 'purple'] %>
<%= select_tag :type, :grouped_options => { 'foo' => ['foo', 'bar'], 'bar' => ['foo', 'bar'] } %>
<%= radio_button_tag :gender, :value => 'male' %>
<%= radio_button_tag :gender, :value => 'female' %>
<%= submit_tag %>
Expand Down
Expand Up @@ -18,6 +18,7 @@
= search_field_tag :search
= label_tag :color
= select_tag :color, :options => ['green', 'orange', 'purple']
= select_tag :type, :grouped_options => { 'foo' => ['foo', 'bar'], 'bar' => ['foo', 'bar'] }
= label_tag :gender
= radio_button_tag :gender, :value => 'male'
= radio_button_tag :gender, :value => 'female'
Expand Down
Expand Up @@ -18,6 +18,7 @@
= search_field_tag :search
= label_tag :color
= select_tag :color, :options => ['green', 'orange', 'purple']
= select_tag :type, :grouped_options => { 'foo' => ['foo', 'bar'], 'bar' => ['foo', 'bar'] }
= label_tag :gender
= radio_button_tag :gender, :value => 'male'
= radio_button_tag :gender, :value => 'female'
Expand Down
3 changes: 3 additions & 0 deletions padrino-helpers/test/test_form_helpers.rb
Expand Up @@ -787,6 +787,7 @@ def app
assert_have_selector('select option', :content => 'green', :value => '1')
assert_have_selector('select option', :content => 'orange', :value => '2', :selected => 'selected')
assert_have_selector('select option', :content => 'purple', :value => '3')
assert_have_selector('select optgroup', :label => 'foo')
end

should "display select tag in haml" do
Expand All @@ -799,6 +800,7 @@ def app
assert_have_selector('select option', :content => 'green', :value => '1')
assert_have_selector('select option', :content => 'orange', :value => '2', :selected => 'selected')
assert_have_selector('select option', :content => 'purple', :value => '3')
assert_have_selector('select optgroup', :label => 'foo')
end

should "display select tag in slim" do
Expand All @@ -811,6 +813,7 @@ def app
assert_have_selector('select option', :content => 'green', :value => '1')
assert_have_selector('select option', :content => 'orange', :value => '2', :selected => 'selected')
assert_have_selector('select option', :content => 'purple', :value => '3')
assert_have_selector('select optgroup', :label => 'foo')
end
end

Expand Down

0 comments on commit 5976923

Please sign in to comment.