From 98d7c6f627a0a31d4d1ece263b9def7255207ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 2 Sep 2011 20:45:17 +0200 Subject: [PATCH] Move hint to new wrappers API. --- lib/simple_form/components/hints.rb | 14 +------------- lib/simple_form/form_builder.rb | 4 +++- lib/simple_form/wrappers.rb | 4 ++++ test/components/hint_test.rb | 10 ++++++---- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/lib/simple_form/components/hints.rb b/lib/simple_form/components/hints.rb index a5671d3f..43dcd9ad 100644 --- a/lib/simple_form/components/hints.rb +++ b/lib/simple_form/components/hints.rb @@ -8,24 +8,12 @@ module SimpleForm private def enabled_hint - template.content_tag(hint_tag, hint_text, hint_html_options) unless hint_text.blank? + (options[:hint] || translate(:hints)).presence end def disabled_hint nil end - - def hint_tag - options[:hint_tag] || SimpleForm.hint_tag - end - - def hint_text - @hint_text ||= options[:hint] || translate(:hints) - end - - def hint_html_options - html_options_for(:hint, [SimpleForm.hint_class]) - end end end end diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index d214b9c1..ded22f68 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -250,7 +250,9 @@ module SimpleForm column = find_attribute_column(attribute_name) input_type = default_input_type(attribute_name, column, options) end - SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).hint + + SimpleForm::Wrappers.find(:hint). + render(SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options)) end # Creates a default label tag for the given attribute. You can give a label diff --git a/lib/simple_form/wrappers.rb b/lib/simple_form/wrappers.rb index b8015f41..5ba37b2b 100644 --- a/lib/simple_form/wrappers.rb +++ b/lib/simple_form/wrappers.rb @@ -3,7 +3,9 @@ module SimpleForm autoload :Many, 'simple_form/wrappers/many' autoload :Root, 'simple_form/wrappers/root' autoload :Single, 'simple_form/wrappers/single' + autoload :Anonym, 'simple_form/wrappers/anonym' + # TODO: Test the anonym case def self.find(name) SimpleForm.components.find { |c| c.namespace == name } || SingleForm::Wrappers::Anonym.new(name) end @@ -13,6 +15,8 @@ module SimpleForm case item when :error Single.new(:error, :tag => SimpleForm.error_tag, :class => SimpleForm.error_class) + when :hint + Single.new(:hint, :tag => SimpleForm.hint_tag, :class => SimpleForm.hint_class) else item end diff --git a/test/components/hint_test.rb b/test/components/hint_test.rb index eb3282a2..92ffe8b8 100644 --- a/test/components/hint_test.rb +++ b/test/components/hint_test.rb @@ -5,7 +5,7 @@ class HintTest < ActionView::TestCase def with_hint_for(object, attribute_name, type, options={}, &block) with_concat_form_for(object) do |f| options[:reflection] = Association.new(Company, :company, {}) if options.delete(:setup_association) - SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).hint.to_s + f.hint attribute_name, options.merge(:as => type) end end @@ -21,8 +21,10 @@ class HintTest < ActionView::TestCase test 'hint uses the current component tag set' do swap SimpleForm, :hint_tag => :p do - with_hint_for @user, :name, :string, :hint => 'Use with care...' - assert_select 'p.hint', 'Use with care...' + swap SimpleForm, :components => [:hint] do + with_hint_for @user, :name, :string, :hint => 'Use with care...' + assert_select 'p.hint', 'Use with care...' + end end end @@ -68,7 +70,7 @@ class HintTest < ActionView::TestCase end test 'hint should be able to pass html options' do - with_hint_for @user, :name, :string, :hint => 'Yay!', :hint_html => { :id => 'hint', :class => 'yay' } + with_hint_for @user, :name, :string, :hint => 'Yay!', :id => 'hint', :class => 'yay' assert_select 'span#hint.hint.yay' end end