Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Add 'keep_old_files' option.
Browse files Browse the repository at this point in the history
From pcreux@5237495

Conflicts:

	lib/paperclip/attachment.rb
  • Loading branch information
teefax committed Dec 13, 2011
1 parent b4ff2c5 commit 345ec74
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/paperclip.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/attachment.rb
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions test/attachment_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 345ec74

Please sign in to comment.