Permalink
Browse files

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

…ete plugins like acts_as_paranoid


Closes #152
  • Loading branch information...
glappen authored and sikachu committed Feb 12, 2010
1 parent 054a807 commit 65e8d4f6de50732d8e1b84fc8a5304243c5e8f38
Showing with 32 additions and 5 deletions.
  1. +1 −1 lib/paperclip.rb
  2. +8 −4 lib/paperclip/attachment.rb
  3. +23 −0 test/attachment_test.rb
View
@@ -401,7 +401,7 @@ def destroy_attached_files
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 @@ def self.default_options
: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 @@ def initialize name, instance, options = {}
@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 @@ def clear
# 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 @@ def interpolate pattern, style_name = default_style #:nodoc:
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
View
@@ -998,4 +998,27 @@ def do_after_all; 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

0 comments on commit 65e8d4f

Please sign in to comment.