Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #642 from nashby/wrapper-mappings

add config for wrapper mappings.
  • Loading branch information...
commit 74beafc2741761690dded5e9e7da08cbdc0f2650 2 parents c4bceaf + 44f1301
@nashby nashby authored
View
4 CHANGELOG.md
@@ -1,7 +1,9 @@
## 2.1.0.dev
### enhancements
-
+ * Allow to specify custom wrappers for input types.
+ ([@nashby](https://github.com/nashby))
+ Closes [#636](https://github.com/plataformatec/simple_form/issues/636)
* Use separate config file to do bootstrap specific configuration.
([@nashby](https://github.com/nashby))
View
4 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
View
6 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
View
9 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:
View
10 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
Please sign in to comment.
Something went wrong with that request. Please try again.