Skip to content

Commit ef7233d

Browse files
nicksiegerJon Yurek
authored and
Jon Yurek
committed
Don't monkeypatch IO, Tempfile, StringIO.
For compatibility with Rails > 3.0.0. (cherry picked from commit 2e6d337)
1 parent 7a09892 commit ef7233d

File tree

3 files changed

+16
-36
lines changed

3 files changed

+16
-36
lines changed

lib/paperclip/attachment.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Paperclip
44
# when the model saves, deletes when the model is destroyed, and processes
55
# the file upon assignment.
66
class Attachment
7+
include IOStream
78

89
def self.default_options
910
@default_options ||= {
@@ -88,7 +89,7 @@ def assign uploaded_file
8889

8990
return nil if uploaded_file.nil?
9091

91-
@queued_for_write[:original] = uploaded_file.to_tempfile
92+
@queued_for_write[:original] = to_tempfile(uploaded_file)
9293
instance_write(:file_name, uploaded_file.original_filename.strip)
9394
instance_write(:content_type, uploaded_file.content_type.to_s.strip)
9495
instance_write(:file_size, uploaded_file.size.to_i)

lib/paperclip/iostream.rb

+9-23
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,34 @@
11
# Provides method that can be included on File-type objects (IO, StringIO, Tempfile, etc) to allow stream copying
22
# and Tempfile conversion.
33
module IOStream
4-
54
# Returns a Tempfile containing the contents of the readable object.
6-
def to_tempfile
7-
name = respond_to?(:original_filename) ? original_filename : (respond_to?(:path) ? path : "stream")
5+
def to_tempfile(object)
6+
return object.to_tempfile if object.respond_to?(:to_tempfile)
7+
name = object.respond_to?(:original_filename) ? object.original_filename : (object.respond_to?(:path) ? object.path : "stream")
88
tempfile = Paperclip::Tempfile.new(["stream", File.extname(name)])
99
tempfile.binmode
10-
self.stream_to(tempfile)
10+
stream_to(object, tempfile)
1111
end
1212

1313
# Copies one read-able object from one place to another in blocks, obviating the need to load
14-
# the whole thing into memory. Defaults to 8k blocks. If this module is included in both
15-
# StringIO and Tempfile, then either can have its data copied anywhere else without typing
16-
# worries or memory overhead worries. Returns a File if a String is passed in as the destination
17-
# and returns the IO or Tempfile as passed in if one is sent as the destination.
18-
def stream_to path_or_file, in_blocks_of = 8192
14+
# the whole thing into memory. Defaults to 8k blocks. Returns a File if a String is passed
15+
# in as the destination and returns the IO or Tempfile as passed in if one is sent as the destination.
16+
def stream_to object, path_or_file, in_blocks_of = 8192
1917
dstio = case path_or_file
2018
when String then File.new(path_or_file, "wb+")
2119
when IO then path_or_file
2220
when Tempfile then path_or_file
2321
end
2422
buffer = ""
25-
self.rewind
26-
while self.read(in_blocks_of, buffer) do
23+
object.rewind
24+
while object.read(in_blocks_of, buffer) do
2725
dstio.write(buffer)
2826
end
2927
dstio.rewind
3028
dstio
3129
end
3230
end
3331

34-
class IO #:nodoc:
35-
include IOStream
36-
end
37-
38-
%w( Tempfile StringIO ).each do |klass|
39-
if Object.const_defined? klass
40-
Object.const_get(klass).class_eval do
41-
include IOStream
42-
end
43-
end
44-
end
45-
4632
# Corrects a bug in Windows when asking for Tempfile size.
4733
if defined? Tempfile
4834
class Tempfile

test/iostream_test.rb

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
require 'test/helper'
22

33
class IOStreamTest < Test::Unit::TestCase
4-
context "IOStream" do
5-
should "be included in IO, File, Tempfile, and StringIO" do
6-
[IO, File, Tempfile, StringIO].each do |klass|
7-
assert klass.included_modules.include?(IOStream), "Not in #{klass}"
8-
end
9-
end
10-
end
11-
4+
include IOStream
125
context "A file" do
136
setup do
147
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
@@ -21,7 +14,7 @@ class IOStreamTest < Test::Unit::TestCase
2114
context "and given a String" do
2215
setup do
2316
FileUtils.mkdir_p(File.join(ROOT, 'tmp'))
24-
assert @result = @file.stream_to(File.join(ROOT, 'tmp', 'iostream.string.test'))
17+
assert @result = stream_to(@file, File.join(ROOT, 'tmp', 'iostream.string.test'))
2518
end
2619

2720
should "return a File" do
@@ -38,7 +31,7 @@ class IOStreamTest < Test::Unit::TestCase
3831
setup do
3932
tempfile = Tempfile.new('iostream.test')
4033
tempfile.binmode
41-
assert @result = @file.stream_to(tempfile)
34+
assert @result = stream_to(@file, tempfile)
4235
end
4336

4437
should "return a Tempfile" do
@@ -53,9 +46,9 @@ class IOStreamTest < Test::Unit::TestCase
5346

5447
end
5548

56-
context "that is sent #to_tempfile" do
49+
context "that is converted #to_tempfile" do
5750
setup do
58-
assert @tempfile = @file.to_tempfile
51+
assert @tempfile = to_tempfile(@file)
5952
end
6053

6154
should "convert it to a Paperclip Tempfile" do

0 commit comments

Comments
 (0)