From 83c7aa5a22535dc1a87c7f1ac64b8610c3ef0073 Mon Sep 17 00:00:00 2001 From: Paul Bob <69730720+Paul-Bob@users.noreply.github.com> Date: Thu, 23 Nov 2023 16:21:16 +0200 Subject: [PATCH] fix: extract fields from sidebar and add them to permitted params (#2053) * fix: extract fields from sidebar and add them to permitted params * avoid sidebar extraction on index * refactor * refactor to reduce complexity * complexity * test * custom_css only on show * custom_css hide on forms --- lib/avo/concerns/has_items.rb | 34 +++++++++++-------- spec/dummy/app/avo/resources/user.rb | 10 ++++-- .../avo/resource_sidebar_spec.rb | 18 +++++++++- 3 files changed, 45 insertions(+), 17 deletions(-) rename spec/{features => system}/avo/resource_sidebar_spec.rb (57%) diff --git a/lib/avo/concerns/has_items.rb b/lib/avo/concerns/has_items.rb index 28776f83aa..8d5657b88e 100644 --- a/lib/avo/concerns/has_items.rb +++ b/lib/avo/concerns/has_items.rb @@ -87,19 +87,19 @@ def only_fields(only_root: false) unless only_root # Dive into panels to fetch their fields if item.is_panel? - fields << extract_fields_from_items(item) + fields << extract_fields(item) end # Dive into tabs to fetch their fields if item.is_tab_group? item.items.map do |tab| - fields << extract_fields_from_items(tab) + fields << extract_fields(tab) end end # Dive into sidebar to fetch their fields if item.is_sidebar? - fields << extract_fields_from_items(item) + fields << extract_fields(item) end end @@ -108,11 +108,11 @@ def only_fields(only_root: false) end if item.is_row? - fields << extract_fields_from_items(tab) + fields << extract_fields(tab) end if item.is_main_panel? - fields << extract_fields_from_items(item) + fields << extract_fields(item) end end @@ -309,18 +309,24 @@ def set_target_to_top(fields) end end - def extract_fields_from_items(thing) - fields = [] - - thing.items.each do |item| + # Extracts fields from a structure + # Structures can be panels, rows and sidebars + def extract_fields(structure) + structure.items.map do |item| if item.is_field? - fields << item - elsif item.is_panel? || item.is_row? - fields << extract_fields_from_items(item) + item + elsif extractable_structure?(item) + extract_fields(item) + else + nil end - end + end.compact + end - fields + # Extractable structures are panels, rows and sidebars + # Sidebars are only extractable if they are not on the index view + def extractable_structure?(structure) + structure.is_panel? || structure.is_row? || (structure.is_sidebar? && !view.index?) end # Standalone items are fields that don't have their own panel diff --git a/spec/dummy/app/avo/resources/user.rb b/spec/dummy/app/avo/resources/user.rb index c2f1d7ac28..e5da9871f9 100644 --- a/spec/dummy/app/avo/resources/user.rb +++ b/spec/dummy/app/avo/resources/user.rb @@ -121,8 +121,10 @@ def main_panel_fields field :password, as: :password, name: "User Password", required: false, only_on: :forms, help: 'You may verify the password strength here.' field :password_confirmation, as: :password, name: "Password confirmation", required: false - field :dev, as: :heading, label: '