From 5f2659d9c80bf7aa1bc9cc52b27db28377c5cf77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 10 Dec 2009 12:11:29 -0200 Subject: [PATCH] Wrapper support finished by adding component_html_options. Now all components by default parse the options at :#{component_name}_html. For example, if you want to customize your hints, you just need to give :hint_html. --- TODO.rdoc | 2 +- lib/simple_form/components/base.rb | 7 ++++++- lib/simple_form/components/input.rb | 5 +---- lib/simple_form/components/label.rb | 4 ++-- lib/simple_form/components/wrapper.rb | 2 +- lib/simple_form/required_helpers.rb | 6 ++++++ test/components/input_test.rb | 2 +- test/components/label_test.rb | 4 ++-- test/form_builder_test.rb | 9 ++++++++- 9 files changed, 28 insertions(+), 13 deletions(-) diff --git a/TODO.rdoc b/TODO.rdoc index 6b48a9c7..219beb7f 100644 --- a/TODO.rdoc +++ b/TODO.rdoc @@ -6,7 +6,7 @@ * Get default string options from column definition * Add support to default :prompt methods on datetime inputs * Add support to default label method -* Add wrapper support +* Add support to label, error and hint calls * Improve readme with examples * :country, :time_zone, :group and :file types support diff --git a/lib/simple_form/components/base.rb b/lib/simple_form/components/base.rb index 475cc29c..407a6ed4 100644 --- a/lib/simple_form/components/base.rb +++ b/lib/simple_form/components/base.rb @@ -37,8 +37,13 @@ module SimpleForm self.class.basename end + def component_html_options + options[:"#{basename}_html"] || {} + end + def component_tag(content) - template.content_tag(SimpleForm.component_tag, content, :class => basename) + html_options = component_html_options.reverse_merge(:class => basename) + template.content_tag(SimpleForm.component_tag, content, html_options) end def translate(default='') diff --git a/lib/simple_form/components/input.rb b/lib/simple_form/components/input.rb index f97fa799..51e88212 100644 --- a/lib/simple_form/components/input.rb +++ b/lib/simple_form/components/input.rb @@ -27,17 +27,14 @@ module SimpleForm end def content - html_options = options[:html] || {} - html_options[:class] = default_css_classes(html_options[:class]) options[:options] ||= {} - mapping = self.class.mappings[input_type] raise "Invalid input type #{input_type.inspect}" unless mapping args = [ attribute ] apply_collection_behavior(args) if mapping.collection apply_options_behavior(args) if mapping.options - args << html_options + args << component_html_options @builder.send(mapping.method, *args) end diff --git a/lib/simple_form/components/label.rb b/lib/simple_form/components/label.rb index 9a44bff6..9dd9a04c 100644 --- a/lib/simple_form/components/label.rb +++ b/lib/simple_form/components/label.rb @@ -29,8 +29,8 @@ module SimpleForm end def content - html_options = { :class => default_css_classes } - html_options[:for] = options[:html][:id] if options.key?(:html) + html_options = component_html_options + html_options[:for] = options[:input_html][:id] if options.key?(:input_html) @builder.label(attribute, label_text, html_options) end diff --git a/lib/simple_form/components/wrapper.rb b/lib/simple_form/components/wrapper.rb index cd75e63a..6449d74c 100644 --- a/lib/simple_form/components/wrapper.rb +++ b/lib/simple_form/components/wrapper.rb @@ -5,7 +5,7 @@ module SimpleForm def call if SimpleForm.wrapper_tag - template.content_tag(SimpleForm.wrapper_tag, @component.call, :class => default_css_classes) + template.content_tag(SimpleForm.wrapper_tag, @component.call, component_html_options) else @component.call end diff --git a/lib/simple_form/required_helpers.rb b/lib/simple_form/required_helpers.rb index c82b1869..14180219 100644 --- a/lib/simple_form/required_helpers.rb +++ b/lib/simple_form/required_helpers.rb @@ -11,5 +11,11 @@ module SimpleForm def default_css_classes(merge_class=nil) "#{input_type} #{required_class} #{merge_class}".strip end + + def component_html_options + html_options = super + html_options[:class] = default_css_classes(html_options[:class]) + html_options + end end end \ No newline at end of file diff --git a/test/components/input_test.rb b/test/components/input_test.rb index 0c8638c2..ec328b23 100644 --- a/test/components/input_test.rb +++ b/test/components/input_test.rb @@ -37,7 +37,7 @@ class InputTest < ActionView::TestCase end test 'input should allow passing options to text field' do - with_input_for @user, :name, :string, :html => { :class => 'my_input', :id => 'my_input' } + with_input_for @user, :name, :string, :input_html => { :class => 'my_input', :id => 'my_input' } assert_select 'input#my_input.my_input' end diff --git a/test/components/label_test.rb b/test/components/label_test.rb index 64b98b3a..8ff1b971 100644 --- a/test/components/label_test.rb +++ b/test/components/label_test.rb @@ -113,12 +113,12 @@ class LabelTest < ActionView::TestCase end test 'label should allow overwriting input id' do - with_label_for @user, :name, :string, :html => { :id => 'my_new_id' } + with_label_for @user, :name, :string, :input_html => { :id => 'my_new_id' } assert_select 'label[for=my_new_id]' end test 'label should use default input id when it was not overridden' do - with_label_for @user, :name, :string, :html => { :class => 'my_new_id' } + with_label_for @user, :name, :string, :input_html => { :class => 'my_new_id' } assert_select 'label[for=user_name]' end diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb index 5c96c6ab..327df810 100644 --- a/test/form_builder_test.rb +++ b/test/form_builder_test.rb @@ -78,7 +78,7 @@ class FormBuilderTest < ActionView::TestCase end test 'builder should allow passing options to input' do - with_form_for @user, :name, :html => { :class => 'my_input', :id => 'my_input' } + with_form_for @user, :name, :input_html => { :class => 'my_input', :id => 'my_input' } assert_select 'form input#my_input.my_input.string' end @@ -147,6 +147,13 @@ class FormBuilderTest < ActionView::TestCase end end + test 'builder wrapping tag allow custom options to be given' do + swap SimpleForm, :wrapper_tag => :p do + with_form_for @user, :name, :wrapper_html => { :id => "super_cool" } + assert_select 'form p#super_cool.required.string' + end + end + test 'nested simple fields should yields an instance of FormBuilder' do simple_form_for :user do |f| f.simple_fields_for :posts do |posts_form|