Skip to content

Commit

Permalink
Merge pull request #1443 from mlibrary/breadcrumbs-with-parent
Browse files Browse the repository at this point in the history
Resolves #1384, modify breadcrumbs for child publisher's views
  • Loading branch information
jmcglone authored Dec 18, 2017
2 parents 95f2aa7 + 4b69a82 commit bbea69c
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 18 deletions.
43 changes: 43 additions & 0 deletions app/helpers/breadcrumbs_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module BreadcrumbsHelper
def breadcrumbs
crumbs = if @presenter.present? && @presenter.class == Hyrax::FileSetPresenter
breadcrumbs_for_file_set(@presenter.monograph_presenter.subdomain, @presenter)
elsif @monograph_presenter.present?
breadcrumbs_for_monograph(@monograph_presenter.subdomain, @monograph_presenter)
end
crumbs || []
end

private

def breadcrumbs_for_monograph(subdomain, presenter)
press = Press.where(subdomain: subdomain)&.first
return [] if press.blank?

crumbs = possible_parent(press)
crumbs << { href: "", text: presenter.page_title, class: "active" }
end

def breadcrumbs_for_file_set(subdomain, presenter)
press = Press.where(subdomain: subdomain)&.first
return [] if press.blank?

crumbs = possible_parent(press)
crumbs << { href: main_app.monograph_catalog_path(presenter.monograph_id), text: presenter.monograph.page_title, class: "" }
crumbs << { href: "", text: presenter.page_title, class: "active" }
end

def possible_parent(press)
crumbs = []
if press.parent_id.present?
parent = Press.find(press.parent_id)
crumbs << { href: "/#{parent.subdomain}", text: translate('monograph_catalog.index.home'), class: "" }
crumbs << { href: "/#{press.subdomain}", text: press.name, class: "" }
else
crumbs << { href: "/#{press.subdomain}", text: translate('monograph_catalog.index.home'), class: "" }
end
crumbs
end
end
10 changes: 1 addition & 9 deletions app/views/hyrax/file_sets/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@
</div>

<!-- breadcrumbs -->
<div class="row">
<nav class="breadcrumbs col-sm-12">
<ol class="breadcrumb">
<li><a href=<%= "/#{@presenter.monograph.subdomain}" %>>Home</a></li>
<li><a href=<%= main_app.monograph_catalog_path(@presenter.monograph_id) %>><%= @presenter.monograph.page_title %></a></li>
<li class="active"><%= @presenter.page_title %></li>
</ol>
</nav>
</div>
<%= render 'shared/breadcrumbs' %>

<!-- asset navigation -->
<div class="row">
Expand Down
9 changes: 1 addition & 8 deletions app/views/monograph_catalog/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@
<a href="#" class="btn btn-default btn-lg" role="button" data-dismiss="alert" aria-label="Close" onclick="Cookies.set('survey', 'ignore', { expires: 15 });">No thanks.</a>
</div>
<!-- breadcrumbs -->
<div class="row">
<nav class="breadcrumbs col-sm-12">
<ol class="breadcrumb">
<li><a href=<%= "/#{@monograph_presenter.subdomain}" %>><%= t('.home') %></a></li>
<li class="active"><%= @monograph_presenter.page_title %></li>
</ol>
</nav>
</div>
<%= render 'shared/breadcrumbs' %>
<% end %><!-- provide :page_header -->
<% if @monograph_presenter.epub? %>
<%= render 'index_epub' %>
Expand Down
9 changes: 9 additions & 0 deletions app/views/shared/_breadcrumbs.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="row">
<nav class="breadcrumbs col-sm-12">
<ol class="breadcrumb">
<% breadcrumbs.each do |crumb| %>
<%= content_tag(:li, crumb[:href].blank? ? crumb[:text] : content_tag(:a, crumb[:text], href: crumb[:href]), class: crumb[:class]) %>
<% end %>
</ol>
</nav>
</div>
87 changes: 87 additions & 0 deletions spec/helpers/breadcrumbs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe BreadcrumbsHelper do
let!(:press) { create(:press, subdomain: "blue", name: "Blue Press") }
let(:monograph_presenter) { Hyrax::MonographPresenter.new(SolrDocument.new(id: 1,
title_tesim: ["Monograph Title"],
press_tesim: "blue",
has_model_ssim: ['Monograph']), nil) }

describe "when on a monograph catalog page" do
context "with no parent press" do
it "returns the right breadcrumbs" do
@monograph_presenter = monograph_presenter
expect(breadcrumbs).to match_array([{ href: "/blue",
text: "Home",
class: "" },
{ href: "",
text: "Monograph Title",
class: "active" }])
end
end

context "with a parent press" do
let(:parent) { create(:press, subdomain: "maize", name: "Maize Press") }
it "returns the right breadcrumbs" do
@monograph_presenter = monograph_presenter
press.parent_id = parent.id
press.save!
expect(breadcrumbs).to match_array([{ href: "/maize",
text: "Home",
class: "" },
{ href: "/blue",
text: "Blue Press",
class: "" },
{ href: "",
text: "Monograph Title",
class: "active" }])
end
end
end

describe "when on a file_set/asset page" do
let(:file_set_presenter) { Hyrax::FileSetPresenter.new(SolrDocument.new(id: 2,
title_tesim: ["FileSet Title"],
has_model_ssim: ["FileSet"],
monograph_id_ssim: 1), nil) }
context "with no parent press" do
it "returns the right breadcrumbs" do
@presenter = file_set_presenter
@presenter.monograph_presenter = monograph_presenter
expect(breadcrumbs).to match_array([{ href: "/blue",
text: "Home",
class: "" },
{ href: "/concern/monographs/1",
text: "Monograph Title",
class: "" },
{ href: "",
text: "FileSet Title",
class: "active" }])
end
end

context "with a parent press" do
let(:parent) { create(:press, subdomain: "maize", name: "Maize Press") }
it "returns the right breadcrumbs" do
@presenter = file_set_presenter
@presenter.monograph_presenter = monograph_presenter
press.parent_id = parent.id
press.save!
expect(breadcrumbs).to match_array([{ href: "/maize",
text: "Home",
class: "" },
{ href: "/blue",
text: "Blue Press",
class: "" },
{ href: "/concern/monographs/1",
text: "Monograph Title",
class: "" },
{ href: "",
text: "FileSet Title",
class: "active" }])
end
end
end
end
3 changes: 2 additions & 1 deletion spec/views/monograph_catalog/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def t(value)
subject { view.view_flow.content[:page_header] }
let(:page_title) { 'PAGE-TITLE' }
let(:subdomain) { 'SUBDOMAIN' }
let!(:press) { create(:press, subdomain: subdomain) }
before do
allow(monograph_presenter).to receive(:page_title).and_return(page_title)
allow(monograph_presenter).to receive(:subdomain).and_return(subdomain)
Expand All @@ -69,7 +70,7 @@ def t(value)
debug_puts subject.to_s
is_expected.not_to be_empty
# Breadcrumbs
is_expected.to match(/<li>.*?<a.*?href=\/#{subdomain}.*?>.*?#{t('.home')}.*?<\/a>.*?<\/li>/m)
is_expected.to match(/<li.*?>.*?<a.*?href="\/#{subdomain}">Home<\/a>.*?<\/li>/m)
is_expected.to match(/<li.*?active.*?>.*?#{page_title}.*?<\/li>/m)
end
end
Expand Down

0 comments on commit bbea69c

Please sign in to comment.