diff --git a/app/services/hyrax/versioning_service.rb b/app/services/hyrax/versioning_service.rb index b9177760d5..81bdcdec50 100644 --- a/app/services/hyrax/versioning_service.rb +++ b/app/services/hyrax/versioning_service.rb @@ -32,14 +32,10 @@ def initialize(resource:, storage_adapter: Hyrax.storage_adapter) # If the resource is nil, or if it is a Hyrax::FileMetadata and versioning # is not supported in the storage adapter, an empty array will be returned. def versions - if resource.nil? + if !supports_multiple_versions? [] elsif resource.is_a?(Hyrax::FileMetadata) - if storage_adapter.try(:"supports?", :versions) - storage_adapter.find_versions(id: resource.file_identifier).to_a - else - [] - end + storage_adapter.find_versions(id: resource.file_identifier).to_a else return resource.versions if resource.versions.is_a?(Array) resource.versions.all.to_a @@ -53,6 +49,16 @@ def latest_version versions.last end + ## + # Returns whether support for multiple versions exists on this + # +Hyrax::VersioningService+. + # + # Versioning is unsupported on nil resources or on Valkyrie resources when + # the configured storage adapter does not advertise versioning support. + def supports_multiple_versions? + !(resource.nil? || resource.is_a?(Hyrax::FileMetadata) && !storage_adapter.try(:"supports?", :versions)) + end + ## # Returns the file ID of the latest version of the file associated with this # Hyrax::VersioningService, or the ID of the file resource itself if no diff --git a/spec/services/hyrax/versioning_service_spec.rb b/spec/services/hyrax/versioning_service_spec.rb index 05a3c2e84c..c9b6a9c296 100644 --- a/spec/services/hyrax/versioning_service_spec.rb +++ b/spec/services/hyrax/versioning_service_spec.rb @@ -9,6 +9,14 @@ Hydra::Works::AddFileToFileSet.call(file, File.open(fixture_path + '/world.png'), :original_file, versioning: true) end + describe '#supports_multiple_versions?' do + subject do + described_class.new(resource: file.original_file).supports_multiple_versions? + end + + it { is_expected.to be true } + end + describe '#versions' do subject do described_class.new(resource: file.original_file).versions.map do |v| @@ -88,6 +96,24 @@ end let(:file_metadata) { query_service.custom_queries.find_file_metadata_by(id: uploaded.id) } + describe '#supports_multiple_versions?' do + subject do + described_class.new(resource: file_metadata).supports_multiple_versions? + end + + context 'when versions are unsupported' do + before do + allow(storage_adapter).to receive(:supports?).and_return(false) + end + + it { is_expected.to be false } + end + + context 'when versions are supported' do + it { is_expected.to be true } + end + end + describe '#versions' do subject { described_class.new(resource: file_metadata).versions.map(&:id) }