diff --git a/app/overrides/actors/hyrax/actors/file_set_actor_override.rb b/app/overrides/actors/hyrax/actors/file_set_actor_override.rb index 666015593..a8b594656 100644 --- a/app/overrides/actors/hyrax/actors/file_set_actor_override.rb +++ b/app/overrides/actors/hyrax/actors/file_set_actor_override.rb @@ -9,4 +9,22 @@ def update_content(file, relation = :original_file) IngestJob.perform_now(wrapper!(file: file, relation: relation), notification: true) end + + # [hyc-override] Fall back to the next fileset for thumbnail/represenatative if there are any remaining in the work + def unlink_from_work + work = parent_for(file_set: file_set) + return unless work && (work.thumbnail_id == file_set.id || work.representative_id == file_set.id || work.rendering_ids.include?(file_set.id)) + + remaining_members = work.members.to_a.reject { |member| member.id == file_set.id } + if remaining_members.empty? + work.thumbnail = nil + work.representative = nil + else + work.thumbnail = remaining_members.first + work.representative = remaining_members.first + end + + work.rendering_ids -= [file_set.id] + work.save! + end end diff --git a/app/overrides/presenters/blacklight/thumbnail_presenter_override.rb b/app/overrides/presenters/blacklight/thumbnail_presenter_override.rb deleted file mode 100644 index 52993d47b..000000000 --- a/app/overrides/presenters/blacklight/thumbnail_presenter_override.rb +++ /dev/null @@ -1,48 +0,0 @@ -# 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 diff --git a/app/overrides/presenters/hyrax/work_show_presenter_override.rb b/app/overrides/presenters/hyrax/work_show_presenter_override.rb index 3a5b84d54..9d6964414 100644 --- a/app/overrides/presenters/hyrax/work_show_presenter_override.rb +++ b/app/overrides/presenters/hyrax/work_show_presenter_override.rb @@ -10,25 +10,4 @@ def scholarly? false end - - def fetch_primary_fileset_id - res = representative_id.blank? ? member_ids.first : representative_id - res - end - - # [hyc-override] Use a work's first related fileset_id instead of the representative_id if it's nil - # @return FileSetPresenter presenter for the representative FileSets - def representative_presenter - @representative_presenter ||= - begin - primary_fileset_id = fetch_primary_fileset_id - return nil if primary_fileset_id.blank? - result = member_presenters([primary_fileset_id]).first - return nil if result.try(:id) == id - result.try(:representative_presenter) || result - rescue Hyrax::ObjectNotFoundError - Hyrax.logger.warn "Unable to find representative_id #{primary_fileset_id} for work #{id}" - return nil - end - end end diff --git a/app/views/hyrax/base/_representative_media.html.erb b/app/views/hyrax/base/_representative_media.html.erb deleted file mode 100644 index 52d74dbd6..000000000 --- a/app/views/hyrax/base/_representative_media.html.erb +++ /dev/null @@ -1,12 +0,0 @@ -<%# [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 %> - -<% 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 %> diff --git a/spec/actors/hyrax/actors/file_set_actor_spec.rb b/spec/actors/hyrax/actors/file_set_actor_spec.rb index 1c3ccf4d1..cbaf6233e 100644 --- a/spec/actors/hyrax/actors/file_set_actor_spec.rb +++ b/spec/actors/hyrax/actors/file_set_actor_spec.rb @@ -17,4 +17,52 @@ expect(actor.update_content(file)).to be_a(IngestJob) end end + + describe '#unlink_from_work' do + let(:work) { FactoryBot.create(:work) } + + context 'work with one file' do + before do + actor.attach_to_work(work) + work.rendering_ids = [file_set.id] + work.save! + end + + it 'removes file_set from work' do + expect(work.thumbnail_id).to eq(file_set.id) + expect(work.representative_id).to eq(file_set.id) + expect(work.rendering_ids).to eq([file_set.id]) + + actor.unlink_from_work + work.reload + expect(work.thumbnail_id).to be_nil + expect(work.representative_id).to be_nil + expect(work.rendering_ids).to be_empty + end + end + + context 'work with two files' do + let(:file_set2) { FactoryBot.create(:file_set) } + let(:actor2) { described_class.new(file_set2, user) } + + before do + actor.attach_to_work(work) + actor2.attach_to_work(work) + work.rendering_ids = [file_set.id, file_set2.id] + work.save! + end + + it 'removes file_set from work' do + expect(work.thumbnail_id).to eq(file_set.id) + expect(work.representative_id).to eq(file_set.id) + expect(work.rendering_ids).to eq([file_set.id, file_set2.id]) + + actor.unlink_from_work + work.reload + expect(work.thumbnail_id).to eq(file_set2.id) + expect(work.representative_id).to eq(file_set2.id) + expect(work.rendering_ids).to eq([file_set2.id]) + end + end + end end diff --git a/spec/presenters/blacklight/thumbnail_presenter_spec.rb b/spec/presenters/blacklight/thumbnail_presenter_spec.rb deleted file mode 100644 index 8346aaa5a..000000000 --- a/spec/presenters/blacklight/thumbnail_presenter_spec.rb +++ /dev/null @@ -1,51 +0,0 @@ -# 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 diff --git a/spec/presenters/hyrax/work_show_presenter_spec.rb b/spec/presenters/hyrax/work_show_presenter_spec.rb index bc2a6fbb4..4b1e2e91d 100644 --- a/spec/presenters/hyrax/work_show_presenter_spec.rb +++ b/spec/presenters/hyrax/work_show_presenter_spec.rb @@ -37,20 +37,4 @@ it { is_expected.to delegate_method(:resource_type).to(:solr_document) } it { is_expected.to delegate_method(:keyword).to(:solr_document) } it { is_expected.to delegate_method(:itemtype).to(:solr_document) } - - describe '#representative_presenter' do - context 'when member_presenters raises a Hyrax::ObjectNotFoundError' do - before do - allow(presenter).to receive(:fetch_primary_fileset_id).and_return('file_set_id_1') - allow(presenter).to receive(:member_presenters).and_raise(Hyrax::ObjectNotFoundError) - allow(Hyrax.logger).to receive(:warn) - end - - it 'logs a warning and returns nil' do - result = presenter.representative_presenter - expect(Hyrax.logger).to have_received(:warn).with('Unable to find representative_id file_set_id_1 for work 888888') - expect(result).to be_nil - end - end - end end