Skip to content

Commit

Permalink
Open restricted downloads in new tab to avoid blank page (#346)
Browse files Browse the repository at this point in the history
* Restricted downloads open in new tab
* Convert downlaod collapse to a view component with a spec
* Remove partial replaced by view component
* View Component initializer needs to call super
  • Loading branch information
spilth authored Aug 6, 2024
1 parent 6635dee commit f9d6dd3
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 47 deletions.
32 changes: 32 additions & 0 deletions app/components/catalog/downloads_collapse_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%# Renders the options of the downloads dropdown button %>
<% if @document.multi_direct_downloads.present? %>
<% @document.multi_direct_downloads.each do |download| %>
<%= download_link_file(download[0], @document.id, download[1]) %>
<% end %>
<% end %>
<% if @document.direct_download.present? %>
<% if @document.direct_download[:download].is_a? Array %>
<% @document.direct_download[:download].each do |download| %>
<%= download_link_file(download['label'], @document.id, download['url']) %>
<% end %>
<% end %>
<% if @document.direct_download[:download].is_a? String %>
<%= download_link_file(download_text(@document.file_format), @document.id, @document.direct_download[:download], @document.restricted? ? "_blank" : nil) %>
<% end %>
<% end %>
<% if @document.hgl_download.present? %>
<%= download_link_hgl(download_text(@document.download_types.first[0]), @document) %>
<% end %>
<% if @document.iiif_download.present? %>
<%= download_link_iiif %>
<% end %>
<% if @document.download_types.present? %>
<% @document.download_types.each do |type| %>
<% next if type.first == :kmz %>
<%= download_link_generated(type.first, @document) %>
<% end %>
<% end %>
10 changes: 10 additions & 0 deletions app/components/catalog/downloads_collapse_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Catalog
class DownloadsCollapseComponent < ViewComponent::Base
def initialize(document:)
super
@document = document
end
end
end
5 changes: 3 additions & 2 deletions app/helpers/geoblacklight_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def iiif_jpg_url
@document.references.iiif.endpoint.sub! 'info.json', 'full/full/0/default.jpg'
end

def download_link_file(label, id, url)
def download_link_file(label, id, url, target = nil)
link_to(
label,
url,
Expand All @@ -22,7 +22,8 @@ def download_link_file(label, id, url)
download: 'trigger',
download_type: 'direct',
download_id: id
}
},
target: target
)
end

Expand Down
33 changes: 0 additions & 33 deletions app/views/catalog/_downloads_collapse.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/catalog/_show_downloads.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</button>
<div class="collapse" id="downloads-collapse">
<div class="card card-body">
<%= render 'downloads_collapse' %>
<%= render Catalog::DownloadsCollapseComponent.new(document: @document) %>
</div>
</div>
</div>
Expand Down
39 changes: 39 additions & 0 deletions spec/components/catalog/downloads_collapse_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Catalog::DownloadsCollapseComponent, type: :component do
let(:document) { instance_double(SolrDocument, id: 123) }

before do
allow(document).to receive_messages(
multi_direct_downloads: [],
file_format: 'Shapefile',
direct_download: { download: 'https://example.com/download' },
restricted?: restricted,
hgl_download: false,
iiif_download: false,
download_types: []
)
end

context 'when the download is restricted' do
let(:restricted) { true }

it 'renders restricted links with a target of _blank' do
render_inline(described_class.new(document:))

expect(page).to have_link('Original Shapefile', href: 'https://example.com/download', target: '_blank')
end
end

context 'when the download is not restricted' do
let(:restricted) { false }

it 'renders the download link with no target' do
render_inline(described_class.new(document:))

expect(page).to have_link('Original Shapefile', href: 'https://example.com/download', target: nil)
end
end
end
44 changes: 33 additions & 11 deletions spec/features/show_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,41 @@

describe 'Show page' do
context 'with restricted NYU result - nyu-2451-34626' do
it 'the displays warning message' do
visit solr_document_path 'nyu-2451-34626'
expect(page).to have_content 'This dataset is only available to members of the New York University community'
end
context 'when signed out' do
it 'the displays warning message' do
visit solr_document_path 'nyu-2451-34626'
expect(page).to have_content 'This dataset is only available to members of the New York University community'
end

it 'does not display download' do
visit solr_document_path 'nyu-2451-34626'
expect(page).not_to have_content 'Export'
it 'does not display download' do
visit solr_document_path 'nyu-2451-34626'
expect(page).not_to have_content 'Export'
end

it 'includes link to login' do
visit solr_document_path 'nyu-2451-34626'
expect(page).to have_link('Login to View and Download')
end
end

it 'includes link to login' do
visit solr_document_path 'nyu-2451-34626'
expect(page).to have_link('Login to View and Download')
context 'when signed in' do
before do
login_as(create(:user), scope: :user)
end

it 'shows a download button' do
visit solr_document_path 'nyu-2451-34626'
expect(page).to have_button('Download')
end

it 'opens download links in a new tab' do
visit solr_document_path 'nyu-2451-34626'
click_button('Download')

within_window(window_opened_by { click_link('Original Shapefile') }) do
expect(page.title).not_to include('NYU Spatial Data Repository')
end
end
end
end

Expand Down Expand Up @@ -44,7 +66,7 @@

# TODO: Refactor this to play nice with Rubocop
# rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations
context 'with multiple downloads - nyu-2451-38645' do #
context 'with multiple downloads - nyu-2451-38645' do
it 'includes six download links' do
visit solr_document_path 'nyu-2451-38645'
expect(page).to have_content 'Download'
Expand Down

0 comments on commit f9d6dd3

Please sign in to comment.