-
-
Save JayTeeSF/a084779355490411e509b1a82d546693 to your computer and use it in GitHub Desktop.
Copies S3-stored Paperclip attachments from one AR model to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/paperclip/copy_attachments.rb | |
# Copies S3-stored Paperclip attachments from one AR model to another. | |
# | |
# This module should be mixed into the target AR model. | |
if Gem::Version.new(::AWS::VERSION) >= Gem::Version.new(2) | |
raise NotImplementedError, 'coded for aws-sdk v1' | |
end | |
module Paperclip | |
module CopyAttachments | |
# Copies S3-stored attachments from the source ActiveRecord instance. | |
def copy_attachments_from(source) | |
self.class.attachment_definitions.keys.each do |attachment_name| | |
source_attachment = source.send attachment_name | |
next if source_attachment.blank? | |
next if source_attachment.options[:storage] != :s3 | |
destination_attachment = send attachment_name | |
[:original, *destination_attachment.styles.keys].uniq.map do |style| | |
Paperclip.log "S3 copy: #{source_attachment.path(style)} -> #{destination_attachment.path(style)}" | |
source_s3_object = source_attachment.s3_object style | |
destination_s3_object = destination_attachment.s3_object style | |
source_s3_object.copy_to destination_s3_object, acl: source_attachment.s3_permissions(style) | |
end | |
end | |
end | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/paperclip.rb | |
require 'paperclip/copy_attachments' | |
ActiveRecord::Base.send :include, Paperclip::CopyAttachments |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment