Merge pull request #942 from plataformatec/issue-938

input_field uses the same wrapper as input but only with attribute components
This commit is contained in:
Vasiliy Ermolovich 2013-11-29 10:38:40 -08:00
commit c6e5ac3d43
3 changed files with 34 additions and 10 deletions

View File

@ -1,6 +1,7 @@
## master
### enhancements
* `input_field` uses the same wrapper as input but only with attribute components. [@nashby](https://github.com/nashby)
* Add wrapper mapping per form basis [@rcillo](https://github.com/rcillo) and [@bernardoamc](https://github.com/bernardoamc)
* Add `for` attribute to `label` when collections are rendered as radio or checkbox [@erichkist](https://github.com/erichkist), [@ulissesalmeida](https://github.com/ulissesalmeida) and [@fabioyamate](https://github.com/fabioyamate)
* Add `include_default_input_wrapper_class` config [@luizcosta](https://github.com/luizcosta)

View File

@ -108,16 +108,11 @@ module SimpleForm
#
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] || find_wrapper_mapping(input.input_type)
name.respond_to?(:render) ? name : SimpleForm.wrapper(name)
else
wrapper
end
input = find_input(attribute_name, options, &block)
wrapper = find_wrapper(input.input_type, options)
chosen.render input
wrapper.render input
end
alias :attribute :input
@ -140,7 +135,11 @@ module SimpleForm
options[:input_html] = options.except(:as, :collection, :label_method, :value_method, *ATTRIBUTE_COMPONENTS)
options = @defaults.deep_dup.deep_merge(options) if @defaults
SimpleForm::Wrappers::Root.new(ATTRIBUTE_COMPONENTS + [:input], wrapper: false).render find_input(attribute_name, options)
input = find_input(attribute_name, options)
wrapper = find_wrapper(input.input_type, options)
components = (wrapper.components & ATTRIBUTE_COMPONENTS) + [:input]
SimpleForm::Wrappers::Root.new(components, wrapper.options.merge(wrapper: false)).render input
end
# Helper for dealing with association selects/radios, generating the
@ -564,6 +563,14 @@ module SimpleForm
end
end
def find_wrapper(input_type, options)
if name = options[:wrapper] || find_wrapper_mapping(input_type)
name.respond_to?(:render) ? name : SimpleForm.wrapper(name)
else
wrapper
end
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 File

@ -88,14 +88,30 @@ class InputFieldTest < ActionView::TestCase
assert_select 'input[min=18]'
end
test 'builder input_field should use pattern component' do
test 'builder input_field should not use pattern component by default' do
with_concat_form_for(@other_validating_user) do |f|
f.input_field :country, as: :string
end
assert_no_select 'input[pattern="\w+"]'
end
test 'builder input_field should infer pattern from attributes' do
with_concat_form_for(@other_validating_user) do |f|
f.input_field :country, as: :string, pattern: true
end
assert_select 'input[pattern="\w+"]'
end
test 'builder input_field should accept custom patter' do
with_concat_form_for(@other_validating_user) do |f|
f.input_field :country, as: :string, pattern: '\d+'
end
assert_select 'input[pattern="\d+"]'
end
test 'builder input_field should use readonly component' do
with_concat_form_for(@other_validating_user) do |f|
f.input_field :age, as: :integer, readonly: true