diff --git a/README.rdoc b/README.rdoc index dd22de3e..56fa5def 100644 --- a/README.rdoc +++ b/README.rdoc @@ -370,6 +370,24 @@ SimpleForm has several configuration values. You can read and change them in the rails generate simple_form:install +== Custom form builder + +You can create a custom form builder that uses SimpleForm. + +Create a helper method that calls simple_form_for with a custom builder: + + def custom_form_for(object, *args, &block) + simple_form_for(object, *(args << { :builder => CustomFormBuilder }), &block) + end + +Create a form builder class that inherits from SimpleForm::FormBuilder. + + class CustomFormBuilder < SimpleForm::FormBuilder + def input(attribute_name, *args, &block) + super(attribute_name, *(args << { :input_html => { :class => 'custom' } }), &block) + end + end + == Maintainers * José Valim (http://github.com/josevalim) diff --git a/Rakefile b/Rakefile index 309ea077..97b4b62d 100644 --- a/Rakefile +++ b/Rakefile @@ -20,6 +20,6 @@ Rake::RDocTask.new(:rdoc) do |rdoc| rdoc.rdoc_dir = 'rdoc' rdoc.title = 'SimpleForm' rdoc.options << '--line-numbers' << '--inline-source' - rdoc.rdoc_files.include('README') + rdoc.rdoc_files.include('README.rdoc') rdoc.rdoc_files.include('lib/**/*.rb') end \ No newline at end of file diff --git a/lib/simple_form/action_view_extensions/form_helper.rb b/lib/simple_form/action_view_extensions/form_helper.rb index b0509730..0fc2002f 100644 --- a/lib/simple_form/action_view_extensions/form_helper.rb +++ b/lib/simple_form/action_view_extensions/form_helper.rb @@ -33,7 +33,7 @@ module SimpleForm class_eval <<-METHOD, __FILE__, __LINE__ def simple_#{helper}(record_or_name_or_array, *args, &block) options = args.extract_options! - options[:builder] = SimpleForm::FormBuilder + options[:builder] ||= SimpleForm::FormBuilder css_class = case record_or_name_or_array when String, Symbol then record_or_name_or_array.to_s when Array then dom_class(record_or_name_or_array.last) diff --git a/lib/simple_form/map_type.rb b/lib/simple_form/map_type.rb index b91ca9e1..e0944f83 100644 --- a/lib/simple_form/map_type.rb +++ b/lib/simple_form/map_type.rb @@ -1,7 +1,10 @@ +require 'active_support/core_ext/class/attribute' + module SimpleForm module MapType - def mappings - @mappings ||= {} + def self.extended(base) + base.class_attribute :mappings + base.mappings = {} end def map_type(*types) diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb index 2f150d36..af1689c3 100644 --- a/test/form_builder_test.rb +++ b/test/form_builder_test.rb @@ -9,6 +9,12 @@ class FormBuilderTest < ActionView::TestCase end end + def with_custom_form_for(object, *args, &block) + with_concat_custom_form_for(object) do |f| + f.input(*args, &block) + end + end + def with_button_for(object, *args) with_concat_form_for(object) do |f| f.button(*args) @@ -583,4 +589,10 @@ class FormBuilderTest < ActionView::TestCase assert_select 'form ul', :count => 1 assert_select 'form ul li', :count => 3 end + + # CUSTOM FORM BUILDER + test 'custom builder should inherit mappings' do + with_custom_form_for @user, :email + assert_select 'form input[type=email]#user_email.custom' + end end diff --git a/test/support/misc_helpers.rb b/test/support/misc_helpers.rb index c87c1527..c5a742e1 100644 --- a/test/support/misc_helpers.rb +++ b/test/support/misc_helpers.rb @@ -28,4 +28,18 @@ module MiscHelpers def with_concat_form_for(object, &block) concat simple_form_for(object, &block) end + + def with_concat_custom_form_for(object, &block) + concat custom_form_for(object, &block) + end + + def custom_form_for(object, *args, &block) + simple_form_for(object, *(args << { :builder => CustomFormBuilder }), &block) + end +end + +class CustomFormBuilder < SimpleForm::FormBuilder + def input(attribute_name, *args, &block) + super(attribute_name, *(args << { :input_html => { :class => 'custom' } }), &block) + end end