Commit 073152ee by Jon Yurek

Abstract the Tempfile generation into a factory

parent b920ee4d
...@@ -34,6 +34,7 @@ require 'paperclip/processor' ...@@ -34,6 +34,7 @@ require 'paperclip/processor'
require 'paperclip/tempfile' require 'paperclip/tempfile'
require 'paperclip/thumbnail' require 'paperclip/thumbnail'
require 'paperclip/interpolations' require 'paperclip/interpolations'
require 'paperclip/tempfile_factory'
require 'paperclip/style' require 'paperclip/style'
require 'paperclip/attachment' require 'paperclip/attachment'
require 'paperclip/attachment_options' require 'paperclip/attachment_options'
......
module Paperclip module Paperclip
class AbstractAdapter class AbstractAdapter
ILLEGAL_FILENAME_CHARACTERS = /^~/
private private
def destination def destination
if @destination.nil? @destination ||= TempfileFactory.new.generate(original_filename)
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
end end
def copy_to_tempfile(src) def copy_to_tempfile(src)
......
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
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment