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: '
DEV
', as_html: true - field :custom_css, as: :code, theme: "dracula", language: "css", help: "This enables you to edit the user's custom styles.", height: "250px" + with_options hide_on: :forms do + field :dev, as: :heading, label: '
DEV
', as_html: true + field :custom_css, as: :code, theme: "dracula", language: "css", help: "This enables you to edit the user's custom styles.", height: "250px" + end field :team_id, as: :hidden, default: 0 # For testing purposes test_sidebar @@ -166,6 +168,10 @@ def main_panel_sidebar field :outside_link, as: :text, only_on: [:show], format_using: -> { link_to("hey", value, target: "_blank") } do main_app.hey_url end + with_options only_on: :forms do + field :dev, as: :heading, label: '
DEV
', as_html: true + field :custom_css, as: :code, theme: "dracula", language: "css", help: "This enables you to edit the user's custom styles.", height: "250px" + end end end diff --git a/spec/features/avo/resource_sidebar_spec.rb b/spec/system/avo/resource_sidebar_spec.rb similarity index 57% rename from spec/features/avo/resource_sidebar_spec.rb rename to spec/system/avo/resource_sidebar_spec.rb index 176f2339c0..cf7dfd015c 100644 --- a/spec/features/avo/resource_sidebar_spec.rb +++ b/spec/system/avo/resource_sidebar_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.feature "ResourceSidebars", type: :feature do +RSpec.feature "ResourceSidebars", type: :system do let(:fish) { create :fish } let(:team) { create :team } @@ -21,4 +21,20 @@ expect(page).to have_css ".resource-sidebar-component" end + + it "allow fields to be eddited on sidebar" do + admin.update!(custom_css: "") + visit avo.edit_resources_user_path(admin) + + within ".CodeMirror" do + current_scope.click + field = current_scope.find("textarea", visible: false) + field.send_keys "Some custom css" + end + + click_on "Save" + wait_for_loaded + + expect(page).to have_css(".CodeMirror-code", text: "Some custom css") + end end