Fixed bug in MapType module when subclassing FormBuilder.
This commit is contained in:
parent
72cfbc5bff
commit
81e3609c3b
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue