Skip to content

Commit

Permalink
Add method to VersioningService to detect support
Browse files Browse the repository at this point in the history
Right now `Hyrax::VersioningService` does some silent coersion of
versioning information in the case that versioning is not supported by
the storage adapter. But, it might be good to actively provide a
different UI to users in some cases depending on whether multiple
versions of a file can actually be stored.

This commit adds a `supports_multiple_versions?` method to
`Hyrax::VersioningService` which returns `true` iff the `resource` is
non‐nil and not a `FileMetadata` object, or if it is a `FileMetadata`
object and the storage adapter supports versioning.

This check requires having both a resource and a storage adapter in‐hand.
My guess is that this is reasonable, but if we need to detect versioning
support more generally (without a specific resource), a different
solution may be desirable (altho
`Hyrax.storage_adapter.try?(:"supports?", :versions)` works in this
case.)
  • Loading branch information
marrus-sh authored and tamsin johnson committed Aug 29, 2023
1 parent f576b42 commit dd9baaf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/services/hyrax/versioning_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions spec/services/hyrax/versioning_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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) }

Expand Down

0 comments on commit dd9baaf

Please sign in to comment.