Skip to content

Commit

Permalink
fix: extract fields from sidebar and add them to permitted params (#2053
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
Paul-Bob authored Nov 23, 2023
1 parent cf48ef9 commit 83c7aa5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
34 changes: 20 additions & 14 deletions lib/avo/concerns/has_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions spec/dummy/app/avo/resources/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http://www.passwordmeter.com/" target="_blank">here</a>.'
field :password_confirmation, as: :password, name: "Password confirmation", required: false

field :dev, as: :heading, label: '<div class="underline uppercase font-bold">DEV</div>', 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: '<div class="underline uppercase font-bold">DEV</div>', 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
Expand Down Expand Up @@ -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: '<div class="underline uppercase font-bold">DEV</div>', 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

Expand Down
Original file line number Diff line number Diff line change
@@ -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 }

Expand All @@ -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

0 comments on commit 83c7aa5

Please sign in to comment.