From 8505eb60d9ce7f0f9a856bcd3330b2aaf7cf615f Mon Sep 17 00:00:00 2001 From: "Tan, Hsi-nien" Date: Sat, 16 May 2020 23:14:49 -0500 Subject: [PATCH 1/4] fix the indent of hidden field in create and edit actions --- app/helpers/rails_admin/form_builder.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/helpers/rails_admin/form_builder.rb b/app/helpers/rails_admin/form_builder.rb index baf0877bad..5c3b79a5d7 100644 --- a/app/helpers/rails_admin/form_builder.rb +++ b/app/helpers/rails_admin/form_builder.rb @@ -47,7 +47,9 @@ def field_wrapper_for(field, nested_in) end end else - field.nested_form ? field_for(field) : input_for(field) + @template.content_tag(:div, class: "form-group control-group #{field.type_css_class} #{field.css_class} #{'error' if field.errors.present?}", id: "#{dom_id(field)}_field") do + field.nested_form ? field_for(field) : input_for(field) + end end end From 4b54f747277646093a1a49c87b115a8b5d3b166b Mon Sep 17 00:00:00 2001 From: "Tan, Hsi-nien" Date: Sun, 17 May 2020 02:42:23 -0500 Subject: [PATCH 2/4] add the spec of field_wrapper_for --- spec/helpers/rails_admin/form_builder_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/helpers/rails_admin/form_builder_spec.rb b/spec/helpers/rails_admin/form_builder_spec.rb index a95cb9b9ad..e78ba6254c 100644 --- a/spec/helpers/rails_admin/form_builder_spec.rb +++ b/spec/helpers/rails_admin/form_builder_spec.rb @@ -3,6 +3,12 @@ RSpec.describe 'RailsAdmin::FormBuilder', type: :helper do describe '#generate' do before do + RailsAdmin.config Player do + create do + include_all_fields + field :number, :hidden + end + end allow(helper).to receive(:authorized?).and_return(true) (@object = Player.new).save @builder = RailsAdmin::FormBuilder.new(:player, @object, helper, {}) @@ -13,6 +19,10 @@ expect(@builder.generate(action: :create, model_config: RailsAdmin.config(Player))).not_to have_css('.field_with_errors') expect(@builder.generate(action: :create, model_config: RailsAdmin.config(Player))).to have_css('.control-group.error') end + + it 'hidden fields should be wrapper' do + expect(@builder.generate(action: :create, model_config: RailsAdmin.config(Player))).to match('form-group control-group hidden_type number_field') + end end describe '#object_infos' do From f1bf4e60fb65c00befda02ff041e878cdf677cb0 Mon Sep 17 00:00:00 2001 From: "Tan, Hsi-nien" Date: Sun, 17 May 2020 02:52:55 -0500 Subject: [PATCH 3/4] refactoring the field_wrapper_for --- app/helpers/rails_admin/form_builder.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/helpers/rails_admin/form_builder.rb b/app/helpers/rails_admin/form_builder.rb index 5c3b79a5d7..75d5886aaf 100644 --- a/app/helpers/rails_admin/form_builder.rb +++ b/app/helpers/rails_admin/form_builder.rb @@ -38,16 +38,14 @@ def fieldset_for(fieldset, nested_in) end def field_wrapper_for(field, nested_in) - if field.label - # do not show nested field if the target is the origin - unless nested_field_association?(field, nested_in) - @template.content_tag(:div, class: "form-group control-group #{field.type_css_class} #{field.css_class} #{'error' if field.errors.present?}", id: "#{dom_id(field)}_field") do + @template.content_tag(:div, class: "form-group control-group #{field.type_css_class} #{field.css_class} #{'error' if field.errors.present?}", id: "#{dom_id(field)}_field") do + if field.label + # do not show nested field if the target is the origin + unless nested_field_association?(field, nested_in) label(field.method_name, capitalize_first_letter(field.label), class: 'col-sm-2 control-label') + (field.nested_form ? field_for(field) : input_for(field)) end - end - else - @template.content_tag(:div, class: "form-group control-group #{field.type_css_class} #{field.css_class} #{'error' if field.errors.present?}", id: "#{dom_id(field)}_field") do + else field.nested_form ? field_for(field) : input_for(field) end end From 01e17006ca6733354ad6bf8587c4c2b44c84a583 Mon Sep 17 00:00:00 2001 From: "Tan, Hsi-nien" Date: Sun, 17 May 2020 07:38:45 -0500 Subject: [PATCH 4/4] Hide association input for deeply nested fields --- app/helpers/rails_admin/form_builder.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/helpers/rails_admin/form_builder.rb b/app/helpers/rails_admin/form_builder.rb index 75d5886aaf..facfc84ea9 100644 --- a/app/helpers/rails_admin/form_builder.rb +++ b/app/helpers/rails_admin/form_builder.rb @@ -38,13 +38,12 @@ def fieldset_for(fieldset, nested_in) end def field_wrapper_for(field, nested_in) + # do not show nested field if the target is the origin + return if nested_field_association?(field, nested_in) @template.content_tag(:div, class: "form-group control-group #{field.type_css_class} #{field.css_class} #{'error' if field.errors.present?}", id: "#{dom_id(field)}_field") do if field.label - # do not show nested field if the target is the origin - unless nested_field_association?(field, nested_in) - label(field.method_name, capitalize_first_letter(field.label), class: 'col-sm-2 control-label') + - (field.nested_form ? field_for(field) : input_for(field)) - end + label(field.method_name, capitalize_first_letter(field.label), class: 'col-sm-2 control-label') + + (field.nested_form ? field_for(field) : input_for(field)) else field.nested_form ? field_for(field) : input_for(field) end