Merge inherited builder/mappings branch from github.com/rejeep

This commit is contained in:
Carlos Antonio da Silva 2011-01-18 23:34:08 -02:00
commit ced9e71af2
6 changed files with 51 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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