1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

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

View file

@ -158,7 +158,19 @@ class InputTest < ActionView::TestCase
assert_select 'select option', 'Carlos' assert_select 'select option', 'Carlos'
end 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'] with_input_for :name, :radio, :collection => ['Jose', 'Carlos']
assert_select 'input[type=radio][value=Jose]' assert_select 'input[type=radio][value=Jose]'
assert_select 'input[type=radio][value=Carlos]' assert_select 'input[type=radio][value=Carlos]'
@ -166,6 +178,12 @@ class InputTest < ActionView::TestCase
assert_select 'label.radio', 'Carlos' assert_select 'label.radio', 'Carlos'
end 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 test 'input should allow using a collection with text/value arrays' do
with_input_for :name, :radio, :collection => [['Jose', 'jose'], ['Carlos', 'carlos']] with_input_for :name, :radio, :collection => [['Jose', 'jose'], ['Carlos', 'carlos']]
assert_select 'input[type=radio][value=jose]' assert_select 'input[type=radio][value=jose]'
@ -193,5 +211,6 @@ class InputTest < ActionView::TestCase
test 'input should allow disabling required' do test 'input should allow disabling required' do
with_input_for :name, :string, :required => false with_input_for :name, :string, :required => false
assert_no_select 'input.required' assert_no_select 'input.required'
assert_select 'input.optional#user_name'
end end
end end

View file

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