Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bbwharris/957179 to your computer and use it in GitHub Desktop.
Save bbwharris/957179 to your computer and use it in GitHub Desktop.
module PaperclipMigrations
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def add_paperclip_fields(table, prefix)
add_column table, "#{prefix}_file_name", :string
add_column table, "#{prefix}_content_type", :string
add_column table, "#{prefix}_file_size", :integer
add_column table, "#{prefix}_updated_at", :datetime
end
def remove_paperclip_fields(table, prefix)
remove_column table, "#{prefix}_file_name"
remove_column table, "#{prefix}_content_type"
remove_column table, "#{prefix}_file_size"
remove_column table, "#{prefix}_updated_at"
end
def populate_paperclip_from_attachment_fu(model, attachment, prefix, path_prefix)
unless attachment.filename.nil?
model.send("#{prefix}_file_name=", attachment.filename)
model.send("#{prefix}_content_type=", attachment.content_type)
model.send("#{prefix}_file_size=", attachment.size)
file_path = ("%08d" % model.id).scan(/..../).join('/')
old_path = File.join(RAILS_ROOT, 'public', path_prefix, file_path, attachment.filename)
new_path = model.send(prefix).path(:original)
if File.exists?(old_path)
puts "Moving #{old_path} to Paperclip"
file = File.open(old_path)
model.send("#{prefix}=",file)
model.save
file.close
else
puts "No such file: #{old_path}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment