Test detect collection methods.

This commit is contained in:
José Valim 2009-12-09 21:18:29 -02:00
parent fb3f50412c
commit 3134c6804e
3 changed files with 39 additions and 16 deletions

View File

@ -35,7 +35,7 @@ module SimpleForm
args = [ @attribute ]
if mapping.collection
collection = @options[:collection] || self.class.boolean_collection
collection = (@options[:collection] || self.class.boolean_collection).to_a
detect_collection_methods(collection, @options)
args.push(collection, @options[:value_method], @options[:label_method])
end
@ -47,20 +47,19 @@ module SimpleForm
end
def detect_collection_methods(collection, options)
sample = collection.first
if sample.is_a?(Array) # TODO Test me
options[:label_method] ||= :first
options[:value_method] ||= :last
elsif sample.is_a?(String) # TODO Test me
options[:label_method] ||= :to_s
options[:value_method] ||= :to_s
elsif sample.is_a?(Numeric) # TODO Test me (including selected)
options[:label_method] ||= :to_s
options[:value_method] ||= :to_i
else # TODO Implement collection label methods or something similar
options[:label_method] ||= :to_s
options[:value_method] ||= :to_s
case collection.first
when Array
options[:label_method] ||= :first
options[:value_method] ||= :last
when String
options[:label_method] ||= :to_s
options[:value_method] ||= :to_s
when Numeric
options[:label_method] ||= :to_s
options[:value_method] ||= :to_i
else
options[:label_method] ||= :to_s
options[:value_method] ||= :to_s
end
end
end

View File

@ -158,7 +158,19 @@ class InputTest < ActionView::TestCase
assert_select 'select option', 'Carlos'
end
test 'inputs should allow overriding collection for radio types' do
test 'input should mark the selected value by default' do
@user.name = "Carlos"
with_input_for :name, :select, :collection => ['Jose', 'Carlos']
assert_select 'select option[selected=selected]', 'Carlos'
end
test 'input should mark the selected value also when using integers' do
@user.age = 18
with_input_for :age, :select, :collection => 18..60
assert_select 'select option[selected=selected]', '18'
end
test 'input should allow overriding collection for radio types' do
with_input_for :name, :radio, :collection => ['Jose', 'Carlos']
assert_select 'input[type=radio][value=Jose]'
assert_select 'input[type=radio][value=Carlos]'
@ -166,6 +178,12 @@ class InputTest < ActionView::TestCase
assert_select 'label.radio', 'Carlos'
end
test 'input should mark the current radio value by default' do
@user.name = "Carlos"
with_input_for :name, :radio, :collection => ['Jose', 'Carlos']
assert_select 'input[type=radio][value=Carlos][checked=checked]'
end
test 'input should allow using a collection with text/value arrays' do
with_input_for :name, :radio, :collection => [['Jose', 'jose'], ['Carlos', 'carlos']]
assert_select 'input[type=radio][value=jose]'
@ -193,5 +211,6 @@ class InputTest < ActionView::TestCase
test 'input should allow disabling required' do
with_input_for :name, :string, :required => false
assert_no_select 'input.required'
assert_select 'input.optional#user_name'
end
end

View File

@ -4,6 +4,11 @@ require 'test/unit'
require 'action_controller'
require 'action_view/test_case'
begin
require 'ruby-debug'
rescue LoadError
end
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib', 'simple_form')
require 'simple_form'