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 ...@@ -21,7 +21,8 @@ module Paperclip
:whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails], :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails],
:use_default_time_zone => true, :use_default_time_zone => true,
:hash_digest => "SHA1", :hash_digest => "SHA1",
:hash_data => ":class/:attachment/:id/:style/:updated_at" :hash_data => ":class/:attachment/:id/:style/:updated_at",
:preserve_files => false
} }
end end
...@@ -55,6 +56,7 @@ module Paperclip ...@@ -55,6 +56,7 @@ module Paperclip
@hash_secret = options[:hash_secret] @hash_secret = options[:hash_secret]
@convert_options = options[:convert_options] @convert_options = options[:convert_options]
@processors = options[:processors] @processors = options[:processors]
@preserve_files = options[:preserve_files]
@options = options @options = options
@post_processing = true @post_processing = true
@queued_for_delete = [] @queued_for_delete = []
...@@ -175,9 +177,11 @@ module Paperclip ...@@ -175,9 +177,11 @@ module Paperclip
# nil to the attachment *and saving*. This is permanent. If you wish to # nil to the attachment *and saving*. This is permanent. If you wish to
# wipe out the existing attachment but not save, use #clear. # wipe out the existing attachment but not save, use #clear.
def destroy def destroy
unless @preserve_files
clear clear
save save
end end
end
# Returns the name of the file as originally assigned, and lives in the # Returns the name of the file as originally assigned, and lives in the
# <attachment>_file_name attribute of the model. # <attachment>_file_name attribute of the model.
...@@ -366,7 +370,7 @@ module Paperclip ...@@ -366,7 +370,7 @@ module Paperclip
end end
def queue_existing_for_delete #:nodoc: 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| @queued_for_delete += [:original, *styles.keys].uniq.map do |style|
path(style) if exists?(style) path(style) if exists?(style)
end.compact end.compact
......
...@@ -998,4 +998,27 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -998,4 +998,27 @@ class AttachmentTest < Test::Unit::TestCase
end end
end 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 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