Permalink
Browse files

Abstract the Tempfile generation into a factory

  • Loading branch information...
1 parent b920ee4 commit 073152eea6a08dfcd886dc7a169c2d31e67b4402 @jyurek jyurek committed Jun 8, 2012
View
@@ -34,6 +34,7 @@
require 'paperclip/tempfile'
require 'paperclip/thumbnail'
require 'paperclip/interpolations'
+require 'paperclip/tempfile_factory'
require 'paperclip/style'
require 'paperclip/attachment'
require 'paperclip/attachment_options'
@@ -1,19 +1,9 @@
module Paperclip
class AbstractAdapter
- ILLEGAL_FILENAME_CHARACTERS = /^~/
-
private
def destination
- if @destination.nil?
- extension = File.extname(original_filename)
- basename = File.basename(original_filename, extension)
- basename = basename.gsub(ILLEGAL_FILENAME_CHARACTERS, '_')
- dest = Tempfile.new([basename, extension])
- dest.binmode
- @destination = dest
- end
- @destination
+ @destination ||= TempfileFactory.new.generate(original_filename)
end
def copy_to_tempfile(src)
@@ -0,0 +1,21 @@
+module Paperclip
+ class TempfileFactory
+
+ ILLEGAL_FILENAME_CHARACTERS = /^~/
+
+ def generate(name)
+ @name = name
+ file = Tempfile.new([basename, extension])
+ file.binmode
+ file
+ end
+
+ def extension
+ File.extname(@name)
+ end
+
+ def basename
+ File.basename(@name, extension).gsub(ILLEGAL_FILENAME_CHARACTERS, '_')
+ end
+ end
+end
@@ -0,0 +1,13 @@
+require './test/helper'
+
+class Paperclip::TempfileFactoryTest < Test::Unit::TestCase
+ should "be able to generate a tempfile with the right name" do
+ file = subject.generate("omg.png")
+ end
+ should "be able to generate a tempfile with the right name with a tilde at the beginning" do
+ file = subject.generate("~omg.png")
+ end
+ should "be able to generate a tempfile with the right name with a tilde at the end" do
+ file = subject.generate("omg.png~")
+ end
+end

0 comments on commit 073152e

Please sign in to comment.