Skip to content

Commit dd9baaf

Browse files
marrus-shtamsin johnson
authored andcommitted
Add method to VersioningService to detect support
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.)
1 parent f576b42 commit dd9baaf

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

app/services/hyrax/versioning_service.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,10 @@ def initialize(resource:, storage_adapter: Hyrax.storage_adapter)
3232
# If the resource is nil, or if it is a Hyrax::FileMetadata and versioning
3333
# is not supported in the storage adapter, an empty array will be returned.
3434
def versions
35-
if resource.nil?
35+
if !supports_multiple_versions?
3636
[]
3737
elsif resource.is_a?(Hyrax::FileMetadata)
38-
if storage_adapter.try(:"supports?", :versions)
39-
storage_adapter.find_versions(id: resource.file_identifier).to_a
40-
else
41-
[]
42-
end
38+
storage_adapter.find_versions(id: resource.file_identifier).to_a
4339
else
4440
return resource.versions if resource.versions.is_a?(Array)
4541
resource.versions.all.to_a
@@ -53,6 +49,16 @@ def latest_version
5349
versions.last
5450
end
5551

52+
##
53+
# Returns whether support for multiple versions exists on this
54+
# +Hyrax::VersioningService+.
55+
#
56+
# Versioning is unsupported on nil resources or on Valkyrie resources when
57+
# the configured storage adapter does not advertise versioning support.
58+
def supports_multiple_versions?
59+
!(resource.nil? || resource.is_a?(Hyrax::FileMetadata) && !storage_adapter.try(:"supports?", :versions))
60+
end
61+
5662
##
5763
# Returns the file ID of the latest version of the file associated with this
5864
# Hyrax::VersioningService, or the ID of the file resource itself if no

spec/services/hyrax/versioning_service_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
Hydra::Works::AddFileToFileSet.call(file, File.open(fixture_path + '/world.png'), :original_file, versioning: true)
1010
end
1111

12+
describe '#supports_multiple_versions?' do
13+
subject do
14+
described_class.new(resource: file.original_file).supports_multiple_versions?
15+
end
16+
17+
it { is_expected.to be true }
18+
end
19+
1220
describe '#versions' do
1321
subject do
1422
described_class.new(resource: file.original_file).versions.map do |v|
@@ -88,6 +96,24 @@
8896
end
8997
let(:file_metadata) { query_service.custom_queries.find_file_metadata_by(id: uploaded.id) }
9098

99+
describe '#supports_multiple_versions?' do
100+
subject do
101+
described_class.new(resource: file_metadata).supports_multiple_versions?
102+
end
103+
104+
context 'when versions are unsupported' do
105+
before do
106+
allow(storage_adapter).to receive(:supports?).and_return(false)
107+
end
108+
109+
it { is_expected.to be false }
110+
end
111+
112+
context 'when versions are supported' do
113+
it { is_expected.to be true }
114+
end
115+
end
116+
91117
describe '#versions' do
92118
subject { described_class.new(resource: file_metadata).versions.map(&:id) }
93119

0 commit comments

Comments
 (0)