Commit 65e8d4f6 by Greg Lappen Committed by Prem Sichanugrist

added :preserve_files option so paperclip can get along with soft-delete plugins…

added :preserve_files option so paperclip can get along with soft-delete plugins like acts_as_paranoid

Closes #152
parent 054a807e
......@@ -401,7 +401,7 @@ module Paperclip
Paperclip.log("Deleting attachments.")
each_attachment do |name, attachment|
attachment.send(:queue_existing_for_delete)
attachment.send(:flush_deletes)
attachment.send(:flush_deletes)
end
end
end
......
......@@ -21,7 +21,8 @@ module Paperclip
:whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails],
:use_default_time_zone => true,
:hash_digest => "SHA1",
:hash_data => ":class/:attachment/:id/:style/:updated_at"
:hash_data => ":class/:attachment/:id/:style/:updated_at",
:preserve_files => false
}
end
......@@ -55,6 +56,7 @@ module Paperclip
@hash_secret = options[:hash_secret]
@convert_options = options[:convert_options]
@processors = options[:processors]
@preserve_files = options[:preserve_files]
@options = options
@post_processing = true
@queued_for_delete = []
......@@ -175,8 +177,10 @@ module Paperclip
# nil to the attachment *and saving*. This is permanent. If you wish to
# wipe out the existing attachment but not save, use #clear.
def destroy
clear
save
unless @preserve_files
clear
save
end
end
# Returns the name of the file as originally assigned, and lives in the
......@@ -366,7 +370,7 @@ module Paperclip
end
def queue_existing_for_delete #:nodoc:
return unless file?
return unless (file? && @preserve_files==false)
@queued_for_delete += [:original, *styles.keys].uniq.map do |style|
path(style) if exists?(style)
end.compact
......
......@@ -998,4 +998,27 @@ class AttachmentTest < Test::Unit::TestCase
end
end
end
context "an attachment with delete_file option set to false" do
setup do
rebuild_model :preserve_files => true
@dummy = Dummy.new
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
@dummy.avatar = @file
@dummy.save!
@attachment = @dummy.avatar
@path = @attachment.path
end
should "not delete the files from storage when attachment is destroyed" do
@attachment.destroy
assert File.exists?(@path)
end
should "not dleete the file when model is destroy" do
@dummy.destroy
assert File.exists?(@path)
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment