Sha256: 44825e21b4481e4ec865eea9dbbd5cb3fa351518d4e30ad179ba50959f9eaef4

Contents?: true

Size: 1.4 KB

Versions: 16

Compression:

Stored size: 1.4 KB

Contents

class AdditionalPaperclipRefactor < ActiveRecord::Migration
  def self.up

    change_table :assets do |t|
      # remove another defunct attachment_fu column
      t.remove :parent_id
      # drop extraneous records for the thumbnails (paperclip knows about these automatically - nice!)
      execute "DELETE FROM assets WHERE viewable_id IS NULL"
      # provide a default value for the type field 
      execute "UPDATE assets SET type = 'Image' WHERE type IS NULL"
    end

    # move legacy images to the new directory structure
    puts "Copying legacy assets ..."
    Image.all.each do |image|
      sizes = %w{mini original product small}
      sizes.each do |size| 
        target_dir = "#{RAILS_ROOT}/public/assets/products/#{image.id}/#{size}" 
        FileUtils.mkdir_p target_dir
        subdir = image.id.to_s.rjust(4, '0')
        # note: not sure where the 0000 comes from with attachment_fu but it seems ok to hardcode it
        filename = image.attachment.original_filename
        unless size == "original"
          filename = filename.gsub(".", "_#{size}.")
        end
        source = "#{RAILS_ROOT}/public/images/products/0000/#{subdir}/#{filename}"
        FileUtils.cp source, target_dir
        FileUtils.mv "#{target_dir}/#{filename}", "#{target_dir}/#{image.attachment.original_filename}" unless size == "original"
      end
    end
    puts "Finished."
  end

  def self.down
    # no going back!
  end
end

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
kdmny-spree-0.0.1 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-enriquez-0.9.4 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.9.4 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.9.3 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.9.2 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.9.1 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.9.0 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.8.4 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.8.5 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.6.0 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.7.1 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.7.0 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.8.0 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.8.1 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.8.2 db/migrate/20081121192045_additional_paperclip_refactor.rb
spree-0.8.3 db/migrate/20081121192045_additional_paperclip_refactor.rb