Thoughtbot have released a suite of plugins over the past few months that are enlivening the fields of Ruby on Rails file attachments ( paperclip) and authentication ( clearance), long dominated by Rick Olson’s attachment_fu and restful_authentication. You can see some previous posts about attachment_fu here and here.
I’ve been playing with paperclip on a couple of projects, including one which was previously using attachment_fu. That necessitated some work migrating the previous attachments. Opinions are divided on whether data should be transformed in migrations or in separate libraries, and I’d rather remain agnostic on that, but I whipped together some code that can be used either in migrations or an external library to make the transfer.
To use it, do something like:
class ConvertAvatar < ActiveRecord::Migration
include PaperclipMigrations
def self.up
add_paperclip_fields :users, :photo
User.reset_column_information
User.all.each do |user|
populate_paperclip_from_attachment_fu(user, user.avatar, 'photo') if user.avatar
end
end
end
It’s quick and a little dirty, but it’s done the job for me. If you’re using S3 for storage, you might prefer to check out Andrew Timberlake’s writeup.