How to: use callbacks

Ojash Dahal edited this page Nov 25, 2015 · 5 revisions

How to: Use Callbacks

Callbacks allow you to hook in your own code when a particular CarrierWave event occurs. The following callbacks are available for use in CarrierWave uploaders:

  • :cache
  • :retrieve_from_cache
  • :store
  • :retrieve_from_store
  • :remove

Example

class MyUploader < CarrierWave::Uploader::Base
  #....
  after :cache, :unlink_original

  def unlink_original(file)
    return unless delete_original_file
    File.delete if version_name.blank?
  end
end

Using callbacks for a specific version of uploaded files

When you have multiple versions of files(images) to be processed and just want the callback to be triggered for a particular version then you can simply use the callback within the versions.

Example

class MyUploader < CarrierWave::Uploader::Base
  #....

  version :thumb50 do
    process :resize_to_fit => [50, 50]
  end

  version :thumb100 do
    process :resize_to_fit => [100, 100]
    after :store, :callback_method
  end
   
  def callback_method file
   puts self.version_name
  end
end
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.