From 968e9325b5fa230e51dee8939d4e7f6bcd621e31 Mon Sep 17 00:00:00 2001 From: Johan Andersson Date: Wed, 5 Jan 2011 20:51:31 +0100 Subject: [PATCH 1/4] Make possible to specify custom builder. --- lib/simple_form/action_view_extensions/form_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) From 7c3b42ba2220a2a7bf54c5c192de911868a46402 Mon Sep 17 00:00:00 2001 From: Johan Andersson Date: Wed, 5 Jan 2011 20:52:49 +0100 Subject: [PATCH 2/4] Mappings should be inherited. --- lib/simple_form/map_type.rb | 7 +++++-- test/form_builder_test.rb | 12 ++++++++++++ test/support/misc_helpers.rb | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) 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 From c8c842c779c2f27b383ca916a5c7a6c978536035 Mon Sep 17 00:00:00 2001 From: Johan Andersson Date: Thu, 6 Jan 2011 10:54:37 +0100 Subject: [PATCH 3/4] README file is an RDoc file. --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 14d922db0120634116ead263aa088a755e4f923b Mon Sep 17 00:00:00 2001 From: Johan Andersson Date: Thu, 6 Jan 2011 10:55:59 +0100 Subject: [PATCH 4/4] Add documentation in README about how to create a custom form builder. --- README.rdoc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.rdoc b/README.rdoc index 6e30f21a..8a3f9a35 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 + == TODO Please refer to TODO file.