-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #480 from nla/feat/blac-676_related-docs
feat: related docs
- Loading branch information
Showing
20 changed files
with
419 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<ol id="related-documents" class="list-unstyled"> | ||
<% related_docs.each do |doc| %> | ||
<% cache doc do %> | ||
<li> | ||
<i class="fa-solid fa-download"></i> <a href="<%= doc.download_url %>"><%= doc.link_text %></a> | ||
<% if doc.externalComments.present? %> | ||
<p><%= doc.externalComments %></p> | ||
<% end %> | ||
</li> | ||
<% end %> | ||
<% end %> | ||
</ol> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class RelatedDocumentsComponent < Blacklight::Component | ||
attr_reader :related_docs | ||
|
||
def initialize(related_docs:) | ||
@related_docs = related_docs | ||
end | ||
|
||
def render? | ||
related_docs.present? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module FieldHelper | ||
def render_related_docs(document:, field:, config:, value:, context:) | ||
if document.pid.present? | ||
render RelatedDocumentsComponent.new(related_docs: value) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module FileSizeHelper | ||
def format_filesize(size) | ||
if size.present? | ||
if size.to_i < 1024 | ||
"#{size} B" | ||
elsif size.to_i < 1048576 # 1024 * 1024 | ||
"#{(size.to_f / 1024).round(2)} KB" | ||
elsif size.to_i < 1073741824 # 1024 * 1024 * 1024 | ||
"#{(size.to_f / 1048576).round(2)} MB" | ||
else | ||
"#{(size.to_f / 1073741824).round(2)} GB" | ||
end | ||
else | ||
"0 B" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class RelatedDocument < OpenStruct | ||
def link_text | ||
if copyRole == "fas" | ||
"#{I18n.t("related_docs.link_text_fas")} (Type: #{fileType}, Size: #{ApplicationController.helpers.format_filesize(fileSize)})" | ||
else | ||
"#{I18n.t("related_docs.link_text")} ( Type: #{fileType}, Size: #{ApplicationController.helpers.format_filesize(fileSize)})" | ||
end | ||
end | ||
|
||
def download_url | ||
if copyRole == "fas" | ||
"https://nla.gov.au/#{pid}/relateddocs/download?copyRole=#{copyRole}©Id=#{id}" | ||
else | ||
"https://nla.gov.au/#{pid}/relateddocs/download?copyRole=#{copyRole}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe RelatedDocumentsComponent, type: :component do | ||
let(:pid) { "nla.obj-123" } | ||
|
||
let(:related_docs) do | ||
docs = JSON.parse(IO.read("spec/files/catalogue_services/related_docs.json"))["docs"] | ||
docs.map! do |attrs| | ||
attrs[:pid] = pid | ||
RelatedDocument.new(attrs) | ||
end | ||
end | ||
|
||
context "when there are no related documents" do | ||
it "does not render when there are no related documents" do | ||
render_inline(described_class.new(related_docs: nil)) | ||
|
||
expect(page).to have_no_css("ol#related-documents") | ||
end | ||
end | ||
|
||
context "when there are related document" do | ||
it "renders the related documents" do | ||
render_inline(described_class.new(related_docs: related_docs(pid))) | ||
|
||
expect(page).to have_css("ol#related-documents") | ||
expect(page).to have_css("li", count: 4) | ||
end | ||
end | ||
|
||
context "when there are external comments" do | ||
it "renders the external comments" do | ||
render_inline(described_class.new(related_docs: related_docs(pid))) | ||
|
||
expect(page).to have_content("Appendix C - Items in Series 12.1") | ||
end | ||
end | ||
|
||
private | ||
|
||
def related_docs(pid) | ||
docs = JSON.parse(IO.read("spec/files/catalogue_services/related_docs.json"))["docs"] | ||
docs.map! do |attrs| | ||
attrs[:pid] = pid # insert the pid since we need it to construct the download URL | ||
RelatedDocument.new(attrs) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe "Related Documents" do | ||
describe "when at the collection level" do | ||
before do | ||
solr_response = IO.read("spec/files/solr/280011976.json") | ||
|
||
WebMock.stub_request(:get, /solr:8983/) | ||
.with( | ||
headers: { | ||
"Accept" => "*/*", | ||
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" | ||
} | ||
) | ||
.to_return(status: 200, body: solr_response, headers: {}) | ||
|
||
availability_response = IO.read("spec/files/catalogue_services/08aed703-3648-54d0-80ef-fddb3c635731.json") | ||
|
||
WebMock.stub_request(:get, /catalogue-services\/folio\/instance\/(.*)/) | ||
.to_return(status: 200, body: availability_response, headers: {"Content-Type" => "application/json"}) | ||
end | ||
|
||
context "when there are related documents" do | ||
before do | ||
docs_response = IO.read("spec/files/catalogue_services/related_docs.json") | ||
WebMock.stub_request(:get, /catservices.test\/catalogue-services\/ead\/relateddocs\/(nla.obj-[0-9]*)/) | ||
.to_return(status: 200, body: docs_response, headers: {"Content-Type" => "application/json"}) | ||
end | ||
|
||
it "renders the related documents" do | ||
visit solr_document_path(id: "280011976") | ||
|
||
expect(page).to have_css("h3", text: "Related") | ||
expect(page).to have_css("ol#related-documents") | ||
expect(page).to have_css("ol#related-documents li", count: 4) | ||
end | ||
end | ||
|
||
context "when there are no related documents" do | ||
before do | ||
WebMock.stub_request(:get, /catservices.test\/catalogue-services\/ead\/relateddocs\/(nla.obj-[0-9]*)/) | ||
.to_return(status: 200, body: "", headers: {"Content-Type" => "application/json"}) | ||
end | ||
|
||
it "does not render the related documents" do | ||
visit solr_document_path(id: "280011976") | ||
|
||
expect(page).to have_no_css("h3", text: "Related") | ||
expect(page).to have_no_css("ol#related-documents") | ||
expect(page).to have_no_css("ol#related-documents li") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"docs": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"docs": [ | ||
{ | ||
"id": "nla.obj-367760674", | ||
"copyRole": "fas", | ||
"fileName": "Finding aid supplementary", | ||
"externalComments": "Appendix C - Items in Series 12.1", | ||
"fileType": "pdf", | ||
"fileSize": 124305 | ||
}, | ||
{ | ||
"id": "nla.obj-367758458", | ||
"copyRole": "fas", | ||
"fileName": "Finding aid supplementary", | ||
"externalComments": "Appendix B - Selected items in Subseries 5.2", | ||
"fileType": "pdf", | ||
"fileSize": 113986 | ||
}, | ||
{ | ||
"id": "nla.obj-367758206", | ||
"copyRole": "fas", | ||
"fileName": "Finding aid supplementary", | ||
"externalComments": "Appendix A - Concordance (Box List)", | ||
"fileType": "pdf", | ||
"fileSize": 401572 | ||
}, | ||
{ | ||
"id": "nla.obj-367766877", | ||
"copyRole": "fas", | ||
"fileName": "Finding aid supplementary", | ||
"externalComments": "Appendix D - Items in Series 17", | ||
"fileType": "pdf", | ||
"fileSize": 200793 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe FileSizeHelper do | ||
describe "#format_filesize" do | ||
it "returns 0 when there is no file size" do | ||
expect(helper.format_filesize(nil)).to eq("0 B") | ||
end | ||
|
||
it "returns the size in kilobytes" do | ||
expect(helper.format_filesize(1024)).to eq("1.0 KB") | ||
end | ||
|
||
it "returns the size in megabytes" do | ||
expect(helper.format_filesize(1024 * 1024)).to eq("1.0 MB") | ||
end | ||
|
||
it "returns the size in gigabytes" do | ||
expect(helper.format_filesize(1024 * 1024 * 1024)).to eq("1.0 GB") | ||
end | ||
|
||
it "returns sizes 1 TB or larger in gigabytes" do | ||
expect(helper.format_filesize(1024 * 1024 * 1024 * 1024)).to eq("1024.0 GB") | ||
end | ||
|
||
it "rounds to the nearest 2 decimal places" do | ||
expect(helper.format_filesize(124305)).to eq("121.39 KB") | ||
end | ||
end | ||
end |
Oops, something went wrong.