Skip to content

Instantly share code, notes, and snippets.

@agibralter
Created May 16, 2012 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agibralter/2706768 to your computer and use it in GitHub Desktop.
Save agibralter/2706768 to your computer and use it in GitHub Desktop.
monkey patch for carrierwave to make updates remove previous images when activerecord's identity map is enabled
# -*- encoding : utf-8 -*-
class Image < ActiveRecord::Base
mount_uploader :image, ImageUploader
private
#############################################################################
# CarrierWave's behavior as of 0.5.8 is not compatible with ActiveRecord's
# identity map. This is a monkey patch.
def store_previous_model_for_image
if image.remove_previously_stored_files_after_update && image_changed?
@remove_previous_image = true
end
end
def remove_previously_stored_image
if @remove_previous_image
ImageUploader.new(self, :image).tap do |uploader|
uploader.retrieve_from_store!(image_was.file.original_filename)
uploader.remove!
end
@remove_previous_image = nil
end
end
#############################################################################
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment