From 345ec743fc512f290e90451a7776d824f7e3b566 Mon Sep 17 00:00:00 2001 From: Eike Bernhardt Date: Tue, 13 Dec 2011 10:09:59 +0100 Subject: [PATCH] Add 'keep_old_files' option. From https://github.com/pcreux/paperclip/commit/52374958ef83f1641cf008aead27c23b25fb842d Conflicts: lib/paperclip/attachment.rb --- lib/paperclip.rb | 3 +++ lib/paperclip/attachment.rb | 2 +- test/attachment_test.rb | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/paperclip.rb b/lib/paperclip.rb index 02026cb15..d6a06344b 100644 --- a/lib/paperclip.rb +++ b/lib/paperclip.rb @@ -266,6 +266,9 @@ module ClassMethods # has_attached_file :avatar, :styles => { :normal => "100x100#" }, # :default_style => :normal # user.avatar.url # => "/avatars/23/normal_me.png" + # * +keep_old_files+: Keep the existing attachment files (original + resized) from + # being automatically deleted when an attachment is cleared or updated. + # Defaults to +false+.# # * +whiny+: Will raise an error if Paperclip cannot post_process an uploaded file due # to a command line error. This will override the global setting for this attachment. # Defaults to true. This option used to be called :whiny_thumbanils, but this is diff --git a/lib/paperclip/attachment.rb b/lib/paperclip/attachment.rb index d172124e8..1587bd189 100644 --- a/lib/paperclip/attachment.rb +++ b/lib/paperclip/attachment.rb @@ -208,7 +208,7 @@ def dirty? # Saves the file, if there are no errors. If there are, it flushes them to # the instance's errors and returns false, cancelling the save. def save - flush_deletes + flush_deletes unless @options[:keep_old_files] flush_writes @dirty = false true diff --git a/test/attachment_test.rb b/test/attachment_test.rb index 6e41befa6..5f5c89e7b 100644 --- a/test/attachment_test.rb +++ b/test/attachment_test.rb @@ -893,6 +893,41 @@ def do_after_all; end @attachment.destroy @existing_names.each{|f| assert ! File.exists?(f) } end + + context "when keeping old files" do + setup do + @attachment.options[:keep_old_files] = true + end + + should "keep the files after assigning nil" do + @attachment.expects(:instance_write).with(:file_name, nil) + @attachment.expects(:instance_write).with(:content_type, nil) + @attachment.expects(:instance_write).with(:file_size, nil) + @attachment.expects(:instance_write).with(:updated_at, nil) + @attachment.assign nil + @attachment.save + @existing_names.each{|f| assert File.exists?(f) } + end + + should "keep the files when you call #clear and #save" do + @attachment.expects(:instance_write).with(:file_name, nil) + @attachment.expects(:instance_write).with(:content_type, nil) + @attachment.expects(:instance_write).with(:file_size, nil) + @attachment.expects(:instance_write).with(:updated_at, nil) + @attachment.clear + @attachment.save + @existing_names.each{|f| assert File.exists?(f) } + end + + should "keep the files when you call #delete" do + @attachment.expects(:instance_write).with(:file_name, nil) + @attachment.expects(:instance_write).with(:content_type, nil) + @attachment.expects(:instance_write).with(:file_size, nil) + @attachment.expects(:instance_write).with(:updated_at, nil) + @attachment.destroy + @existing_names.each{|f| assert File.exists?(f) } + end + end end end end