Add wrapper mapping per form basis.

Fixes #931.
This commit is contained in:
Ricardo de Cillo 2013-11-12 20:59:30 -02:00
parent 1d85b03463
commit 74a3ebf6ec
3 changed files with 22 additions and 1 deletions

View File

@ -107,6 +107,8 @@ module SimpleForm
# Custom wrappers for input types. This should be a hash containing an input
# type as key and the wrapper that will be used for all inputs with specified type.
# e.g { string: :string_wrapper, boolean: :boolean_wrapper }
# You can also set a wrapper mapping per form basis.
# e.g simple_form_for(@foo, wrapper_mapping: { checkbox: :bootstrap_checkbox })
mattr_accessor :wrapper_mappings
@@wrapper_mappings = nil

View File

@ -552,8 +552,16 @@ module SimpleForm
end
end
# Attempts to find a wrapper mapping. It follows the following rules:
#
# 1) It tries to find a wrapper for the current form
# 2) If not, it tries to find a config
def find_wrapper_mapping(input_type) #:nodoc:
SimpleForm.wrapper_mappings && SimpleForm.wrapper_mappings[input_type]
if options[:wrapper_mappings] && options[:wrapper_mappings][input_type]
options[:wrapper_mappings][input_type]
else
SimpleForm.wrapper_mappings && SimpleForm.wrapper_mappings[input_type]
end
end
# If cache_discovery is enabled, use the class level cache that persists

View File

@ -200,4 +200,15 @@ class WrapperTest < ActionView::TestCase
end
end
end
test 'use custom wrapper mapping per form basis' do
swap_wrapper :another do
with_concat_form_for @user, wrapper_mappings: { string: :another } do |f|
concat f.input :name
end
end
assert_select "section.custom_wrapper div.another_wrapper label"
assert_select "section.custom_wrapper div.another_wrapper input.string"
end
end