Skip to content

Commit

Permalink
File show page has a breadcrumb for its work.
Browse files Browse the repository at this point in the history
Fixes #1653
  • Loading branch information
jcoyne committed Mar 29, 2016
1 parent 7635923 commit b11f8f3
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 185 deletions.
Expand Up @@ -62,6 +62,10 @@ def update

protected

def add_breadcrumb_for_controller
add_breadcrumb I18n.t('sufia.dashboard.my.works'), sufia.dashboard_works_path
end

def _prefixes
# This allows us to use the templates in curation_concerns/base
@_prefixes ||= super + ['curation_concerns/base']
Expand Down
15 changes: 2 additions & 13 deletions app/controllers/concerns/sufia/breadcrumbs.rb
Expand Up @@ -25,23 +25,12 @@ def trail_from_referer
end
end

# Override these in your controller
def add_breadcrumb_for_controller
case controller_name
when 'file_sets'.freeze, 'my/works'.freeze, 'batch_edits'.freeze, 'stats'.freeze
add_breadcrumb I18n.t('sufia.dashboard.my.works'), sufia.dashboard_works_path
when 'my/collections'.freeze
add_breadcrumb I18n.t('sufia.dashboard.my.collections'), sufia.dashboard_collections_path
end
end

# Override these in your controller
def add_breadcrumb_for_action
case controller_name
when 'file_sets'.freeze
add_breadcrumb I18n.t("sufia.file_set.browse_view"), Rails.application.routes.url_helpers.curation_concerns_file_set_path(params["id"]) if action_name.eql? 'edit'
when 'stats'.freeze
add_breadcrumb I18n.t("sufia.file_set.browse_view"), Rails.application.routes.url_helpers.curation_concerns_file_set_path(params["id"]) if action_name.eql? 'file'
add_breadcrumb I18n.t("sufia.work.browse_view"), Rails.application.routes.url_helpers.curation_concerns_file_set_path(params["id"]) if action_name.eql? 'work'
end
end
end
end
14 changes: 14 additions & 0 deletions app/controllers/concerns/sufia/file_sets_controller_behavior.rb
Expand Up @@ -40,6 +40,20 @@ def stats
def citation
end

def add_breadcrumb_for_controller
add_breadcrumb I18n.t('sufia.dashboard.my.works'), sufia.dashboard_works_path
end

def add_breadcrumb_for_action
case action_name
when 'edit'.freeze
add_breadcrumb I18n.t("sufia.file_set.browse_view"), main_app.curation_concerns_file_set_path(params["id"])
when 'show'.freeze
add_breadcrumb presenter.parent.title, sufia.polymorphic_path(presenter.parent)
add_breadcrumb presenter.title, main_app.polymorphic_path(presenter)
end
end

protected

def _prefixes
Expand Down
Expand Up @@ -2,8 +2,8 @@ module Sufia::SingularSubresourceController
extend ActiveSupport::Concern

included do
load_and_authorize_resource ::FileSet, only: :file, id_param: :id
load_and_authorize_resource ::GenericWork, only: :work, id_param: :id
load_and_authorize_resource :file, class: 'FileSet', only: :file, id_param: :id
load_and_authorize_resource :work, class: 'GenericWork', only: :work, id_param: :id
end

# Overriding the default behavior from Hydra::Core::ControllerBehavior
Expand Down
17 changes: 16 additions & 1 deletion app/controllers/stats_controller.rb
@@ -1,6 +1,6 @@
class StatsController < ApplicationController
include Sufia::Breadcrumbs
include Sufia::SingularSubresourceController
include Sufia::Breadcrumbs

before_action :build_breadcrumbs, only: [:work, :file]

Expand All @@ -11,4 +11,19 @@ def work
def file
@stats = FileUsage.new(params[:id])
end

protected

def add_breadcrumb_for_controller
add_breadcrumb I18n.t('sufia.dashboard.my.works'), sufia.dashboard_works_path
end

def add_breadcrumb_for_action
case action_name
when 'file'.freeze
add_breadcrumb I18n.t("sufia.file_set.browse_view"), main_app.curation_concerns_file_set_path(params["id"])
when 'work'.freeze
add_breadcrumb I18n.t("sufia.work.browse_view"), polymorphic_path(@work)
end
end
end
7 changes: 7 additions & 0 deletions app/presenters/sufia/file_set_presenter.rb
Expand Up @@ -48,6 +48,13 @@ def audit_status
audit_service.logged_audit_status
end

def parent
ids = solr_document.fetch('generic_work_ids_ssim')
@parent_presenter ||= CurationConcerns::PresenterFactory.build_presenters(ids,
WorkShowPresenter,
current_ability).first
end

