0

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 !

UPDATE

Thanks to @Abhishek I updated my model Diapo.rb by adding the relative attribute:

class Diapo < ActiveRecord::Base

  mount_uploader :file_name, DiapoUploader

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

This time I get the following error:

$ rake carrierwave:reprocess_diapo
ERROR: MyModel: 1 -> uninitialized constant MimeMagic::Encoding
...

The initial upload goes well. Only the preprocessing does throw this error.

SEJU
  • 995
  • 1
  • 9
  • 28

1 Answers1

1

recreate_versions! method works on the corresponding attribute in the model.

In your case, it should be something like:

class Diapo < ActiveRecord::Base

  mount_uploader :file_name, DiapoUploader

  def reprocess
    file_name.recreate_versions!
  end
end
Abhishek
  • 331
  • 3
  • 7
  • Thanks @Abhishek! Now I get `uninitialized constant MimeMagic::Encoding`. Do you know why? – SEJU Dec 16 '19 at 16:20
  • include CarrierWave::MimeTypes #--------------> This line might be the issue in ur DiapoUploader – Abhishek Dec 16 '19 at 16:31
  • I tried to comment it out, but to no avail... Also the initial upload goes well. Only the reprocessing throws an error .... – SEJU Dec 16 '19 at 16:40
  • I tried it in Rails 6 with Carrierwave 2.0.2 and reprocessing works ! There is some problem with Carrierwave 0.11.1. I will mark your answer as correct. – SEJU Dec 17 '19 at 14:52