diff --git a/app/components/avo/tab_switcher_component.rb b/app/components/avo/tab_switcher_component.rb index f489d77318..67fd5b4e63 100644 --- a/app/components/avo/tab_switcher_component.rb +++ b/app/components/avo/tab_switcher_component.rb @@ -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 diff --git a/lib/avo/fields/field_extensions/visible_in_different_views.rb b/lib/avo/fields/field_extensions/visible_in_different_views.rb index f3a3a87611..2fe4f1372d 100644 --- a/lib/avo/fields/field_extensions/visible_in_different_views.rb +++ b/lib/avo/fields/field_extensions/visible_in_different_views.rb @@ -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) diff --git a/lib/avo/tab.rb b/lib/avo/tab.rb index e5e83873d2..63750e5365 100644 --- a/lib/avo/tab.rb +++ b/lib/avo/tab.rb @@ -65,4 +65,8 @@ def visible_on?(view) super(view) end end + + def has_a_single_item? + items.count == 1 + end end diff --git a/spec/system/avo/tabs_spec.rb b/spec/system/avo/tabs_spec.rb index 868f241cf2..7cdf90f8b5 100644 --- a/spec/system/avo/tabs_spec.rb +++ b/spec/system/avo/tabs_spec.rb @@ -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