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
......@@ -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,9 +177,11 @@ 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
unless @preserve_files
clear
save
end
end
# Returns the name of the file as originally assigned, and lives in the
# <attachment>_file_name attribute of the model.
......@@ -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