From addeadd41475829c394265877388429fc9bc95c1 Mon Sep 17 00:00:00 2001 From: tamsin johnson Date: Mon, 30 Oct 2023 11:47:52 -0700 Subject: [PATCH] parameterize Hyrax::FileSetFileService open Hyrax to downstream customization of Hyrax::FileSetFileService --- app/indexers/hyrax/valkyrie_file_set_indexer.rb | 4 ++-- app/presenters/hyrax/version_list_presenter.rb | 9 +++++++-- app/services/hyrax/file_set_file_service.rb | 10 +++++----- .../hyrax/templates/config/initializers/hyrax.rb | 4 ++++ lib/hyrax/configuration.rb | 8 ++++++++ spec/lib/hyrax/configuration_spec.rb | 2 ++ spec/services/hyrax/file_set_file_service_spec.rb | 2 +- 7 files changed, 29 insertions(+), 10 deletions(-) diff --git a/app/indexers/hyrax/valkyrie_file_set_indexer.rb b/app/indexers/hyrax/valkyrie_file_set_indexer.rb index 2e1093ac71..a848e1d518 100644 --- a/app/indexers/hyrax/valkyrie_file_set_indexer.rb +++ b/app/indexers/hyrax/valkyrie_file_set_indexer.rb @@ -25,8 +25,8 @@ def to_solr # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Met index_lease(solr_doc) index_embargo(solr_doc) - # Add in metadata from the original file. - file_metadata = Hyrax::FileSetFileService.new(file_set: resource).primary_file + # Add in metadata from the primary file. + file_metadata = Hyrax.config.file_set_file_service.new(file_set: resource).primary_file return solr_doc unless file_metadata # Label is the actual file name. It's not editable by the user. diff --git a/app/presenters/hyrax/version_list_presenter.rb b/app/presenters/hyrax/version_list_presenter.rb index 6ea24f4395..6c08514f45 100644 --- a/app/presenters/hyrax/version_list_presenter.rb +++ b/app/presenters/hyrax/version_list_presenter.rb @@ -20,8 +20,13 @@ def initialize(service) # relevant file versions. # # @raise [ArgumentError] if we can't build an enumerable - def self.for(file_set:) - original_file = Hyrax::FileSetFileService.new(file_set: file_set).primary_file + def self.for(file_set:, file_service: Hyrax.config.file_set_file_service) + original_file = case file_set + when ActiveFedora::Base + file_set.original_file + else + file_service.new(file_set: file_set).primary_file + end new(Hyrax::VersioningService.new(resource: original_file)) rescue NoMethodError diff --git a/app/services/hyrax/file_set_file_service.rb b/app/services/hyrax/file_set_file_service.rb index 6d98ff614c..376a7cf708 100644 --- a/app/services/hyrax/file_set_file_service.rb +++ b/app/services/hyrax/file_set_file_service.rb @@ -53,11 +53,11 @@ def primary_file # # See NOTE above regarding use of :find_file_metadata_by. @primary_file ||= begin - query_service.custom_queries.find_original_file(file_set: file_set) - rescue Valkyrie::Persistence::ObjectNotFoundError - fallback_id = file_set.file_ids.first - query_service.custom_queries.find_file_metadata_by(id: fallback_id) if fallback_id - end + query_service.custom_queries.find_original_file(file_set: file_set) + rescue Valkyrie::Persistence::ObjectNotFoundError + fallback_id = file_set.file_ids.first + query_service.custom_queries.find_file_metadata_by(id: fallback_id) if fallback_id + end end end alias original_file primary_file diff --git a/lib/generators/hyrax/templates/config/initializers/hyrax.rb b/lib/generators/hyrax/templates/config/initializers/hyrax.rb index d3240de8af..ec549e4473 100644 --- a/lib/generators/hyrax/templates/config/initializers/hyrax.rb +++ b/lib/generators/hyrax/templates/config/initializers/hyrax.rb @@ -248,6 +248,10 @@ # Identify the form that will be used for File Sets # config.file_set_form = Hyrax::Forms::FileSetForm + # Service for resolving {FileMetadata} nodes by status, e.g. "primary", rather + # than use. + # config.file_set_file_service = Hyrax::FileSetFileService + # Identify the form that will be used for Collections # config.pcdm_collection_form = Hyrax::Forms::PcdmCollectionForm diff --git a/lib/hyrax/configuration.rb b/lib/hyrax/configuration.rb index b81b9c46ff..bd6ca361d5 100644 --- a/lib/hyrax/configuration.rb +++ b/lib/hyrax/configuration.rb @@ -267,6 +267,14 @@ def derivative_services @derivative_services ||= [Hyrax::FileSetDerivativesService] end + ## + # @!attribute [rw] file_set_file_service + # @return [Class] implementer of {Hyrax::FileSetFileService} + attr_writer :file_set_file_service + def file_set_file_service + @file_set_file_service ||= Hyrax::FileSetFileService + end + attr_writer :fixity_service def fixity_service @fixity_service ||= Hyrax::Fixity::ActiveFedoraFixityService diff --git a/spec/lib/hyrax/configuration_spec.rb b/spec/lib/hyrax/configuration_spec.rb index 4249d8e8fb..074b9c4b87 100644 --- a/spec/lib/hyrax/configuration_spec.rb +++ b/spec/lib/hyrax/configuration_spec.rb @@ -55,6 +55,8 @@ it { is_expected.to respond_to(:feature_config_path) } it { is_expected.to respond_to(:file_set_form) } it { is_expected.to respond_to(:file_set_form=) } + it { is_expected.to respond_to(:file_set_file_service) } + it { is_expected.to respond_to(:file_set_file_service=) } it { is_expected.to respond_to(:identifier_registrars) } it { is_expected.to respond_to(:iiif_image_compliance_level_uri) } it { is_expected.to respond_to(:iiif_image_compliance_level_uri=) } diff --git a/spec/services/hyrax/file_set_file_service_spec.rb b/spec/services/hyrax/file_set_file_service_spec.rb index ba57d14bbf..7338b91569 100644 --- a/spec/services/hyrax/file_set_file_service_spec.rb +++ b/spec/services/hyrax/file_set_file_service_spec.rb @@ -15,7 +15,7 @@ it "finds the original_file" do expect(service.primary_file) .to have_attributes(file_set_id: file_set.id, - pcdm_use: contain_exactly("http://pcdm.org/use#OriginalFile")) + pcdm_use: include("http://pcdm.org/use#OriginalFile")) end end