Class: Hyrax::VersioningService

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, storage_adapter: Hyrax.storage_adapter) ⇒ VersioningService

Returns a new instance of VersioningService.

Parameters:



23
24
25
26
# File 'app/services/hyrax/versioning_service.rb', line 23

def initialize(resource:, storage_adapter: Hyrax.storage_adapter)
  @storage_adapter = storage_adapter
  self.resource = resource
end

Instance Attribute Details

#resourceActiveFedora::File | Hyrax::FileMetadata | NilClass

Returns:



14
15
16
# File 'app/services/hyrax/versioning_service.rb', line 14

def resource
  @resource
end

#storage_adapterObject (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

Parameters:



91
92
93
94
# File 'app/services/hyrax/versioning_service.rb', line 91

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



97
98
99
# File 'app/services/hyrax/versioning_service.rb', line 97

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

Parameters:



109
110
111
112
113
114
115
# File 'app/services/hyrax/versioning_service.rb', line 109

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



102
103
104
# File 'app/services/hyrax/versioning_service.rb', line 102

def versioned_file_id(file)
  Hyrax::VersioningService.new(resource: file).versioned_file_id
end

Instance Method Details

#latest_versionObject

Returns the latest version of the file associated with this Hyrax::VersioningService.



50
51
52
# File 'app/services/hyrax/versioning_service.rb', line 50

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.

Returns:

  • (Boolean)


60
61
62
# File 'app/services/hyrax/versioning_service.rb', line 60

def supports_multiple_versions?
  !(resource.nil? || resource.is_a?(Hyrax::FileMetadata) && !storage_adapter.try(:"supports?", :versions))
end

#versioned_file_idObject

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.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/services/hyrax/versioning_service.rb', line 70

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

#versionsObject

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.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/hyrax/versioning_service.rb', line 34

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