Browse files

Add 'keep_old_files' option.

From pcreux@5237495

Conflicts:

	lib/paperclip/attachment.rb
  • Loading branch information...
1 parent b4ff2c5 commit 345ec743fc512f290e90451a7776d824f7e3b566 @teefax teefax committed Dec 13, 2011
Showing with 39 additions and 1 deletion.
  1. +3 −0 lib/paperclip.rb
  2. +1 −1 lib/paperclip/attachment.rb
  3. +35 −0 test/attachment_test.rb
View
3 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
View
2 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
View
35 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

0 comments on commit 345ec74

Please sign in to comment.