From 3134c6804efbb0ff27e1c7af671c38f781de396e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 9 Dec 2009 21:18:29 -0200 Subject: [PATCH] Test detect collection methods. --- lib/simple_form/components/input.rb | 29 ++++++++++++++--------------- test/components/input_test.rb | 21 ++++++++++++++++++++- test/test_helper.rb | 5 +++++ 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lib/simple_form/components/input.rb b/lib/simple_form/components/input.rb index a42604dc..6d41d4ea 100644 --- a/lib/simple_form/components/input.rb +++ b/lib/simple_form/components/input.rb @@ -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 diff --git a/test/components/input_test.rb b/test/components/input_test.rb index 9ad7065a..dfbf9767 100644 --- a/test/components/input_test.rb +++ b/test/components/input_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 3212b9bf..ea145400 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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'