Skip to content

Commit

Permalink
Merge pull request #1054 from padrino/add-disabled-attribute-to-selec…
Browse files Browse the repository at this point in the history
…t-options

Implemented disabled attribute for select_tag form helper
  • Loading branch information
DAddYE committed Jul 1, 2013
2 parents 2afae0f + 148698e commit c409e42
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
21 changes: 17 additions & 4 deletions padrino-helpers/lib/padrino-helpers/form_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,10 @@ def options_from_collection(collection, fields)
#
def options_for_select(option_items, selected_value=nil)
return [] if option_items.blank?
option_items.map do |caption, value|
option_items.map do |caption, value, disabled|
value ||= caption
content_tag(:option, caption, :value => value, :selected => option_is_selected?(value, caption, selected_value))
disabled ||= false
content_tag(:option, caption, :value => value, :selected => option_is_selected?(value, caption, selected_value), :disabled => disabled)
end
end

Expand All @@ -826,13 +827,25 @@ def options_for_select(option_items, selected_value=nil)
def grouped_options_for_select(collection, selected=nil, prompt=false)
if collection.is_a?(Hash)
collection.map do |key, value|
content_tag :optgroup, :label => key do
# Hash format:
# {:first => [1,2,3], :second => [4,5,6]}
# or:
# {: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
end
elsif collection.is_a?(Array)
# Array format:
# ["Option Label", [:option1, :option2, ...]]
# or:
# ["Option Label", [:option1, :option2, ...], true]
# the last item tells if it is disabled or not. This is keeps it backwards compatible.
collection.map do |optgroup|
content_tag :optgroup, :label => optgroup.first do
disabled ||= optgroup.count > 2 ? optgroup.pop : false
content_tag :optgroup, :label => optgroup.first, :disabled => disabled do
options_for_select(optgroup.last, selected)
end
end
Expand Down
68 changes: 66 additions & 2 deletions padrino-helpers/test/test_form_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ def app
assert_has_tag('select option:first-child', :value => "", :content => "") { actual_html }
end

should "return a select tag with grouped options for an nested array" do
should "display select tag with grouped options for a nested array" do
opts = [
["Friends",["Yoda",["Obiwan",2]]],
["Enemies", ["Palpatine",['Darth Vader',3]]]
Expand All @@ -665,7 +665,33 @@ def app
assert_has_tag(:option, :value => "3", :content => "Darth Vader") { actual_html }
end

should "return a select tag with grouped options for a hash" do
should "display select tag with grouped options for a nested array and accept disabled groups" do
opts = [
["Friends",["Yoda",["Obiwan",2]]],
["Enemies", ["Palpatine",['Darth Vader',3]], true]
]
actual_html = select_tag( 'name', :grouped_options => opts )
assert_has_tag(:select, :name => "name") { actual_html }
assert_has_tag(:option, :disabled => 'disabled', :count => 0) { actual_html }
assert_has_tag(:optgroup, :disabled => 'disabled', :count => 1) { actual_html }
assert_has_tag(:optgroup, :label => "Enemies", :disabled => 'disabled') { actual_html }
end

should "display select tag with grouped options for a nested array and accept disabled groups and/or with disabled options" do
opts = [
["Friends",["Yoda",["Obiwan",2, true]]],
["Enemies", [["Palpatine", "Palpatine", true],['Darth Vader',3]], true]
]
actual_html = select_tag( 'name', :grouped_options => opts )
assert_has_tag(:select, :name => "name") { actual_html }
assert_has_tag(:option, :disabled => 'disabled', :count => 2) { actual_html }
assert_has_tag(:optgroup, :disabled => 'disabled', :count => 1) { actual_html }
assert_has_tag(:option, :content => "Obiwan", :disabled => 'disabled') { actual_html }
assert_has_tag(:optgroup, :label => "Enemies", :disabled => 'disabled') { actual_html }
assert_has_tag(:option, :value => "Palpatine", :content => "Palpatine", :disabled => 'disabled') { actual_html }
end

should "display select tag with grouped options for a hash" do
opts = {
"Friends" => ["Yoda",["Obiwan",2]],
"Enemies" => ["Palpatine",['Darth Vader',3]]
Expand All @@ -680,6 +706,34 @@ def app
assert_has_tag(:option, :value => "3", :content => "Darth Vader") { actual_html }
end

should "display select tag with grouped options for a hash and accept disabled groups" do
opts = {
"Friends" => ["Yoda",["Obiwan",2]],
"Enemies" => ["Palpatine",['Darth Vader',3], {:disabled => true}]
}
actual_html = select_tag( 'name', :grouped_options => opts )
puts actual_html
assert_has_tag(:select, :name => "name") { actual_html }
assert_has_tag(:option, :disabled => 'disabled', :count => 0) { actual_html }
assert_has_tag(:optgroup, :disabled => 'disabled', :count => 1) { actual_html }
assert_has_tag(:optgroup, :label => "Enemies", :disabled => 'disabled') { actual_html }
end

should "display select tag with grouped options for a hash and accept disabled groups and/or with disabled options" do
opts = {
"Friends" => ["Yoda",["Obiwan",2,true]],
"Enemies" => [["Palpatine","Palpatine",true],["Darth Vader",3], {:disabled => true}]
}
actual_html = select_tag( 'name', :grouped_options => opts )
assert_has_tag(:select, :name => "name") { actual_html }
assert_has_tag(:option, :disabled => 'disabled', :count => 2) { actual_html }
assert_has_tag(:optgroup, :disabled => 'disabled', :count => 1) { actual_html }
assert_has_tag(:option, :content => "Obiwan", :disabled => 'disabled') { actual_html }
assert_has_tag(:optgroup, :label => "Enemies", :disabled => 'disabled') { actual_html }
assert_has_tag(:option, :value => "Palpatine", :content => "Palpatine", :disabled => 'disabled') { actual_html }
end


should "display select tag in ruby with multiple attribute" do
actual_html = select_tag(:favorite_color, :multiple => true, :options => ['only', 'option'])
assert_has_tag(:select, :multiple => 'multiple', :name => 'favorite_color[]') { actual_html }
Expand All @@ -695,6 +749,16 @@ def app
assert_has_tag('select option', :content => 'Black', :value => 'black1') { actual_html }
end

should "display options with values and accept disabled options" do
options = [['Green', 'green1', true], ['Blue', 'blue1'], ['Black', "black1"]]
actual_html = select_tag(:favorite_color, :options => options)
assert_has_tag(:select, :name => 'favorite_color') { actual_html }
assert_has_tag('select option', :disabled => 'disabled', :count => 1) { actual_html }
assert_has_tag('select option', :content => 'Green', :value => 'green1', :disabled => 'disabled') { actual_html }
assert_has_tag('select option', :content => 'Blue', :value => 'blue1') { actual_html }
assert_has_tag('select option', :content => 'Black', :value => 'black1') { actual_html }
end

should "display option with values and multiple selected" do
options = [['Green', 'green1'], ['Blue', 'blue1'], ['Black', "black1"]]
actual_html = select_tag(:favorite_color, :options => options, :selected => ['green1', 'Black'])
Expand Down

0 comments on commit c409e42

Please sign in to comment.