From 21d03ff583f037c0d9558090b4b3831eb1c5ae0d Mon Sep 17 00:00:00 2001 From: Ian Vaughan Date: Wed, 28 May 2014 11:16:51 +0100 Subject: [PATCH] Only output the wrapper if the block yields content This change allows the wrappers to not output their rendered HTML when when the block yields no content. e.g. when defining an optional component : ba.wrapper tag: :div, class: 'form-helper -message -hint' do |bb| bb.optional :hint, wrap_with: { tag: :p } end and that component is not used in a template : <%= form.input :name %> Then the HTML output still contains the wrapper output :
If this behaviour is not desired, then the output of the empty tags can be disabled via the `remove_empty` option. ba.wrapper tag: :div, class: 'form-helper -message -hint', remove_empty: true do |bb| bb.optional :hint, wrap_with: { tag: :p } end The above config will not output any wrapper HTML. --- lib/simple_form/wrappers/many.rb | 1 + test/form_builder/hint_test.rb | 25 +++++++++++++++++++++++++ test/support/misc_helpers.rb | 16 ++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/lib/simple_form/wrappers/many.rb b/lib/simple_form/wrappers/many.rb index deff9995..ebc01fc2 100644 --- a/lib/simple_form/wrappers/many.rb +++ b/lib/simple_form/wrappers/many.rb @@ -50,6 +50,7 @@ module SimpleForm def wrap(input, options, content) return content if options[namespace] == false + return if defaults[:optional] && content.empty? tag = (namespace && options[:"#{namespace}_tag"]) || @defaults[:tag] return content unless tag diff --git a/test/form_builder/hint_test.rb b/test/form_builder/hint_test.rb index ca783676..af915611 100644 --- a/test/form_builder/hint_test.rb +++ b/test/form_builder/hint_test.rb @@ -141,4 +141,29 @@ class HintTest < ActionView::TestCase assert_select 'div.omg_hint', "can't be blank" end end + + test 'optional hint displays when given' do + swap_wrapper :default, self.custom_wrapper_with_optional_div do + with_form_for @user, :name, hint: "can't be blank" + assert_select 'section.custom_wrapper div.no_output_wrapper p.omg_hint', "can't be blank" + assert_select 'p.omg_hint' + end + end + + test 'optional hint displays empty wrapper when no hint given' do + swap_wrapper :default, self.custom_wrapper_with_optional_div do + with_form_for @user, :name + assert_select 'section.custom_wrapper div.no_output_wrapper' + assert_no_select 'p.omg_hint' + end + end + + test 'optional hint displays no wrapper or hint when no hint and override is given' do + swap_wrapper :default, self.custom_wrapper_with_optional_div_and_override do + with_form_for @user, :name + assert_no_select 'section.custom_wrapper div.no_output_wrapper' + assert_no_select 'div.no_output_wrapper' + assert_no_select 'p.omg_hint' + end + end end diff --git a/test/support/misc_helpers.rb b/test/support/misc_helpers.rb index 1826bc93..71313c86 100644 --- a/test/support/misc_helpers.rb +++ b/test/support/misc_helpers.rb @@ -68,6 +68,22 @@ module MiscHelpers end end + def custom_wrapper_with_optional_div + SimpleForm.build tag: :section, class: "custom_wrapper" do |b| + b.wrapper tag: :div, class: 'no_output_wrapper' do |ba| + ba.optional :hint, wrap_with: { tag: :p, class: 'omg_hint' } + end + end + end + + def custom_wrapper_with_optional_div_and_override + SimpleForm.build tag: :section, class: "custom_wrapper" do |b| + b.wrapper tag: :div, class: 'no_output_wrapper', optional: true do |ba| + ba.optional :hint, wrap_with: { tag: :p, class: 'omg_hint' } + end + end + end + def custom_wrapper_with_input_class SimpleForm.build tag: :div, class: "custom_wrapper" do |b| b.use :label