gist: 20473 Download_button fork
public
Public Clone URL: git://gist.github.com/20473.git
migrate_products_to_paperclip.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class MigrateProductsToPaperclip < ActiveRecord::Migration
  def self.up
    
    # Rename the old "image" column to "old_file_name", since Product.image will now try to do Paperclip stuff
    rename_column :products, :image, :old_file_name
 
    # Rename asset directories to pluralize
    File.rename("public/system/product","public/system/products")
    File.rename("public/system/products/image","public/system/products/images")
 
    Product.all.each do |p|
      unless p.old_file_name.blank?
 
        # If an image was set by file_column, copy the relevant parameters to the new Paperclip columns
        p.update_attributes(
        :image_file_name => p.old_file_name,
        :image_content_type => 'image/jpeg',
        :image_updated_at => Time.now.utc,
        :image_file_size => File.stat("public/system/products/images/#{p.id}/#{p.old_file_name}").size)
 
        # Now rename the image file to add the original_ prefix
        File.rename("public/system/products/images/#{p.id}/#{p.old_file_name}","public/system/products/images/#{p.id}/original_#{p.old_file_name}")
      end
 
    end
 
    # And finally remove the old_file_name column, which we don't need any more
    remove_column :products, :old_file_name
  end
 
  def self.down
 
    # laziness, impatience, hubris
    raise IrreversibleMigration
  end
end
 
 

Owner

mcornick

Forks

Revisions