Class: ClowneActiveStorage::Resolvers::AttachmentAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/clowne_active_storage/resolvers/attachment_association.rb

Overview

Clone the files attached to source and attach the clones to the record

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, record, declaration) ⇒ AttachmentAssociation

Returns a new instance of AttachmentAssociation.



15
16
17
18
19
# File 'lib/clowne_active_storage/resolvers/attachment_association.rb', line 15

def initialize(source, record, declaration)
  @source = source
  @record = record
  @declaration = declaration
end

Instance Attribute Details

#declarationObject (readonly)

Returns the value of attribute declaration.



13
14
15
# File 'lib/clowne_active_storage/resolvers/attachment_association.rb', line 13

def declaration
  @declaration
end

#recordObject (readonly)

Returns the value of attribute record.



13
14
15
# File 'lib/clowne_active_storage/resolvers/attachment_association.rb', line 13

def record
  @record
end

#sourceObject (readonly)

Returns the value of attribute source.



13
14
15
# File 'lib/clowne_active_storage/resolvers/attachment_association.rb', line 13

def source
  @source
end

Class Method Details

.call(source, record, declaration) ⇒ Object



7
8
9
10
11
# File 'lib/clowne_active_storage/resolvers/attachment_association.rb', line 7

def self.call(source, record, declaration, **)
  new(source, record, declaration).call
rescue Errno::ENOENT
  record
end

Instance Method Details

#callObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/clowne_active_storage/resolvers/attachment_association.rb', line 21

def call
  return record unless source_association.attached?

  source_association.attachments.each do |attachment|
    open_attached attachment do |tempfile|
      record_association.attach(
        io: tempfile, filename: attachment.filename,
        content_type: attachment.content_type
      )
    end
  end
  record
end