def audit_service
# model = solr_document.to_model # See https://github.com/projecthydra-labs/hydra-pcdm/issues/197
model = FileSet.find(id)
Expand Down
5 changes: 4 additions & 1 deletion spec/controllers/batch_edits_controller_spec.rb
Expand Up @@ -10,7 +10,7 @@

routes { Internal::Application.routes }

describe "edit" do
describe "#edit" do
before do
@one = GenericWork.new(creator: ["Fred"], title: ["abc"], language: ['en'])
@one.apply_depositor_metadata('mjg36')
Expand All @@ -24,6 +24,9 @@
end

it "is successful" do
allow(controller.request).to receive(:referer).and_return('foo')
expect(controller).to receive(:add_breadcrumb).with(I18n.t('sufia.dashboard.title'), Sufia::Engine.routes.url_helpers.dashboard_index_path)
expect(controller).to receive(:add_breadcrumb).with(I18n.t('sufia.dashboard.my.works'), Sufia::Engine.routes.url_helpers.dashboard_works_path)
get :edit
expect(response).to be_successful
expect(assigns[:terms]).to eq [:creator, :contributor, :description, :tag, :rights, :publisher,
Expand Down
2 changes: 2 additions & 0 deletions spec/controllers/citations_controller_spec.rb
Expand Up @@ -7,9 +7,11 @@
let(:work) { create(:work, user: user) }
before do
sign_in user
allow(controller.request).to receive(:referer).and_return('foo')
end

it "is successful" do
expect(controller).to receive(:add_breadcrumb).with('My Dashboard', Sufia::Engine.routes.url_helpers.dashboard_index_path)
get :work, id: work
expect(response).to be_successful
expect(assigns(:presenter)).to be_kind_of Sufia::WorkShowPresenter
Expand Down
36 changes: 33 additions & 3 deletions spec/controllers/file_sets_controller_spec.rb
Expand Up @@ -457,7 +457,7 @@
end
end

describe "someone else's files" do
describe "#edit" do
let(:file_set) do
FileSet.create(read_groups: ['public']) do |f|
f.apply_depositor_metadata('archivist1@example.com')
Expand All @@ -473,15 +473,20 @@
Hydra::Works::UploadFileToFileSet.call(file_set, file)
end

describe "edit" do
context "someone else's files" do
it "sets flash error" do
get :edit, id: file_set
expect(response.code).to eq '401'
expect(response).to render_template('unauthorized')
end
end
end

describe "#show" do
describe "#show" do
let(:file_set) do
create(:file_set, title: ['test file'], user: user)
end
context "without a referer" do
it "shows me the file and set breadcrumbs" do
expect(controller).to receive(:add_breadcrumb).with(I18n.t('sufia.dashboard.title'), Sufia::Engine.routes.url_helpers.dashboard_index_path)
get :show, id: file_set
Expand All @@ -493,6 +498,31 @@
expect(assigns[:presenter].audit_status).to eq 'Audits have not yet been run on this file.'
end
end

context "with a referer" do
let(:work) do
create(:generic_work,
title: ['test title'],
user: user,
visibility: Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC)
end

before do
allow(controller.request).to receive(:referer).and_return('foo')
work.ordered_members << file_set
work.save!
file_set.save!
end

it "shows me the breadcrumbs" do
expect(controller).to receive(:add_breadcrumb).with('My Dashboard', Sufia::Engine.routes.url_helpers.dashboard_index_path)
expect(controller).to receive(:add_breadcrumb).with('My Works', Sufia::Engine.routes.url_helpers.dashboard_works_path)
expect(controller).to receive(:add_breadcrumb).with('test title', Sufia::Engine.routes.url_helpers.curation_concerns_generic_work_path(work.id))
expect(controller).to receive(:add_breadcrumb).with('test file', main_app.curation_concerns_file_set_path(file_set))
get :show, id: file_set
expect(response).to be_successful
end
end
end

describe "flash" do
Expand Down
22 changes: 13 additions & 9 deletions spec/controllers/my/works_controller_spec.rb
Expand Up @@ -5,12 +5,6 @@

before { sign_in user }

it "responds with success" do
get :index
expect(response).to be_successful
expect(response).to render_template :index
end

context "with multiple pages of works" do
before { 3.times { create(:work, user: user) } }
it "paginates" do
Expand Down Expand Up @@ -54,16 +48,26 @@

let(:doc_ids) { assigns[:document_list].map(&:id) }
let(:user_collections) { assigns[:user_collections].map(&:id) }
let(:sufia) do
Sufia::Engine.routes.url_helpers
end
before do
allow(controller.request).to receive(:referer).and_return("http://...blargh/")
end

it 'shows only the correct records' do
get :index
expect(response).to be_successful
expect(response).to render_template :index
expect(doc_ids).to contain_exactly(my_work.id)
expect(user_collections).to contain_exactly(my_collection.id)
end
end

it "sets add_files_to_collection when provided in params" do
get :index, add_files_to_collection: '12345'
expect(assigns(:add_files_to_collection)).to eql('12345')
context "when add_files_to_collection is provided" do
it "sets add_files_to_collection ivar" do
get :index, add_files_to_collection: '12345'
expect(assigns(:add_files_to_collection)).to eql('12345')
end
end
end
65 changes: 26 additions & 39 deletions spec/controllers/stats_controller_spec.rb
Expand Up @@ -5,39 +5,18 @@
before do
allow_any_instance_of(User).to receive(:groups).and_return([])
end
describe 'file' do
routes { Sufia::Engine.routes }

let(:file_set) do
FileSet.create do |fs|
fs.apply_depositor_metadata(user)
end
end
routes { Sufia::Engine.routes }
let(:usage) { double }

describe '#file' do
let(:file_set) { create(:file_set, user: user) }
context 'when user has access to file' do
before do
sign_in user
file_set_query = double('query')
allow(file_set_query).to receive(:for_path).and_return([
OpenStruct.new(date: '2014-01-01', pageviews: 4),
OpenStruct.new(date: '2014-01-02', pageviews: 8),
OpenStruct.new(date: '2014-01-03', pageviews: 6),
OpenStruct.new(date: '2014-01-04', pageviews: 10),
OpenStruct.new(date: '2014-01-05', pageviews: 2)])
allow(file_set_query).to receive(:map).and_return(file_set_query.for_path.map(&:marshal_dump))
profile = double('profile')
allow(profile).to receive(:sufia__pageview).and_return(file_set_query)
allow(Sufia::Analytics).to receive(:profile).and_return(profile)

download_query = double('query')
allow(download_query).to receive(:for_file).and_return([
OpenStruct.new(eventCategory: "Files", eventAction: "Downloaded", eventLabel: "123456789", totalEvents: "3")
])
allow(download_query).to receive(:map).and_return(download_query.for_file.map(&:marshal_dump))
allow(profile).to receive(:sufia__download).and_return(download_query)
end

it 'renders the stats view' do
expect(FileUsage).to receive(:new).with(file_set.id).and_return(usage)
allow(controller.request).to receive(:referer).and_return('foo')
expect(controller).to receive(:add_breadcrumb).with(I18n.t('sufia.dashboard.title'), Sufia::Engine.routes.url_helpers.dashboard_index_path)
expect(controller).to receive(:add_breadcrumb).with(I18n.t('sufia.dashboard.my.works'), Sufia::Engine.routes.url_helpers.dashboard_works_path)
Expand All @@ -46,24 +25,22 @@
expect(response).to be_success
expect(response).to render_template('stats/file')
end
end

context "user is not signed in but the file is public" do
before do
file_set.read_groups = ['public']
file_set.save
end
context "user is not signed in but the file is public" do
let(:file_set) { create(:file_set, :public, user: user) }

it 'renders the stats view' do
get :file, id: file_set
expect(response).to be_success
expect(response).to render_template('stats/file')
end
it 'renders the stats view' do
get :file, id: file_set
expect(response).to be_success
expect(response).to render_template('stats/file')
end
end

context 'when user lacks access to file' do
let(:file_set) { create(:file_set) }
before do
sign_in FactoryGirl.create(:user)
sign_in user
end

it 'redirects to root_url' do
Expand All @@ -74,10 +51,20 @@
end

describe 'work' do
routes { Sufia::Engine.routes }
let(:work) { create(:generic_work, user: user) }
before do
sign_in user
allow(controller.request).to receive(:referer).and_return('foo')
end

it 'renders the stats view' do
get :work, id: '123'
expect(WorkUsage).to receive(:new).with(work.id).and_return(usage)
expect(controller).to receive(:add_breadcrumb).with(I18n.t('sufia.dashboard.my.works'), Sufia::Engine.routes.url_helpers.dashboard_works_path)
expect(controller).to receive(:add_breadcrumb).with(I18n.t('sufia.dashboard.title'), Sufia::Engine.routes.url_helpers.dashboard_index_path)
expect(controller).to receive(:add_breadcrumb).with(I18n.t('sufia.work.browse_view'), curation_concerns_generic_work_path(work))
get :work, id: work
expect(response).to be_success
expect(response).to render_template('stats/work')
end
end
end
4 changes: 4 additions & 0 deletions spec/factories/file_sets.rb
Expand Up @@ -7,6 +7,10 @@
fs.apply_depositor_metadata evaluator.user.user_key
end

trait :public do
read_groups ["public"]
end

trait :registered do
read_groups ["registered"]
end
Expand Down

0 comments on commit b11f8f3

Please sign in to comment.