Skip to content

Commit

Permalink
add config for wrapper mappings.
Browse files Browse the repository at this point in the history
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 }
  • Loading branch information
nashby committed Sep 7, 2012
1 parent c4bceaf commit 4c5a5b9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions lib/simple_form.rb
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions lib/simple_form/form_builder.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions test/form_builder/wrapper_test.rb
Expand Up @@ -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

0 comments on commit 4c5a5b9

Please sign in to comment.