class ClowneActiveStorage::Resolvers::AttachmentAssociation

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

Attributes

declaration[R]
record[R]
source[R]

Public Class Methods

call(source, record, declaration, **) click to toggle source
# 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
new(source, record, declaration) click to toggle source
# File lib/clowne_active_storage/resolvers/attachment_association.rb, line 15
def initialize(source, record, declaration)
  @source = source
  @record = record
  @declaration = declaration
end

Public Instance Methods

call() click to toggle source
# 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

Private Instance Methods

open_attached(attached, &blk) click to toggle source
# File lib/clowne_active_storage/resolvers/attachment_association.rb, line 45
def open_attached(attached, &blk)
  if attached.respond_to? :open
    attached.open(&blk)
  else
    require 'clowne_active_storage/helpers/active_storage_downloader'
    ActiveStorageDownloader.new(attached).download_blob_to_tempfile(&blk)
  end
end
record_association() click to toggle source
# File lib/clowne_active_storage/resolvers/attachment_association.rb, line 41
def record_association
  record.send declaration.name
end
source_association() click to toggle source
# File lib/clowne_active_storage/resolvers/attachment_association.rb, line 37
def source_association
  source.send declaration.name
end