Skip to content

Commit

Permalink
fix: tab visibility (#1826)
Browse files Browse the repository at this point in the history
* fix: tab visibility

* improve readability (try)

* review changes

* if instead unless

* hide has many tab by policy
  • Loading branch information
Paul-Bob authored Jul 5, 2023
1 parent 1cf596c commit ad8725a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
41 changes: 19 additions & 22 deletions app/components/avo/tab_switcher_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,29 @@ def selected?(tab)
# Because the developer hasn't specified that it should be visible on edit views (with the show_on: :edit option),
# the field should not be visible in the item switcher either.
def visible_items
tabs.select do |item|
visible = true

if item.items.blank?
visible = false
end
tabs.select do |tab|
next false if tab.items.blank?
next false if tab.is_field? && !tab.authorized?
next false if tab.has_a_single_item? && !single_item_visible?(tab.items.first)
next false if !tab.visible?
next false if !tab.visible_on?(view)

true
end
end

first_item = item.items.first
if item.items.count == 1 && first_item.is_field? && first_item.has_own_panel? && !first_item.visible_on?(view)
# Return nil if tab contians a has_many type of fields and it's hidden in current view
visible = false
end
private

if item.respond_to?(:visible_on?)
visible = item.visible_on? view
end
def single_item_visible?(item)
# Item is visible if is not a field or don't have its own panel
return true if !item.is_field?
return true if !item.has_own_panel?

if item.respond_to?(:visible?)
visible = item.visible?
end
return false if !item.visible_on?(view)

if item.respond_to?(:authorized?)
visible = item.authorized?
end
# If item is hydrated with the correct resource and is not authorized, it's not visible
return false if item.resource.present? && !item.authorized?

visible
end
true
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(id = nil, **args, &block)
def visible_on?(view)
raise "No view specified on visibility check." if view.blank?

send :"show_on_#{view.to_s}"
send :"show_on_#{view}"
end

def show_on(*where)
Expand Down
4 changes: 4 additions & 0 deletions lib/avo/tab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ def visible_on?(view)
super(view)
end
end

def has_a_single_item?
items.count == 1
end
end
16 changes: 16 additions & 0 deletions spec/system/avo/tabs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@
end
end
end

it "hides has_many tab header button by policy" do
visit avo.resources_user_path user

expect(page).to have_selector("a.button-component[data-tabs-id-param='Teams']", text: "Teams")

UserPolicy.define_method(:view_teams?) do
false
end

visit avo.resources_user_path user

expect(page).to have_no_selector("a.button-component[data-tabs-id-param='Teams']", text: "Teams")

UserPolicy.remove_method(:view_teams?)
end
end

context "edit" do
Expand Down

0 comments on commit ad8725a

Please sign in to comment.