Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add breadcrumbs to association pages #3082

Merged
merged 11 commits into from
Aug 6, 2024
2 changes: 1 addition & 1 deletion app/components/avo/views/resource_index_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
description: description,
cover_photo: resource.cover_photo,
data: {component: "resources-index"},
display_breadcrumbs: @reflection.blank?
display_breadcrumbs: @reflection.blank? || helpers.html_req_for_association_page?
) do |c| %>
<% c.with_name_slot do %>
<%= render Avo::PanelNameComponent.new name: title, url: (params[:turbo_frame].present? && linkable?) ? field.frame_url(add_turbo_frame: false) : nil, target: :_blank do |panel_name_component| %>
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class BaseController < ApplicationController

def index
@page_title = @resource.plural_name.humanize
@turbo_frame = params[:turbo_frame]

if html_req_for_association_page?
add_breadcrumb @record.class.to_s.pluralize, resources_path(resource: @parent_resource)
add_breadcrumb @parent_resource.record_title, resource_path(record: @record, resource: @parent_resource)
@turbo_frame = nil
end
add_breadcrumb @resource.plural_name.humanize

set_index_params
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/avo/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def chart_color(index)
Avo.configuration.branding.chart_colors[index % Avo.configuration.branding.chart_colors.length]
end

def html_req_for_association_page?
@reflection.present? && request.format == :html
end

private

# Taken from the original library
Expand Down
2 changes: 1 addition & 1 deletion app/views/avo/base/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= render Avo::TurboFrameWrapperComponent.new(params[:turbo_frame]) do %>
<%= render Avo::TurboFrameWrapperComponent.new(@turbo_frame) do %>
<%= render @component.new(
resource: @resource,
resources: @resources,
Expand Down
33 changes: 33 additions & 0 deletions spec/features/avo/breadcrumbs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,37 @@
it { is_expected.to have_css ".breadcrumbs" }
it { is_expected.to have_text "Home\n" }
end

describe "on a has_and_belongs_to_many turbo frame" do
let!(:user) { create(:user) }
let!(:team) { create(:team) }

it "displays breadcrumbs" do
url = "/admin/resources/users/#{user.slug}/teams?view=show&turbo_frame=has_and_belongs_to_many_field_show_teams"
binarygit marked this conversation as resolved.
Show resolved Hide resolved
visit url

expect(page).to have_selector ".breadcrumbs"
expect(page.find(".breadcrumbs").text).to eq "Home Users #{user.name} Teams"

breadcrumbs = find(".breadcrumbs")
expect(breadcrumbs).to have_link "Home"
expect(breadcrumbs).to have_link "Users"
expect(breadcrumbs).to have_link user.name.to_s
expect(breadcrumbs).to_not have_link "Teams"
end

it "displays breadcrumbs" do
url = "/admin/resources/teams/#{team.id}/team_members?view=show&turbo_frame=has_many_field_show_team_members"
visit url

expect(page).to have_selector ".breadcrumbs"
expect(page.find(".breadcrumbs").text).to eq "Home Teams #{team.name} Users"

breadcrumbs = find(".breadcrumbs")
expect(breadcrumbs).to have_link "Home"
expect(breadcrumbs).to have_link "Teams"
expect(breadcrumbs).to have_link team.name.to_s
expect(breadcrumbs).to_not have_link "Users"
end
end
end
Loading