Permalink
Browse files

Fix a problem with `Rack::Test::UploadedFile`

In Rails, `ActionDispatch::Http::UploadedFile` has an accessor to access
a `Tempfile` object. However, `Rack::Test::UploadedFile` does not
provide that, but provinding the `#path` method instead. To be able to
support both, we have to check for existance for `#tempfile` method,
then fallback to `#path` method.

Fixes #807
  • Loading branch information...
1 parent 237675d commit 7d84569092f03cf5d9d487e7b667408e4bc9fa59 @sikachu sikachu committed Apr 4, 2012
Showing with 78 additions and 34 deletions.
  1. +6 −1 lib/paperclip/io_adapters/uploaded_file_adapter.rb
  2. +1 −2 test/helper.rb
  3. +71 −31 test/uploaded_file_adapter_test.rb
View
7 lib/paperclip/io_adapters/uploaded_file_adapter.rb
@@ -2,7 +2,12 @@ module Paperclip
class UploadedFileAdapter
def initialize(target)
@target = target
- @tempfile = copy_to_tempfile(@target.tempfile)
+
+ if @target.respond_to?(:tempfile)
+ @tempfile = copy_to_tempfile(@target.tempfile)
+ else
+ @tempfile = copy_to_tempfile(@target)
+ end
end
def original_filename
View
3 test/helper.rb
@@ -13,8 +13,7 @@
require 'active_support/core_ext'
require 'mime/types'
require 'pathname'
-
-require 'pathname'
+require 'ostruct'
puts "Testing against version #{ActiveRecord::VERSION::STRING}"
View
102 test/uploaded_file_adapter_test.rb
@@ -2,44 +2,84 @@
class UploadedFileAdapterTest < Test::Unit::TestCase
context "a new instance" do
- setup do
- class UploadedFile < OpenStruct; end
- @file = UploadedFile.new(
- :original_filename => "5k.png",
- :content_type => "image/png",
- :head => "",
- :tempfile => File.new(fixture_file("5k.png"))
- )
- @subject = Paperclip.io_adapters.for(@file)
- end
+ context "with UploadedFile responding to #tempfile" do
+ setup do
+ class UploadedFile < OpenStruct; end
+ @file = UploadedFile.new(
+ :original_filename => "5k.png",
+ :content_type => "image/png",
+ :head => "",
+ :tempfile => File.new(fixture_file("5k.png"))
+ )
+ @subject = Paperclip.io_adapters.for(@file)
+ end
- should "get the right filename" do
- assert_equal "5k.png", @subject.original_filename
- end
+ should "get the right filename" do
+ assert_equal "5k.png", @subject.original_filename
+ end
- should "get the content type" do
- assert_equal "image/png", @subject.content_type
- end
+ should "get the content type" do
+ assert_equal "image/png", @subject.content_type
+ end
- should "get the file's size" do
- assert_equal 4456, @subject.size
- end
+ should "get the file's size" do
+ assert_equal 4456, @subject.size
+ end
- should "return false for a call to nil?" do
- assert ! @subject.nil?
- end
+ should "return false for a call to nil?" do
+ assert ! @subject.nil?
+ end
- should "generate a MD5 hash of the contents" do
- expected = Digest::MD5.file(@file.tempfile.path).to_s
- assert_equal expected, @subject.fingerprint
- end
+ should "generate a MD5 hash of the contents" do
+ expected = Digest::MD5.file(@file.tempfile.path).to_s
+ assert_equal expected, @subject.fingerprint
+ end
- should "read the contents of the file" do
- expected = @file.tempfile.read
- assert expected.length > 0
- assert_equal expected, @subject.read
+ should "read the contents of the file" do
+ expected = @file.tempfile.read
+ assert expected.length > 0
+ assert_equal expected, @subject.read
+ end
end
- end
+ context "with UploadFile responding to #path" do
+ setup do
+ class UploadedFile < OpenStruct; end
+ @file = UploadedFile.new(
+ :original_filename => "5k.png",
+ :content_type => "image/png",
+ :head => "",
+ :path => fixture_file("5k.png")
+ )
+ @subject = Paperclip.io_adapters.for(@file)
+ end
+ should "get the right filename" do
+ assert_equal "5k.png", @subject.original_filename
+ end
+
+ should "get the content type" do
+ assert_equal "image/png", @subject.content_type
+ end
+
+ should "get the file's size" do
+ assert_equal 4456, @subject.size
+ end
+
+ should "return false for a call to nil?" do
+ assert ! @subject.nil?
+ end
+
+ should "generate a MD5 hash of the contents" do
+ expected = Digest::MD5.file(@file.path).to_s
+ assert_equal expected, @subject.fingerprint
+ end
+
+ should "read the contents of the file" do
+ expected = File.new(@file.path).read
+ assert expected.length > 0
+ assert_equal expected, @subject.read
+ end
+ end
+ end
end

0 comments on commit 7d84569

Please sign in to comment.