Fixed bug in MapType module when subclassing FormBuilder.

This commit is contained in:
Roel van Dijk 2011-03-30 02:46:08 +08:00 committed by Carlos Antonio da Silva
parent 72cfbc5bff
commit 81e3609c3b
4 changed files with 33 additions and 9 deletions

View File

@ -14,7 +14,6 @@ module SimpleForm
def input
label_method, value_method = detect_collection_methods
@builder.send(:"collection_#{input_type}", attribute_name, collection,
value_method, label_method, input_options, input_html_options)
end

View File

@ -10,7 +10,7 @@ module SimpleForm
def map_type(*types)
map_to = types.extract_options![:to]
raise ArgumentError, "You need to give :to as option to map_type" unless map_to
types.each { |t| mappings[t] = map_to }
self.mappings = mappings.merge types.each_with_object({}) { |t, m| m[t] = map_to }
end
end
end

View File

@ -1,11 +1,6 @@
require 'test_helper'
class BuilderTest < ActionView::TestCase
def with_concat_form_for(object, &block)
concat form_for(object, &block)
end
class BuilderTest < ActionView::TestCase
def with_custom_form_for(object, *args, &block)
with_concat_custom_form_for(object) do |f|
assert f.instance_of?(CustomFormBuilder)
@ -24,7 +19,7 @@ class BuilderTest < ActionView::TestCase
f.collection_check_boxes attribute, collection, value_method, text_method, options, html_options
end
end
# COLLECTION RADIO
test 'collection radio accepts a collection and generate inputs from value method' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
@ -296,4 +291,22 @@ class BuilderTest < ActionView::TestCase
end
end
end
test 'form with CustomMapTypeFormBuilder should use custom map type builder' do
with_concat_custom_mapping_form_for(:user) do |user|
assert user.instance_of?(CustomMapTypeFormBuilder)
end
end
test 'form with CustomMapTypeFormBuilder should use custom mapping' do
with_concat_custom_mapping_form_for(:user) do |user|
assert_equal SimpleForm::Inputs::StringInput, user.class.mappings[:custom_type]
end
end
test 'form without CustomMapTypeFormBuilder should not use custom mapping' do
with_concat_form_for(:user) do |user|
assert_equal nil, user.class.mappings[:custom_type]
end
end
end

View File

@ -36,6 +36,14 @@ module MiscHelpers
def custom_form_for(object, *args, &block)
simple_form_for(object, *(args << { :builder => CustomFormBuilder }), &block)
end
def custom_mapping_form_for(object, *args, &block)
simple_form_for(object, *(args << { :builder => CustomMapTypeFormBuilder }), &block)
end
def with_concat_custom_mapping_form_for(object, &block)
concat custom_mapping_form_for(object, &block)
end
end
class CustomFormBuilder < SimpleForm::FormBuilder
@ -43,3 +51,7 @@ class CustomFormBuilder < SimpleForm::FormBuilder
super(attribute_name, *(args << { :input_html => { :class => 'custom' } }), &block)
end
end
class CustomMapTypeFormBuilder < SimpleForm::FormBuilder
map_type :custom_type, :to => SimpleForm::Inputs::StringInput
end