lundi 16 décembre 2019

How to reprocess image versions in carrierwave?

I am trying to write a rake task which should reprocess all my uploaded image versions after I change the version parameters in the uploader file.

I would like to be able to call it from bash or the rails console. For that reason I wrote a rake task and a method reprocess for the relative model diapo.rb which I can call in the rake task. (I followed this answer https://stackoverflow.com/a/31220535)

Currently I have a resource model Diapo.rb

class Diapo < ActiveRecord::Base

  mount_uploader :file_name, DiapoUploader

  def reprocess
    begin
      self.cache_stored_file!
      self.retrieve_from_cache!(self.cache_name)
      self.recreate_versions!
      self.save!
    rescue => e
      STDERR.puts  "ERROR: MyModel: #{id} -> #{e.to_s}"
    end
  end
end

In my uploader file I have specified a series of versions diapo_uploader.rb

class DiapoUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick
  include CarrierWave::MimeTypes

  process :set_content_type

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.downcase.pluralize}/#{mounted_as}/#{model.id}"
  end

  version :diapo0500, :if => :diapo? do
    process :resize_to_fit => [500, 500]
    process :quality => 50
  end

  version :thumb, :if => :diapo? do
    process :resize_to_fit => [200, 200]
    process :quality => 50
  end
end

I wrote a rake task: carrierwave.rake

# CarrierWave rake tasks
#
# Task:   reprocess
# Desc:   Reprocess all diapos
# Usage:  rake carrierwave:reprocess_diapo

namespace :carrierwave do
  task :reprocess_diapo => :environment do
    Diapo.all.each do |d|
      d.reprocess
    end
  end
end

currently I get:

$ rake carrierwave:reprocess_diapo
ERROR: MyModel: 1 -> undefined method `cache_stored_file!' for #<Diapo:0x111cd8428>
ERROR: MyModel: 2 -> undefined method `cache_stored_file!' for #<Diapo:0x111c98300>
ERROR: MyModel: 3 -> undefined method `cache_stored_file!' for #<Diapo:0x111c97db0>
ERROR: MyModel: 4 -> undefined method `cache_stored_file!' for #<Diapo:0x111c97798>
ERROR: MyModel: 5 -> undefined method `cache_stored_file!' for #<Diapo:0x111c97108>
ERROR: MyModel: 6 -> undefined method `cache_stored_file!' for #<Diapo:0x111c96af0>

Rails is 3.2.5 Carrierwave is 0.11.1

What am I doing wrong?

Thank you in advance !

Aucun commentaire:

Enregistrer un commentaire