Skip to content

Commit

Permalink
Convert downlaod collapse to a view component with a spec
Browse files Browse the repository at this point in the history
  • Loading branch information
spilth committed Aug 1, 2024
1 parent be62a45 commit 3d049c8
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 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 %>
9 changes: 9 additions & 0 deletions app/components/catalog/downloads_collapse_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Catalog
class DownloadsCollapseComponent < ViewComponent::Base
def initialize(document:)
@document = document
end
end
end
1 change: 0 additions & 1 deletion app/views/catalog/_downloads_collapse.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<%# Renders the options of the downloads dropdown button %>
<% document ||= @document %>

<% if document.multi_direct_downloads.present? %>
<% document.multi_direct_downloads.each do |download| %>
Expand Down
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

0 comments on commit 3d049c8

Please sign in to comment.