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