Class: Hyrax::FileSetFileService

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/file_set_file_service.rb

Overview

Note:

housing the “primary” file abstraction here allows us to begin separating from the idea that the ‘pcdmuse:OriginalFile` is special in Hyrax in a hard coded way.

A service for accessing FileMetadata resources by their status within a FileSet. For example, this is the home for the abstraction of a “Primary” file for the FileSet, used for versioning and as the default source for the FileSet label, etc…

If you’re looking for FileMetadata by PCDM Use, use the custom queries (e.g. Hyrax.custom_queries.find_original_file).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_set:, query_service: Hyrax.query_service) ⇒ FileSetFileService

Returns a new instance of FileSetFileService.

Parameters:



29
30
31
32
# File 'app/services/hyrax/file_set_file_service.rb', line 29

def initialize(file_set:, query_service: Hyrax.query_service)
  @query_service = query_service
  @file_set = file_set
end

Instance Attribute Details

#file_setObject (readonly)

Returns the value of attribute file_set.



20
21
22
# File 'app/services/hyrax/file_set_file_service.rb', line 20

def file_set
  @file_set
end

#query_serviceObject (readonly)

Returns the value of attribute query_service.



25
26
27
# File 'app/services/hyrax/file_set_file_service.rb', line 25

def query_service
  @query_service
end

Class Method Details

.primary_file_for(file_set:, query_service: Hyrax.query_service) ⇒ Hyrax::FileMetadata

Return the Hyrax::FileMetadata which should be considered “primary” for indexing and version‐tracking.

Returns:



39
40
41
# File 'app/services/hyrax/file_set_file_service.rb', line 39

def self.primary_file_for(file_set:, query_service: Hyrax.query_service)
  new(file_set: file_set, query_service: query_service).primary_file
end

Instance Method Details

#primary_fileHyrax::FileMetadata Also known as: original_file

Return the Hyrax::FileMetadata which should be considered “primary” for indexing and version‐tracking.

If file_set.original_file_id is defined, it will be used; otherwise, this requires a custom query. The ultimate fallback, if no pcdm:OriginalFile is associated with the :file_set, is to just use the first file in its :file_ids.

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/hyrax/file_set_file_service.rb', line 53

def primary_file
  if file_set.original_file_id
    # Always just use original_file_id if it is defined.
    #
    # NOTE: This needs to use :find_file_metadata_by, not :find_by, because
    # at time of writing the latter does not work in Wings.
    query_service.custom_queries.(id: file_set.original_file_id)
  else
    # Cache the fallback to avoid needing to do this query twice.
    #
    # See NOTE above regarding use of :find_file_metadata_by.
    @primary_file ||= begin
                        query_service.custom_queries.find_original_file(file_set: file_set)
                      rescue Valkyrie::Persistence::ObjectNotFoundError
                        fallback_id = file_set.file_ids.first
                        query_service.custom_queries.(id: fallback_id) if fallback_id
                      end
  end
end