Permalink
Browse files

Fix broken Tempfile monkey-patch. This is the same fix as in the

original paperclip source (I just copied it over).

Ruby 1.8.7 provides a Tempfile that not only supports extensions, but avoids a bug where reusing the same filename to paperclip in succession (such as in a test) can result in missing file exceptions, since the tempfile is moved, another tempfile is created in the same location (same basename), then the first tempfile's finalizer triggers and remove the new tempfile that we are still working on.
  • Loading branch information...
1 parent 1e2a2c9 commit 8b8781397d81f43f7e4446966ff44f4b939a5a2b @xaviershay xaviershay committed with solnic Nov 27, 2010
Showing with 14 additions and 4 deletions.
  1. +14 −4 lib/dm-paperclip/processor.rb
@@ -40,10 +40,20 @@ def self.make file, options = {}, attachment = nil
# on this blog post:
# http://marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions
class Tempfile < ::Tempfile
- # Replaces Tempfile's +make_tmpname+ with one that honors file extensions.
- def make_tmpname(basename, n)
- extension = File.extname(basename)
- sprintf("%s,%d,%d%s", File.basename(basename, extension), $$, n || 0, extension)
+ # This is Ruby 1.8.7's implementation.
+ if RUBY_VERSION <= "1.8.6"
+ def make_tmpname(basename, n)
+ case basename
+ when Array
+ prefix, suffix = *basename
+ else
+ prefix, suffix = basename, ''
+ end
+
+ t = Time.now.strftime("%y%m%d")
+ path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}"
+ end
end
end
+
end

0 comments on commit 8b87813

Please sign in to comment.