Skip to content

Commit

Permalink
Feature: Add breadcrumbs to association pages (#3082)
Browse files Browse the repository at this point in the history
* Show breadcrumbs when association pages are opened directly

* Refactor: create helper

* Add breadcrumbs as links

* Display resource name instead of id

* Fix feature tests

* Use the turbo_frame_request helper

* Delete helper

* Remove helpers

* Add bang

* Update app/components/avo/views/resource_index_component.html.erb

Co-authored-by: Paul Bob <[email protected]>

* Remove query params

---------

Co-authored-by: Paul Bob <[email protected]>
  • Loading branch information
binarygit and Paul-Bob authored Aug 6, 2024
1 parent e5ae21d commit 03bd724
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
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? || (@reflection.present? && !helpers.turbo_frame_request?)
) 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
5 changes: 5 additions & 0 deletions app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class BaseController < ApplicationController

def index
@page_title = @resource.plural_name.humanize

if @reflection.present? && !turbo_frame_request?
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)
end
add_breadcrumb @resource.plural_name.humanize

set_index_params
Expand Down
9 changes: 6 additions & 3 deletions spec/features/avo/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@
expect(find("thead")).not_to have_text "User"
expect(page).to have_text comment.id
expect(page).to have_text "a comment"
expect(page).not_to have_text user.name
# breadcrumb contains the user's name
expect(page).to have_text user.name, count: 1
end
end

Expand All @@ -173,7 +174,8 @@
expect(page).to have_text comment.id
expect(page).to have_text "a comment"
expect(page).to have_text user.name
expect(page).not_to have_text project.name
# breadcrumb contains the project's name
expect(page).to have_text project.name, count: 1
end
end

Expand All @@ -192,7 +194,8 @@
expect(page).to have_text review.id
expect(page).to have_text "a review"
expect(page).to have_text user.name
expect(page).not_to have_text team.name
# breadcrumb contains the team's name
expect(page).to have_text team.name, count: 1
end
end
end
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"
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"
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

0 comments on commit 03bd724

Please sign in to comment.