Class: Hyrax::VersioningService
- Inherits:
-
Object
- Object
- Hyrax::VersioningService
- Defined in:
- app/services/hyrax/versioning_service.rb
Overview
Provides methods for dealing with versions of files across both ActiveFedora and Valkyrie.
Note that many of the methods pertaining to version creation are currently implemented as static methods.
Instance Attribute Summary collapse
- #resource ⇒ ActiveFedora::File | Hyrax::FileMetadata | NilClass
-
#storage_adapter ⇒ Object
readonly
Returns the value of attribute storage_adapter.
Class Method Summary collapse
-
.create(content, user = nil, file = nil) ⇒ Object
Make a version and record the version committer.
- .latest_version_of(file) ⇒ Object
-
.record_committer(content, user_key) ⇒ Object
Record the version committer of the last version.
- .versioned_file_id(file) ⇒ Object
Instance Method Summary collapse
-
#initialize(resource:, storage_adapter: nil) ⇒ VersioningService
constructor
A new instance of VersioningService.
-
#latest_version ⇒ Object
Returns the latest version of the file associated with this Hyrax::VersioningService.
-
#supports_multiple_versions? ⇒ Boolean
Returns whether support for multiple versions exists on this
Hyrax::VersioningService
. -
#versioned_file_id ⇒ Object
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 latest version is defined.
-
#versions ⇒ Object
Returns an array of versions for the resource associated with this Hyrax::VersioningService.
Constructor Details
#initialize(resource:, storage_adapter: nil) ⇒ VersioningService
Returns a new instance of VersioningService.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/services/hyrax/versioning_service.rb', line 23 def initialize(resource:, storage_adapter: nil) @storage_adapter = if storage_adapter.nil? if resource.respond_to?(:file_identifier) Valkyrie::StorageAdapter.adapter_for(id: resource.file_identifier) else Hyrax.storage_adapter end else storage_adapter end self.resource = resource end |
Instance Attribute Details
#resource ⇒ ActiveFedora::File | Hyrax::FileMetadata | NilClass
14 15 16 |
# File 'app/services/hyrax/versioning_service.rb', line 14 def resource @resource end |
#storage_adapter ⇒ Object (readonly)
Returns the value of attribute storage_adapter.
19 20 21 |
# File 'app/services/hyrax/versioning_service.rb', line 19 def storage_adapter @storage_adapter end |
Class Method Details
.create(content, user = nil, file = nil) ⇒ Object
Make a version and record the version committer
99 100 101 102 |
# File 'app/services/hyrax/versioning_service.rb', line 99 def create(content, user = nil, file = nil) use_valkyrie = content.is_a? Hyrax::FileMetadata perform_create(content, user, file, use_valkyrie) end |
.latest_version_of(file) ⇒ Object
105 106 107 |
# File 'app/services/hyrax/versioning_service.rb', line 105 def latest_version_of(file) Hyrax::VersioningService.new(resource: file).latest_version end |
.record_committer(content, user_key) ⇒ Object
Record the version committer of the last version
117 118 119 120 121 122 123 |
# File 'app/services/hyrax/versioning_service.rb', line 117 def record_committer(content, user_key) user_key = user_key.user_key if user_key.respond_to?(:user_key) version = latest_version_of(content) return if version.nil? version_id = content.is_a?(Hyrax::FileMetadata) ? version.version_id.to_s : version.uri Hyrax::VersionCommitter.create(version_id: version_id, committer_login: user_key) end |
.versioned_file_id(file) ⇒ Object
110 111 112 |
# File 'app/services/hyrax/versioning_service.rb', line 110 def versioned_file_id(file) Hyrax::VersioningService.new(resource: file).versioned_file_id end |
Instance Method Details
#latest_version ⇒ Object
Returns the latest version of the file associated with this Hyrax::VersioningService.
58 59 60 |
# File 'app/services/hyrax/versioning_service.rb', line 58 def latest_version versions.last end |
#supports_multiple_versions? ⇒ Boolean
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.
68 69 70 |
# File 'app/services/hyrax/versioning_service.rb', line 68 def supports_multiple_versions? !(resource.nil? || resource.is_a?(Hyrax::FileMetadata) && !storage_adapter.try(:"supports?", :versions)) end |
#versioned_file_id ⇒ Object
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 latest version is defined.
If the resource is nil, this method returns an empty string.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/services/hyrax/versioning_service.rb', line 78 def versioned_file_id latest = latest_version if latest && !resource.is_a?(Hyrax::FileMetadata) if latest.respond_to?(:id) latest.id else Hyrax.config.translate_uri_to_id.call(latest.uri) end elsif resource.nil? "" elsif resource.is_a?(Hyrax::FileMetadata) latest_version&.version_id || resource.file_identifier else resource.id end end |
#versions ⇒ Object
Returns an array of versions for the resource associated with this Hyrax::VersioningService.
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.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/services/hyrax/versioning_service.rb', line 42 def versions if !supports_multiple_versions? [] elsif resource.is_a?(Hyrax::FileMetadata) # Reverse - Valkyrie puts these most recent first, we assume most recent # last. storage_adapter.find_versions(id: resource.file_identifier).to_a.reverse else return resource.versions if resource.versions.is_a?(Array) resource.versions.all.to_a end end |