Class: Hyrax::FileSetFileService

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

Overview

A service for accessing specific Hyrax::FileMetadata objects referenced by a Hyrax::FileSet.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FileSetFileService.

Parameters:



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

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.



11
12
13
# File 'app/services/hyrax/file_set_file_service.rb', line 11

def file_set
  @file_set
end

#query_serviceObject (readonly)

Returns the value of attribute query_service.



16
17
18
# File 'app/services/hyrax/file_set_file_service.rb', line 16

def query_service
  @query_service
end

Instance Method Details

#original_fileHyrax::FileMetadata

Return the Hyrax::FileMetadata which should be considered “original” 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:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/hyrax/file_set_file_service.rb', line 35

def original_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.
    @original_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