Sha256: 8b253d8c6cec42755b21bd6887f145c8760dbd5645e12ed4cc83fc74e5c00d8b

Contents?: true

Size: 946 Bytes

Versions: 5

Compression:

Stored size: 946 Bytes

Contents

namespace :carrierwave do
  desc "Migrate to new storage from file"
  task :from_file_to_qiniu, [:model_class, :attribute] => :environment do |t, args|
    model_class, attribute = args.model_class, args.attribute
    model_class.constantize.all.each do |model|
      puts "migrate: #{model_class} #{model.id}"
      if model.try(attribute).present?
        attr = model.try(attribute)
        filename = File.basename(attr.path)
        # 使用原有的 store_dir 找到本地文件,如果 store_dir 逻辑没有改的话。
        file = Rails.root.join('public', attr.store_dir, filename)
        puts "Upload #{file}"
        File.open(file) do |f|
          model.public_send("#{attribute}=", f)
        end
        model.save!
      end
    end
  end

  # TODO:  独立出 public/uploads 目录, 和 store_dir 策略
  # def store_dir(model, attribute)
  #   "uploads/#{model.class.to_s.underscore}/#{attribute}/#{model.id}"
  # end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
blacksand-2.5.0 lib/tasks/carrierwave_storage_migrate_to_qiniu.rake
blacksand-2.4.1 lib/tasks/carrierwave_storage_migrate_to_qiniu.rake
blacksand-2.4.0 lib/tasks/carrierwave_storage_migrate_to_qiniu.rake
blacksand-2.3.0 lib/tasks/carrierwave_storage_migrate_to_qiniu.rake
blacksand-2.2.2.1 lib/tasks/carrierwave_storage_migrate_to_qiniu.rake