From d9cc0a3f9468d28622d5636ff6fd23acc7281bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 10 Dec 2009 00:22:53 -0200 Subject: [PATCH] Detect label and value automatically. --- TODO.rdoc | 1 - lib/simple_form.rb | 8 ++++++++ lib/simple_form/components/input.rb | 16 ++++++++-------- test/components/input_test.rb | 7 +++++++ test/support/models.rb | 6 ++++-- test/test_helper.rb | 7 ++++--- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/TODO.rdoc b/TODO.rdoc index 669f7c27..5a32de19 100644 --- a/TODO.rdoc +++ b/TODO.rdoc @@ -5,7 +5,6 @@ * Sample CSS * Test forms with non objects * Get default string options from column definition -* Detect label and values automatically * Add support to default :prompt methods on datetime inputs * Add support to default label method * Add wrapper support diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 7118a2fa..052b85f3 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -18,4 +18,12 @@ module SimpleForm SimpleForm::Components::Label, SimpleForm::Components::Input, SimpleForm::Components::Hint, SimpleForm::Components::Error ] + + # Series of attemps to detect a default label method for collection + mattr_accessor :collection_label_methods + @@collection_label_methods = [ :name, :title, :to_s ] + + # Series of attemps to detect a default value method for collection + mattr_accessor :collection_value_methods + @@collection_value_methods = [ :id, :to_s ] end \ No newline at end of file diff --git a/lib/simple_form/components/input.rb b/lib/simple_form/components/input.rb index 26e070a6..d859e5b1 100644 --- a/lib/simple_form/components/input.rb +++ b/lib/simple_form/components/input.rb @@ -57,19 +57,19 @@ module SimpleForm end def detect_collection_methods(collection, options) - case collection.first + sample = collection.first || collection.last + + case sample when Array label, value = :first, :last when Integer - value = :to_i - when String - # Do nothing ... - else - # TODO Implement detection logic + label, value = :to_s, :to_i + when String, NilClass + label, value = :to_s, :to_s end - options[:label_method] ||= label || :to_s - options[:value_method] ||= value || :to_s + options[:label_method] ||= label || SimpleForm.collection_label_methods.find { |m| sample.respond_to?(m) } + options[:value_method] ||= value || SimpleForm.collection_value_methods.find { |m| sample.respond_to?(m) } end end end diff --git a/test/components/input_test.rb b/test/components/input_test.rb index efa2c031..2c199bfb 100644 --- a/test/components/input_test.rb +++ b/test/components/input_test.rb @@ -190,6 +190,13 @@ class InputTest < ActionView::TestCase assert_no_select 'select option[value=]', "" end + test 'input should detect label and value on collections' do + users = [ setup_new_user(:id => 1, :name => "Jose"), setup_new_user(:id => 2, :name => "Carlos") ] + with_input_for :description, :select, :collection => users + assert_select 'select option[value=1]', 'Jose' + assert_select 'select option[value=2]', 'Carlos' + 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]' diff --git a/test/support/models.rb b/test/support/models.rb index 05bcf516..e2eaba24 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -10,9 +10,11 @@ class Column end class User < OpenStruct + attr_reader :id - def id - 1 + def initialize(attributes={}) + @id = attributes.delete(:id) + super end def new_record? diff --git a/test/test_helper.rb b/test/test_helper.rb index 8ff6b0fd..b83ac61c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -32,12 +32,13 @@ class ActionView::TestCase @response = MockResponse.new(self) end - def setup_new_user - @user = User.new( + def setup_new_user(options={}) + @user = User.new({ + :id => 1, :name => 'New in Simple Form!', :description => 'Hello!', :created_at => Time.now - ) + }.merge(options)) end def protect_against_forgery?