Commander Johnson
2011-Jun-12 11:29 UTC
Monkey patch for Paperclip to prevent it from deleting attachment files in test mode
Hello, For anyone who might stumble upon the following problem: In my unit test, I''m checking if the deletion of a Paperclip attachment on my model actually works. However, when I call Object.save, my file is deleted from disk! You don''t want this to happen in a test environment. Inspired by some ideas on the interwebz, (specifically on the Stack Overflow site) I created this monkey patch. lib/paperclip_monkey_patch.rb: # -*- encoding : utf-8 -*- # Only in test mode do we prevent Paperclip from deleting attachments on disk module Paperclip class Attachment def clear # nop #raise "hell" # op instance_write(:file_name, nil) instance_write(:content_type, nil) instance_write(:file_size, nil) instance_write(:updated_at, nil) end end end Then add this line at the top of any test file that deleted attachments: require ''paperclip_monkey_patch'' Now, when your attachments are deleted, the file they point to is not deleted by Paperclip. All the best, CmdJohnson -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.