diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e674e5e..487f128a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 44ae509e..94a0f5d5 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -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: diff --git a/test/form_builder/input_field_test.rb b/test/form_builder/input_field_test.rb index 755ad85b..1f399c1e 100644 --- a/test/form_builder/input_field_test.rb +++ b/test/form_builder/input_field_test.rb @@ -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