Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add 'keep_old_files' option.

  • Loading branch information...
commit 52374958ef83f1641cf008aead27c23b25fb842d 1 parent e430afe
Philippe Creux authored
3  lib/paperclip.rb
@@ -185,6 +185,9 @@ module ClassMethods
185 185 # has_attached_file :avatar, :styles => { :normal => "100x100#" },
186 186 # :default_style => :normal
187 187 # user.avatar.url # => "/avatars/23/normal_me.png"
  188 + # * +keep_old_files+: Keep the existing attachment files (original + resized) from
  189 + # being automatically deleted when an attachment is cleared or updated.
  190 + # Defaults to +false+.#
188 191 # * +whiny+: Will raise an error if Paperclip cannot post_process an uploaded file due
189 192 # to a command line error. This will override the global setting for this attachment.
190 193 # Defaults to true. This option used to be called :whiny_thumbanils, but this is
2  lib/paperclip/attachment.rb
@@ -138,7 +138,7 @@ def dirty?
138 138 # the instance's errors and returns false, cancelling the save.
139 139 def save
140 140 if valid?
141   - flush_deletes
  141 + flush_deletes unless @options[:keep_old_files]
142 142 flush_writes
143 143 @dirty = false
144 144 true
35 test/attachment_test.rb
@@ -652,6 +652,41 @@ def do_after_all; end
652 652 @attachment.destroy
653 653 @existing_names.each{|f| assert ! File.exists?(f) }
654 654 end
  655 +
  656 + context "when keeping old files" do
  657 + setup do
  658 + @attachment.options[:keep_old_files] = true
  659 + end
  660 +
  661 + should "keep the files after assigning nil" do
  662 + @attachment.expects(:instance_write).with(:file_name, nil)
  663 + @attachment.expects(:instance_write).with(:content_type, nil)
  664 + @attachment.expects(:instance_write).with(:file_size, nil)
  665 + @attachment.expects(:instance_write).with(:updated_at, nil)
  666 + @attachment.assign nil
  667 + @attachment.save
  668 + @existing_names.each{|f| assert File.exists?(f) }
  669 + end
  670 +
  671 + should "keep the files when you call #clear and #save" do
  672 + @attachment.expects(:instance_write).with(:file_name, nil)
  673 + @attachment.expects(:instance_write).with(:content_type, nil)
  674 + @attachment.expects(:instance_write).with(:file_size, nil)
  675 + @attachment.expects(:instance_write).with(:updated_at, nil)
  676 + @attachment.clear
  677 + @attachment.save
  678 + @existing_names.each{|f| assert File.exists?(f) }
  679 + end
  680 +
  681 + should "keep the files when you call #delete" do
  682 + @attachment.expects(:instance_write).with(:file_name, nil)
  683 + @attachment.expects(:instance_write).with(:content_type, nil)
  684 + @attachment.expects(:instance_write).with(:file_size, nil)
  685 + @attachment.expects(:instance_write).with(:updated_at, nil)
  686 + @attachment.destroy
  687 + @existing_names.each{|f| assert File.exists?(f) }
  688 + end
  689 + end
655 690 end
656 691 end
657 692 end

7 comments on commit 5237495

Ryan Romanchuk

very nice, has this ever made it back into the official gem? This feature is pretty key (especially for s3)

Philippe Creux
Owner

I don't think so. https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/attachment.rb#L156

Feel free to add similar code to the existing paperclip and try to get it merged into the official gem. :)

Ryan Romanchuk

Ugghh, he has so many pull requests waiting already. I'm surprised not more people have complained about this. If you use paperclip for images that are changing and have any sort of sharing, all your hotlinks die :(

Are you keeping this fork recent with upstream? I may just use your fork to avoid patching.

Philippe Creux
Owner
Eike Bernhardt

I've taken your commit and opened a pull request for it: thoughtbot#674

Philippe Creux
Owner

@teefax Cool! :)

Please sign in to comment.
Something went wrong with that request. Please try again.