Skip to content

Instantly share code, notes, and snippets.

@kylekeesling
Created April 26, 2018 13:02
Show Gist options
  • Save kylekeesling/87231b253ab15b0ea99f65f755ebd576 to your computer and use it in GitHub Desktop.
Save kylekeesling/87231b253ab15b0ea99f65f755ebd576 to your computer and use it in GitHub Desktop.
Migrating from Paperclip to ActiveStorage
class Organization < ApplicationRecord
has_one_attached :logo
# Old Paperclip config
# must be removed BEFORE to running the rake task
has_attached_file :logo,
path: "/companies/:id/:basename_:style.:extension",
default_url: "https://xxxxx.cloudfront.net/companies/missing_:style.jpg",
default_style: :normal,
styles: { thumb: "64x64#", normal: "400x400>" },
convert_options: { thumb: "-quality 100 -strip", normal: "-quality 75 -strip" }
end
namespace :organizations do
task migrate_to_active_storage: :environment do
Organization.where.not(logo_file_name: nil).find_each do |organization|
image = organization.logo_file_name
ext = File.extname(image)
image_original = URI.unescape(image.gsub(ext, "_original#{ext}"))
logo_url = "https://s3.amazonaws.com/xxxxx/companies/#{organization.id}/#{image_original}"
organization.logo.attach(io: open(logo_url),
filename: organization.logo_file_name,
content_type: organization.logo_content_type)
end
end
end
class User < ApplicationRecord
has_one_attached :avatar
# Old Paperclip config
# can be removed AFTER you run the rake task
has_attached_file :headshot,
path: "/users/:id/:basename_:style.:extension",
default_url: "https://xxxxx.cloudfront.net/users/missing_:style.jpg",
default_style: :normal,
styles: { thumb: "64x64#", normal: "400x400>" },
convert_options: { thumb: "-quality 100 -strip", normal: "-quality 75 -strip" }
end
namespace :users do
task migrate_to_active_storage: :environment do
User.where.not(headshot_file_name: nil).find_each do |user|
user.avatar.attach(io: open(user.headshot.url(:original, timestamp: false)),
filename: user.headshot_file_name,
content_type: user.headshot_content_type)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment