Skip to content

Instantly share code, notes, and snippets.

@cknoxrun
Created January 24, 2018 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cknoxrun/b492fa25811446ca0f163b095446eb8f to your computer and use it in GitHub Desktop.
Save cknoxrun/b492fa25811446ca0f163b095446eb8f to your computer and use it in GitHub Desktop.
namespace :update do
desc "Upload all Paperclip attachments to S3"
task s3: [:environment] do
models = {
documents: [Document, :file],
ms_ms_peak_assignments: [MsMsPeakAssignment, :assignment_view],
sops: [Sop, :file]
}
models.each do |name, (klass, attachment_method)|
Dir.glob("public/system/#{name}/#{attachment_method.to_s.pluralize}/**/*").reject { |f| File.directory?(f) }.each do |path|
attachment = File.open path
id = File.expand_path(attachment).match(/([\d\/]+)\/original\/.+$/)[1].remove("/").to_i
puts "Re-saving #{klass} ##{id}..."
record = klass.find_by(id: id)
if record.nil?
puts "\tCan't find record"
next
end
# Paperclip will re-save the attachment using the new settings
record.send("#{attachment_method}=", attachment)
record.save!
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment