Skip to content
This repository

add config for wrapper mappings. #642

Merged
merged 2 commits into from almost 2 years ago

3 participants

Vasiliy Ermolovich Rafael Mendonça França Carlos Antonio da Silva
Vasiliy Ermolovich
Collaborator

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 }

related to #636

Rafael Mendonça França
Collaborator

:+1:

Vasiliy Ermolovich
Collaborator

we need some @carlosantoniodasilva's love here :)

...ple_form/templates/config/initializers/simple_form.rb
... ... @@ -118,6 +118,10 @@
118 118 # matches the regexp as value.
119 119 # config.input_mappings = { /count/ => :integer }
120 120
  121 + # Custom wrappers for input types. This should be a hash containing an input
  122 + # type as key and the wrapper that will be used for all inputs with specified type
1
Carlos Antonio da Silva Collaborator

Mind finishing with a . (and in lib/simple_form as well... very nitpicky, I know :P)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Carlos Antonio da Silva
Collaborator

Looks great <3. Changelog and merge it!

Sorry for being sooo delayed. Seems that love is a bit scarce these days, so there it is: :yellow_heart::green_heart::heart::blue_heart::purple_heart:

nashby added some commits
Vasiliy Ermolovich nashby 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 }
4c5a5b9
Vasiliy Ermolovich nashby update CHANGELOG 44f1301
Vasiliy Ermolovich nashby merged commit 74beafc into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Sep 07, 2012
Vasiliy Ermolovich nashby 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 }
4c5a5b9
Vasiliy Ermolovich nashby update CHANGELOG 44f1301
This page is out of date. Refresh to see the latest.
4 CHANGELOG.md
Source Rendered
... ... @@ -1,7 +1,9 @@
1 1 ## 2.1.0.dev
2 2
3 3 ### enhancements
4   -
  4 + * Allow to specify custom wrappers for input types.
  5 + ([@nashby](https://github.com/nashby))
  6 + Closes [#636](https://github.com/plataformatec/simple_form/issues/636)
5 7 * Use separate config file to do bootstrap specific configuration.
6 8 ([@nashby](https://github.com/nashby))
7 9
4 lib/generators/simple_form/templates/config/initializers/simple_form.rb
@@ -118,6 +118,10 @@
118 118 # matches the regexp as value.
119 119 # config.input_mappings = { /count/ => :integer }
120 120
  121 + # Custom wrappers for input types. This should be a hash containing an input
  122 + # type as key and the wrapper that will be used for all inputs with specified type.
  123 + # config.wrapper_mappings = { :string => :prepend }
  124 +
121 125 # Default priority for time_zone inputs.
122 126 # config.time_zone_priority = nil
123 127
6 lib/simple_form.rb
@@ -95,6 +95,12 @@ module SimpleForm
95 95 mattr_accessor :input_mappings
96 96 @@input_mappings = nil
97 97
  98 + # Custom wrappers for input types. This should be a hash containing an input
  99 + # type as key and the wrapper that will be used for all inputs with specified type.
  100 + # e.g { :string => :string_wrapper, :boolean => :boolean_wrapper }
  101 + mattr_accessor :wrapper_mappings
  102 + @@wrapper_mappings = nil
  103 +
98 104 # Default priority for time_zone inputs.
99 105 mattr_accessor :time_zone_priority
100 106 @@time_zone_priority = nil
9 lib/simple_form/form_builder.rb
@@ -104,15 +104,16 @@ def initialize(*) #:nodoc:
104 104 #
105 105 def input(attribute_name, options={}, &block)
106 106 options = @defaults.deep_dup.deep_merge(options) if @defaults
  107 + input = find_input(attribute_name, options, &block)
107 108
108 109 chosen =
109   - if name = options[:wrapper]
  110 + if name = options[:wrapper] || find_wrapper_mapping(input.input_type)
110 111 name.respond_to?(:render) ? name : SimpleForm.wrapper(name)
111 112 else
112 113 wrapper
113 114 end
114 115
115   - chosen.render find_input(attribute_name, options, &block)
  116 + chosen.render input
116 117 end
117 118 alias :attribute :input
118 119
@@ -444,6 +445,10 @@ def find_mapping(input_type) #:nodoc:
444 445 end
445 446 end
446 447
  448 + def find_wrapper_mapping(input_type)
  449 + SimpleForm.wrapper_mappings && SimpleForm.wrapper_mappings[input_type]
  450 + end
  451 +
447 452 # If cache_discovery is enabled, use the class level cache that persists
448 453 # between requests, otherwise use the instance one.
449 454 def discovery_cache #:nodoc:
10 test/form_builder/wrapper_test.rb
@@ -168,4 +168,14 @@ class WrapperTest < ActionView::TestCase
168 168 with_form_for @user, :name, :wrapper => :not_found
169 169 end
170 170 end
  171 +
  172 + test 'use wrapper for specified in config mapping' do
  173 + swap_wrapper :another do
  174 + swap SimpleForm, :wrapper_mappings => { :string => :another } do
  175 + with_form_for @user, :name
  176 + assert_select "section.custom_wrapper div.another_wrapper label"
  177 + assert_select "section.custom_wrapper div.another_wrapper input.string"
  178 + end
  179 + end
  180 + end
171 181 end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.