Module: ActiveFedora::AttachedFiles

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/active_fedora/attached_files.rb

Instance Method Summary collapse

Instance Method Details

#add_file(file, opts) ⇒ Object

Attach the given stream/string to object

Parameters:

  • file (IO, String)

    the file to add

  • args (Hash)

    options: :dsid, :prefix, :mime_type

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :path (String)

    The file path

  • :prefix (String)

    The path prefix (for auto-generated path)

  • :mime_type (String)

    The Mime-Type of the file

  • :original_name (String)

    The original name of the file (used for Content-Disposition)



72
73
74
75
76
77
78
# File 'lib/active_fedora/attached_files.rb', line 72

def add_file(file, opts)
  find_or_create_child_resource(opts[:path], opts[:prefix]).tap do |node|
    node.content = file
    node.mime_type = opts[:mime_type]
    node.original_name = opts[:original_name]
  end
end

#attach_file(file, file_path, _opts = {}) ⇒ String

Add an ActiveFedora::File to the object.

Parameters:

Returns:

  • (String)

    path of the added datastream



49
50
51
52
53
# File 'lib/active_fedora/attached_files.rb', line 49

def attach_file(file, file_path, _opts = {})
  create_singleton_association(file_path)
  attached_files[file_path] = file
  file_path
end

#attached_filesObject

Returns all known attached files for the object. If the object has been saved to fedora, the persisted files will be included. Attached files that have been modified in memory are given preference over the copy in Fedora.



24
25
26
# File 'lib/active_fedora/attached_files.rb', line 24

def attached_files
  @attached_files ||= FilesHash.new(self)
end

#clear_attached_filesObject



28
29
30
# File 'lib/active_fedora/attached_files.rb', line 28

def clear_attached_files
  @attached_files = nil
end

#contains_assertionsObject



32
33
34
# File 'lib/active_fedora/attached_files.rb', line 32

def contains_assertions
  resource.query(subject: resource, predicate: ::RDF::Vocab::LDP.contains).objects.map(&:to_s)
end

#declared_attached_filesObject

Returns only the attached_files that are declared with ‘contains’



10
11
12
13
14
# File 'lib/active_fedora/attached_files.rb', line 10

def declared_attached_files
  attached_files.reflections.keys.each_with_object({}) do |k, h|
    h[k] = attached_files[k]
  end
end

#load_attached_filesObject



36
37
38
39
40
41
42
# File 'lib/active_fedora/attached_files.rb', line 36

def load_attached_files
  contains_assertions.each do |file_uri|
    path = file_uri.to_s.sub(uri + '/', '')
    next if association(path.to_sym)
    create_singleton_association(path)
  end
end

#metadata_streamsArray

Returns all attached files that return true for ‘metadata?` and are not Rels-ext.

Returns:

  • (Array)

    all attached files that return true for ‘metadata?` and are not Rels-ext



56
57
58
# File 'lib/active_fedora/attached_files.rb', line 56

def 
  attached_files.select { |_k, ds| ds.metadata? }.values
end

#serialize_attached_filesObject



5
6
7
# File 'lib/active_fedora/attached_files.rb', line 5

def serialize_attached_files
  declared_attached_files.each_value(&:serialize!)
end

#undeclared_filesObject



80
81
82
# File 'lib/active_fedora/attached_files.rb', line 80

def undeclared_files
  @undeclared_files ||= []
end