Created
May 16, 2012 02:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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