-
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.
HYC-1354 - Fallback Thumbnails (#1133)
* avoid loading overrides multiple times using load * fallback to member ids if representative isnt there * thumbnail presenter override * accounting for nested solr documents, commenting out some stuff * thumbnail presenter tests * test for rescue block in representative_presenter * shared delegates module
- Loading branch information
1 parent
41282a8
commit a4fb320
Showing
9 changed files
with
167 additions
and
12 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
app/overrides/presenters/blacklight/thumbnail_presenter_override.rb
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,48 @@ | ||
# frozen_string_literal: true | ||
# https://github.com/projectblacklight/blacklight/blob/v8.7.0/app/presenters/blacklight/thumbnail_presenter.rb | ||
module Blacklight | ||
class ThumbnailPresenter | ||
private | ||
# [hyc-override] Retrieve the thumbnail of the first file_set of any given work instead of using the default if it exists | ||
def retrieve_values(field_config) | ||
# Return the default thumbnail if the document object is nil | ||
unless document | ||
return FieldRetriever.new(document, field_config, view_context).fetch | ||
end | ||
|
||
solr_doc = extract_solr_document(document) | ||
document_hash = solr_doc.to_h | ||
|
||
# Update the `thumbnail_path_ss` dynamically if needed | ||
if needs_thumbnail_path_update?(document_hash) | ||
file_set_id = document_hash['file_set_ids_ssim']&.first | ||
document_hash['thumbnail_path_ss'] = "/downloads/#{file_set_id}?file=thumbnail" | ||
Rails.logger.info("Updated thumbnail_path_ss: #{document_hash['thumbnail_path_ss']} for work with id #{document_hash['id']}") | ||
# Create a temporary SolrDocument from the updated hash | ||
updated_document = SolrDocument.new(document_hash) | ||
FieldRetriever.new(updated_document, field_config, view_context).fetch | ||
else | ||
FieldRetriever.new(solr_doc, field_config, view_context).fetch | ||
end | ||
end | ||
|
||
# Extract the SolrDocument from the document object if it's nested | ||
# Prevents errors when the document object is a presenter on work show pages | ||
def extract_solr_document(doc) | ||
if doc.is_a?(SolrDocument) | ||
doc | ||
elsif doc.respond_to?(:solr_document) && doc.solr_document.is_a?(SolrDocument) | ||
doc.solr_document | ||
end | ||
end | ||
|
||
def needs_thumbnail_path_update?(document) | ||
thumbnail_path = document['thumbnail_path_ss'] || '' | ||
file_set_ids = document['file_set_ids_ssim'] | ||
thumbnail_missing_or_default = thumbnail_path.blank? || thumbnail_path !~ %r{^/downloads/\w+\?file=thumbnail$} | ||
|
||
# Returns true if file_set_ids are present and the thumbnail path is the default or missing entirely | ||
file_set_ids.present? && thumbnail_missing_or_default | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
module Hyrax | ||
module SharedDelegates | ||
extend ActiveSupport::Concern | ||
included do | ||
delegate :abstract, :admin_note, :alternative_title, :bibliographic_citation, :contributor, :contributor_display, :copyright_date, | ||
:creator, :creator_display, :date_created, :date_issued, :dcmi_type, :deposit_record, :description, :doi, :embargo_release_date, | ||
:extent, :funder, :human_readable_type, :identifier, :itemtype, :kind_of_data, :language, :language_label, :last_modified_date, | ||
:lease_expiration_date, :license, :license_label, :member_ids, :member_of_collection_ids, :methodology, :note, :orcid_label, | ||
:other_affiliation_label, :place_of_publication, :project_director_display, :publisher, :related_url, :rendering_ids, :representative_id, :researcher_display, | ||
:resource_type, :rights_holder, :rights_notes, :rights_statement, :rights_statement_label, :sponsor, :source, :subject, | ||
:thumbnail_id, :title, to: :solr_document | ||
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,12 @@ | ||
<%# [hyc-override] https://github.com/samvera/hyrax/tree/hyrax-v4.0.0/app/views/hyrax/base/_representative_media.html.erb %> | ||
<%# [hyc-override] Use custom function fetch_primary_fileset_id in place of representative_id %> | ||
<!-- This renders the thumbnail for first file set associated with a work instead of the default in case the representative_id is nil. (Typically occurs after deleting the first file attached to a work) --> | ||
<% if presenter.fetch_primary_fileset_id.present? && presenter.representative_presenter.present? %> | ||
<% if defined?(viewer) && viewer %> | ||
<%= iiif_viewer_display presenter %> | ||
<% else %> | ||
<%= render media_display_partial(presenter.representative_presenter), file_set: presenter.representative_presenter %> | ||
<% end %> | ||
<% else %> | ||
<%= image_tag 'default.png', class: "canonical-image", alt: 'default representative image' %> | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
require 'rails_helper' | ||
require Rails.root.join('app/overrides/presenters/blacklight/thumbnail_presenter_override.rb') | ||
|
||
RSpec.describe Blacklight::ThumbnailPresenter do | ||
describe '#retrieve_values' do | ||
it 'does not attempt to process a solr document if it is nil' do | ||
# Mock field config and view context | ||
field_config = double('FieldConfig') | ||
view_context = double('ViewContext') | ||
presenter = Blacklight::ThumbnailPresenter.new(nil, view_context, field_config) | ||
|
||
retriever_instance = Blacklight::FieldRetriever.new(nil, nil, nil) | ||
allow(Blacklight::FieldRetriever).to receive(:new).and_return(retriever_instance) | ||
allow(retriever_instance).to receive(:fetch).and_return('default_thumbnail') | ||
allow(presenter).to receive(:extract_solr_document) | ||
|
||
result = presenter.send(:retrieve_values, field_config) | ||
expect(Blacklight::FieldRetriever).to have_received(:new).with(nil, field_config, view_context) | ||
# Presenter should not process the document if it's nil | ||
expect(presenter).not_to have_received(:extract_solr_document) | ||
expect(result).to eq('default_thumbnail') | ||
end | ||
|
||
it 'updates the thumbnail_path_ss if it needs an update' do | ||
field_config = double('FieldConfig') | ||
view_context = double('ViewContext') | ||
retriever_instance = Blacklight::FieldRetriever.new(nil, nil, nil) | ||
document_hash = { | ||
'thumbnail_path_ss' => '/assets/work-default.png', | ||
'file_set_ids_ssim' => ['file_set_1'], | ||
'id' => '1' | ||
} | ||
solr_doc = SolrDocument.new(document_hash) | ||
presenter = Blacklight::ThumbnailPresenter.new(solr_doc, view_context, field_config) | ||
|
||
allow(presenter).to receive(:needs_thumbnail_path_update?).and_return(true) | ||
allow(SolrDocument).to receive(:new).and_return(instance_double(SolrDocument)) | ||
allow(Blacklight::FieldRetriever).to receive(:new).and_return(retriever_instance) | ||
allow(retriever_instance).to receive(:fetch).and_return('updated_thumbnail') | ||
allow(Rails.logger).to receive(:info) | ||
|
||
result = presenter.send(:retrieve_values, field_config) | ||
expect(Rails.logger).to have_received(:info).with('Updated thumbnail_path_ss: /downloads/file_set_1?file=thumbnail for work with id 1') | ||
expect(SolrDocument).to have_received(:new).with( | ||
hash_including('thumbnail_path_ss' => '/downloads/file_set_1?file=thumbnail') | ||
) | ||
expect(result).to eq('updated_thumbnail') | ||
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