From 4c5a5b9fbc888eb4254ac2ab50b6638dbbb3b37f Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Sat, 11 Aug 2012 15:52:10 +0300 Subject: [PATCH] add config for wrapper mappings. This should be a hash containing an input type as key and the wrapper that will be used for all inputs with specified type config.wrapper_mapings = { :string => :string_wrapper } --- .../templates/config/initializers/simple_form.rb | 4 ++++ lib/simple_form.rb | 6 ++++++ lib/simple_form/form_builder.rb | 9 +++++++-- test/form_builder/wrapper_test.rb | 10 ++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/generators/simple_form/templates/config/initializers/simple_form.rb b/lib/generators/simple_form/templates/config/initializers/simple_form.rb index 59c7d2528..e3f8d0962 100644 --- a/lib/generators/simple_form/templates/config/initializers/simple_form.rb +++ b/lib/generators/simple_form/templates/config/initializers/simple_form.rb @@ -118,6 +118,10 @@ # matches the regexp as value. # config.input_mappings = { /count/ => :integer } + # 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. + # config.wrapper_mappings = { :string => :prepend } + # Default priority for time_zone inputs. # config.time_zone_priority = nil diff --git a/lib/simple_form.rb b/lib/simple_form.rb index cd5e97fa7..adf2170d5 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -95,6 +95,12 @@ module SimpleForm mattr_accessor :input_mappings @@input_mappings = nil + # 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 } + mattr_accessor :wrapper_mappings + @@wrapper_mappings = nil + # Default priority for time_zone inputs. mattr_accessor :time_zone_priority @@time_zone_priority = nil diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 7ce448b5e..91abc1d2e 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -104,15 +104,16 @@ def initialize(*) #:nodoc: # def input(attribute_name, options={}, &block) options = @defaults.deep_dup.deep_merge(options) if @defaults + input = find_input(attribute_name, options, &block) chosen = - if name = options[:wrapper] + if name = options[:wrapper] || find_wrapper_mapping(input.input_type) name.respond_to?(:render) ? name : SimpleForm.wrapper(name) else wrapper end - chosen.render find_input(attribute_name, options, &block) + chosen.render input end alias :attribute :input @@ -444,6 +445,10 @@ def find_mapping(input_type) #:nodoc: end end + def find_wrapper_mapping(input_type) + SimpleForm.wrapper_mappings && SimpleForm.wrapper_mappings[input_type] + end + # If cache_discovery is enabled, use the class level cache that persists # between requests, otherwise use the instance one. def discovery_cache #:nodoc: diff --git a/test/form_builder/wrapper_test.rb b/test/form_builder/wrapper_test.rb index 914c9eaff..f74a04b41 100644 --- a/test/form_builder/wrapper_test.rb +++ b/test/form_builder/wrapper_test.rb @@ -168,4 +168,14 @@ class WrapperTest < ActionView::TestCase with_form_for @user, :name, :wrapper => :not_found end end + + test 'use wrapper for specified in config mapping' do + swap_wrapper :another do + swap SimpleForm, :wrapper_mappings => { :string => :another } do + with_form_for @user, :name + assert_select "section.custom_wrapper div.another_wrapper label" + assert_select "section.custom_wrapper div.another_wrapper input.string" + end + end + end end