Commit 9574cd00 by Prem Sichanugrist

Merge pull request #674 from teefax/keep_old_files

Add ':keep_old_files' option to `has_attached_file`

Set this option to `true` will make paperclip retain your file from the storage engine.
parents b4ff2c57 345ec743
......@@ -266,6 +266,9 @@ module Paperclip
# 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
......
......@@ -208,7 +208,7 @@ module Paperclip
# 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
......
......@@ -893,6 +893,41 @@ class AttachmentTest < Test::Unit::TestCase
@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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment