Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add 'keep_old_files' option.

From pcreux@5237495

Conflicts:

	lib/paperclip/attachment.rb
  • Loading branch information...
commit 345ec743fc512f290e90451a7776d824f7e3b566 1 parent b4ff2c5
@teefax teefax authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.