Skip to content

Commit

Permalink
Merge pull request #4965 from samvera/helperize
Browse files Browse the repository at this point in the history
helperize download link display flag
  • Loading branch information
jeremyf authored May 19, 2021
2 parents 96d166d + c4ba324 commit 3de688a
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 9 deletions.
13 changes: 13 additions & 0 deletions app/helpers/hyrax/file_set_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# frozen_string_literal: true
module Hyrax::FileSetHelper
##
# @todo inline the "workflow restriction" into the `can?(:download)` check.
#
# @param file_set [#id]
#
# @return [Boolean] whether to display the download link for the given file
# set
def display_media_download_link?(file_set:)
Hyrax.config.display_media_download_link? &&
can?(:download, file_set) &&
!workflow_restriction?(file_set.try(:parent))
end

def parent_path(parent)
if parent.is_a?(::Collection)
main_app.collection_path(parent)
Expand Down
2 changes: 1 addition & 1 deletion app/views/hyrax/file_sets/media_display/_audio.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if Hyrax.config.display_media_download_link? && !workflow_restriction?(file_set.try(:parent)) %>
<% if display_media_download_link?(file_set: file_set) %>
<div>
<h2 class="sr-only"><%= t('hyrax.file_set.show.downloadable_content.heading') %></h2>
<audio controls="controls" class="audiojs" style="width:100%" controlsList="nodownload" preload="auto">
Expand Down
2 changes: 1 addition & 1 deletion app/views/hyrax/file_sets/media_display/_default.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="no-preview">
<%= t('hyrax.works.show.no_preview') %>
<% if Hyrax.config.display_media_download_link? && !workflow_restriction?(file_set.try(:parent)) %>
<% if display_media_download_link?(file_set: file_set) %>
<p /><%= link_to t('hyrax.file_set.show.download'),
hyrax.download_path(file_set),
id: "file_download",
Expand Down
2 changes: 1 addition & 1 deletion app/views/hyrax/file_sets/media_display/_image.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if Hyrax.config.display_media_download_link? && can?(:download, file_set.id) && !workflow_restriction?(file_set.try(:parent)) %>
<% if display_media_download_link?(file_set: file_set) %>
<div>
<h2 class="sr-only"><%= t('hyrax.file_set.show.downloadable_content.heading') %></h2>
<%= image_tag thumbnail_url(file_set),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if Hyrax.config.display_media_download_link? && !workflow_restriction?(file_set.try(:parent)) %>
<% if display_media_download_link?(file_set: file_set) %>
<div>
<h2 class="sr-only"><%= t('hyrax.file_set.show.downloadable_content.heading') %></h2>
<%= image_tag thumbnail_url(file_set),
Expand Down
2 changes: 1 addition & 1 deletion app/views/hyrax/file_sets/media_display/_pdf.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if Hyrax.config.display_media_download_link? && !workflow_restriction?(file_set.try(:parent)) %>
<% if display_media_download_link?(file_set: file_set) %>
<div>
<h2 class="sr-only"><%= t('hyrax.file_set.show.downloadable_content.heading') %></h2>
<%= image_tag thumbnail_url(file_set),
Expand Down
2 changes: 1 addition & 1 deletion app/views/hyrax/file_sets/media_display/_video.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if Hyrax.config.display_media_download_link? && !workflow_restriction?(file_set.try(:parent)) %>
<% if display_media_download_link?(file_set: file_set) %>
<div>
<h2 class="sr-only"><%= t('hyrax.file_set.show.downloadable_content.heading') %></h2>
<video controls="controls" class="video-js vjs-default-skin" style="width:100%" data-setup="{}" controlsList="nodownload" preload="auto">
Expand Down
21 changes: 21 additions & 0 deletions spec/helpers/hyrax/file_set_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# frozen_string_literal: true
RSpec.describe Hyrax::FileSetHelper do
describe '#display_media_download_link?' do
let(:ability) { double(Ability) }
let(:file_set) { FactoryBot.create(:file_set) }

before do
allow(controller).to receive(:current_ability).and_return(ability)
end

it 'does not allow download when permissions restrict it' do
allow(ability).to receive(:can?).with(:download, file_set).and_return(false)

expect(helper.display_media_download_link?(file_set: file_set)).to eq false
end

it 'allows download when permissions allow it ' do
allow(ability).to receive(:can?).with(:download, file_set).and_return(true)

expect(helper.display_media_download_link?(file_set: file_set)).to eq true
end
end

describe '#media_display' do
let(:file_set) { SolrDocument.new(mime_type_ssi: mime_type) }
let(:mime_type) { 'image/tiff' }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true
RSpec.describe 'hyrax/file_sets/media_display/_audio.html.erb', type: :view do
let(:ability) { double(Ability) }
let(:file_set) { stub_model(FileSet, parent: parent) }
let(:config) { double }
let(:parent) { double }
let(:link) { true }

before do
allow(controller).to receive(:current_ability).and_return(ability)
allow(ability).to receive(:can?).with(:download, file_set).and_return(true)
allow(view).to receive(:workflow_restriction?).with(parent).and_return(false)
allow(Hyrax.config).to receive(:display_media_download_link?).and_return(link)
render 'hyrax/file_sets/media_display/audio', file_set: file_set
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true
RSpec.describe 'hyrax/file_sets/media_display/_default.html.erb', type: :view do
let(:ability) { double(Ability) }
let(:file_set) { stub_model(FileSet, parent: parent) }
let(:parent) { double }
let(:config) { double }
let(:link) { true }

before do
allow(controller).to receive(:current_ability).and_return(ability)
allow(ability).to receive(:can?).with(:download, file_set).and_return(true)
allow(view).to receive(:workflow_restriction?).with(parent).and_return(false)
allow(Hyrax.config).to receive(:display_media_download_link?).and_return(link)
render 'hyrax/file_sets/media_display/default', file_set: file_set
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true
RSpec.describe 'hyrax/file_sets/media_display/_video.html.erb', type: :view do
let(:ability) { double(Ability) }
let(:file_set) { stub_model(FileSet, parent: parent) }
let(:parent) { double }
let(:config) { double }
let(:link) { true }

before do
allow(controller).to receive(:current_ability).and_return(ability)
allow(ability).to receive(:can?).with(:download, file_set).and_return(true)
allow(Hyrax.config).to receive(:display_media_download_link?).and_return(link)
allow(view).to receive(:workflow_restriction?).with(parent).and_return(false)
render 'hyrax/file_sets/media_display/video', file_set: file_set
Expand Down
2 changes: 2 additions & 0 deletions spec/views/hyrax/file_sets/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
let(:parent_presenter) { Hyrax::WorkShowPresenter.new(work_solr_document, ability) }

before do
allow(controller).to receive(:current_ability).and_return(ability)
allow(view).to receive(:workflow_restriction?).and_return(false)
view.lookup_context.prefixes.push 'hyrax/base'
allow(ability).to receive(:can?).with(:download, presenter).and_return(true)
allow(ability).to receive(:can?).with(:edit, presenter).and_return(false)
allow(presenter).to receive(:fixity_status).and_return(mock_metadata)
assign(:presenter, presenter)
Expand Down

0 comments on commit 3de688a

Please sign in to comment.