Ognjen Regoje bio photo

Ognjen Regoje
But you can call me Oggy


I make things that run on the web (mostly).
More ABOUT me and my PROJECTS.

me@ognjen.io LinkedIn

Overriding preform in Carrierwave Backgrounder gem

#technical

Carrierwave Backgrounder is a gem that makes it easier to process assets in background. Resizing and optimization can be done on Sidekiq rather than in the main thread. Very useful.

It’s possible to override the default worker, the instructions to which are here.

I needed it to do some other things at the same time, so I went around digging in the source to see how it actually works.

This does the actual work. It functions the same way as the normal processor – by toggling a flag that’s then picked up by the processer.

It includes this for some set up.

So, a custom worker that does both (and more) looks like this:

class AssetProcesser < ::CarrierWave::Workers::ProcessAsset
  def perform(*args)
    set_args(*args)
    record = constantized_resource.find id

    if record && record.send(:"#{column}").present?
      record.send(:"process_#{column}_upload=", true)
      if record.send(:"#{column}").recreate_versions! && record.respond_to?(:"#{column}_processing")
        record.update_attribute :"#{column}_processing", false
      end
    else
      when_not_ready
    end

    # And the rest of the functionality here.

  end
end