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.
This commit is contained in:
José Valim 2009-12-10 12:11:29 -02:00
parent 3e0df00b74
commit 5f2659d9c8
9 changed files with 28 additions and 13 deletions

View File

@ -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

View File

@ -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='')

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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|