Sha256: 98b31725de1bf41e276989915f5ab6f7c1bcde2be9cfd72382a3b4615ba60fa9

Contents?: true

Size: 1.05 KB

Versions: 23

Compression:

Stored size: 1.05 KB

Contents

module Para
  module Cloneable
    class AttachmentsCloner
      attr_reader :original, :clone

      def initialize(original, clone)
        @original = original
        @clone = clone
      end
      
      def clone!
        return unless defined?(ActiveStorage)
        
        attachment_reflections = original.class.reflections.select { |k, v| 
          k.to_s.match(/_attachment\z/) && 
          v.options[:class_name] == "ActiveStorage::Attachment" 
        }

        attachment_reflections.each do |name, reflection|
          original_attachment = original.send(name)
          next unless original_attachment
          
          association_name = name.gsub(/_attachment\z/, "")

          Para::ActiveStorageDownloader.new(original_attachment).download_blob_to_tempfile do |tempfile|
            clone.send(association_name).attach({
              io: tempfile,
              filename: original_attachment.blob.filename,
              content_type: original_attachment.blob.content_type
            })
          end
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
para-0.9.3.1 lib/para/cloneable/attachments_cloner.rb
para-0.9.2 lib/para/cloneable/attachments_cloner.rb
para-0.9.0 lib/para/cloneable/attachments_cloner.rb
para-0.8.15 lib/para/cloneable/attachments_cloner.rb
para-0.8.14 lib/para/cloneable/attachments_cloner.rb
para-0.8.13 lib/para/cloneable/attachments_cloner.rb
para-0.8.12 lib/para/cloneable/attachments_cloner.rb
para-0.8.11 lib/para/cloneable/attachments_cloner.rb
para-0.8.10 lib/para/cloneable/attachments_cloner.rb
para-0.8.9 lib/para/cloneable/attachments_cloner.rb
para-0.8.8 lib/para/cloneable/attachments_cloner.rb
para-0.8.7 lib/para/cloneable/attachments_cloner.rb
para-0.8.5 lib/para/cloneable/attachments_cloner.rb
para-0.8.3.3 lib/para/cloneable/attachments_cloner.rb
para-0.8.3.2 lib/para/cloneable/attachments_cloner.rb
para-0.8.3.1 lib/para/cloneable/attachments_cloner.rb
para-0.8.3 lib/para/cloneable/attachments_cloner.rb
para-0.8.2.3 lib/para/cloneable/attachments_cloner.rb
para-0.8.2.2 lib/para/cloneable/attachments_cloner.rb
para-0.8.2.1 lib/para/cloneable/attachments_cloner.rb