Permalink
Browse files

Grouped options

  • Loading branch information...
1 parent 4411fe3 commit 4fa8be82e5983d7f5db883e3d12daf30cb615243 @activestylus committed Mar 14, 2011
Showing with 70 additions and 16 deletions.
  1. +1 −9 lib/padrino-fields/form_builder.rb
  2. +25 −1 lib/padrino-fields/form_helpers.rb
  3. +10 −4 test/test_form_builder.rb
  4. +34 −2 test/test_form_helpers.rb
@@ -62,16 +62,8 @@ def #{type}_field(field, options={})
end
EOF
end
-
- def grouped_options(collection)
- collection.map do |optgroup|
- optgroup.map do |option|
- end
- end
- end
-
- protected
+
def collect_inputs_as(attribute,type,options={})
options[:options].map do |item|
collection_input(attribute,type,item,options.keep_if {|key, value| key != :options})
@@ -24,11 +24,35 @@ def select_tag(name, options={})
options[:options] = options_from_collection(collection, fields) if collection
blank = options.delete(:include_blank)
options[:options].to_a.unshift(blank.is_a?(String) ? [blank, ''] : '') if blank
- select_options_html = options_for_select(options.delete(:options), options.delete(:selected))
+ select_options_html = if options[:options]
+ options_for_select(options.delete(:options), options.delete(:selected))
+ elsif options[:grouped_options]
+ grouped_options_for_select(options.delete(:grouped_options), options.delete(:selected))
+ end
options.merge!(:name => "#{options[:name]}[]") if options[:multiple]
content_tag(:select, select_options_html, options)
end
+ def grouped_options_for_select(collection,selected=nil)
+ if collection.is_a?(Hash)
+ collection.each.map do |key, value|
+ content_tag :optgroup, :label => key do
+ options_for_select(value, selected)
+ end
+ end
+ elsif collection.is_a?(Array)
+ collection.map do |optgroup|
+ content_tag :optgroup, :label => optgroup.first do
+ options_for_select(optgroup.last, selected)
+ end
+ end
+ else
+ raise "options must be a hash or array, not a #{collection.class}"
+ end
+ end
+
+
+
end # FormHelpers
end # Helpers
end # Padrino
View
@@ -93,10 +93,17 @@ def setup
assert_has_tag("input", type:"text", id:"person_name", name:"person[name]") { actual }
end
- should "return a select field" do
- actual = field.input(:name, :options => [["Ann",1],["Bob",2]])
+ # should "return a select field" do
+ # actual = field.input(:name, :options => [["Ann",1],["Bob",2]])
+ # assert_has_tag("select", id:"person_name", name:"person[name]") { actual }
+ # assert_has_tag("option", value:"1", content:"Ann") { actual }
+ # assert_has_tag("option", value:"2", content:"Bob") { actual }
+ # end
+
+ should "return a select tag with options" do
+ actual = field.input(:name, :options => ["Ann",["Bob",2]])
assert_has_tag("select", id:"person_name", name:"person[name]") { actual }
- assert_has_tag("option", value:"1", content:"Ann") { actual }
+ assert_has_tag("option", value:"Ann", content:"Ann") { actual }
assert_has_tag("option", value:"2", content:"Bob") { actual }
end
@@ -145,7 +152,6 @@ def setup
actual = field.input(:string, :as => :file)
assert_has_tag("input", :type => "file") { actual }
end
-
end
context "#setup_label" do
View
@@ -1,7 +1,7 @@
require "helper"
-class TestFormHelper < ActiveSupport::TestCase
-
+class TestFormHelper < Test::Unit::TestCase
+
%w(date email number search tel url).each do |type|
class_eval <<-EOF
context 'for ##{type}_field_tag method' do
@@ -12,5 +12,37 @@ class TestFormHelper < ActiveSupport::TestCase
end
EOF
end
+
+ context '#select_tag' do
+ should "return a select tag with grouped options for an nested array" do
+ opts = [
+ ["Friends",["Yoda",["Obiwan",2]]],
+ ["Enemies", ["Palpatine",['Darth Vader',3]]]
+ ]
+ actual = select_tag( 'name', :grouped_options => opts )
+ assert_has_tag("select", name:"name") { actual }
+ assert_has_tag("optgroup", label:"Friends") { actual }
+ assert_has_tag("option", value:"Yoda", content:"Yoda") { actual }
+ assert_has_tag("option", value:"2", content:"Obiwan") { actual }
+ assert_has_tag("optgroup", label:"Enemies") { actual }
+ assert_has_tag("option", value:"Palpatine", content:"Palpatine") { actual }
+ assert_has_tag("option", value:"3", content:"Darth Vader") { actual }
+ end
+
+ should "return a select tag with grouped options for a hash" do
+ opts = {
+ "Friends" => ["Yoda",["Obiwan",2]],
+ "Enemies" => ["Palpatine",['Darth Vader',3]]
+ }
+ actual = select_tag( 'name', :grouped_options => opts )
+ assert_has_tag("select", name:"name") { actual }
+ assert_has_tag("optgroup", label:"Friends") { actual }
+ assert_has_tag("option", value:"Yoda", content:"Yoda") { actual }
+ assert_has_tag("option", value:"2", content:"Obiwan") { actual }
+ assert_has_tag("optgroup", label:"Enemies") { actual }
+ assert_has_tag("option", value:"Palpatine", content:"Palpatine") { actual }
+ assert_has_tag("option", value:"3", content:"Darth Vader") { actual }
+ end
+ end
end

0 comments on commit 4fa8be8

Please sign in to comment.