Permalink
Browse files

Don't monkeypatch IO, Tempfile, StringIO.

For compatibility with Rails > 3.0.0.
(cherry picked from commit 2e6d3370553ef68aeb0deb2b08de3871616139a4)
thoughtbot/paperclip@ef7233d#diff-2
  • Loading branch information...
1 parent 975ba35 commit 022e1b2168e8bbab75d50c9b98b2b0abad6bad9f Andrew Kolesnikov committed Feb 25, 2011
Showing with 17 additions and 36 deletions.
  1. +3 −3 lib/dm-paperclip/attachment.rb
  2. +9 −22 lib/dm-paperclip/iostream.rb
  3. +5 −11 test/iostream_test.rb
View
6 lib/dm-paperclip/attachment.rb
@@ -3,7 +3,7 @@ module Paperclip
# when the model saves, deletes when the model is destroyed, and processes
# the file upon assignment.
class Attachment
-
+ include IOStream
def self.default_options
@default_options ||= {
:url => "/system/:attachment/:id/:style/:filename",
@@ -85,7 +85,7 @@ def assign uploaded_file
instance_write(:file_size, uploaded_file['size'] ? uploaded_file['size'].to_i : uploaded_file['tempfile'].size.to_i)
instance_write(:updated_at, Time.now)
else
- @queued_for_write[:original] = uploaded_file.to_tempfile
+ @queued_for_write[:original] = to_tempfile(uploaded_file)
instance_write(:file_name, uploaded_file.original_filename.strip.gsub(/[^\w\d\.\-]+/, '_'))
instance_write(:content_type, uploaded_file.content_type.to_s.strip)
instance_write(:file_size, uploaded_file.size.to_i)
@@ -412,4 +412,4 @@ def flush_errors #:nodoc:
end
end
end
-end
+end
View
31 lib/dm-paperclip/iostream.rb
@@ -3,46 +3,33 @@
module IOStream
# Returns a Tempfile containing the contents of the readable object.
- def to_tempfile
- name = respond_to?(:original_filename) ? original_filename : (respond_to?(:path) ? path : "stream")
+ def to_tempfile(object)
+ return object.to_tempfile if object.respond_to?(:to_tempfile)
+ name = object.respond_to?(:original_filename) ? object.original_filename : (object.respond_to?(:path) ? object.path : "stream")
tempfile = Paperclip::Tempfile.new(File.basename(name))
tempfile.binmode
- self.stream_to(tempfile)
+ stream_to(object, tempfile)
end
# Copies one read-able object from one place to another in blocks, obviating the need to load
- # the whole thing into memory. Defaults to 8k blocks. If this module is included in both
- # StringIO and Tempfile, then either can have its data copied anywhere else without typing
- # worries or memory overhead worries. Returns a File if a String is passed in as the destination
- # and returns the IO or Tempfile as passed in if one is sent as the destination.
- def stream_to path_or_file, in_blocks_of = 8192
+ # the whole thing into memory. Defaults to 8k blocks. Returns a File if a String is passed
+ # in as the destination and returns the IO or Tempfile as passed in if one is sent as the destination.
+ def stream_to object, path_or_file, in_blocks_of = 8192
dstio = case path_or_file
when String then File.new(path_or_file, "wb+")
when IO then path_or_file
when Tempfile then path_or_file
end
buffer = ""
- self.rewind
- while self.read(in_blocks_of, buffer) do
+ object.rewind
+ while object.read(in_blocks_of, buffer) do
dstio.write(buffer)
end
dstio.rewind
dstio
end
end
-class IO #:nodoc:
- include IOStream
-end
-
-%w( Tempfile StringIO ).each do |klass|
- if Object.const_defined? klass
- Object.const_get(klass).class_eval do
- include IOStream
- end
- end
-end
-
# Corrects a bug in Windows when asking for Tempfile size.
if defined? Tempfile
class Tempfile
View
16 test/iostream_test.rb
@@ -1,13 +1,7 @@
require 'test/helper'
class IOStreamTest < Test::Unit::TestCase
- context "IOStream" do
- should "be included in IO, File, Tempfile, and StringIO" do
- [IO, File, Tempfile, StringIO].each do |klass|
- assert klass.included_modules.include?(IOStream), "Not in #{klass}"
- end
- end
- end
+ include IOStream
context "A file" do
setup do
@@ -21,7 +15,7 @@ class IOStreamTest < Test::Unit::TestCase
context "and given a String" do
setup do
FileUtils.mkdir_p(File.join(ROOT, 'tmp'))
- assert @result = @file.stream_to(File.join(ROOT, 'tmp', 'iostream.string.test'))
+ assert @result = stream_to(@file, File.join(ROOT, 'tmp', 'iostream.string.test'))
end
should "return a File" do
@@ -38,7 +32,7 @@ class IOStreamTest < Test::Unit::TestCase
setup do
tempfile = Tempfile.new('iostream.test')
tempfile.binmode
- assert @result = @file.stream_to(tempfile)
+ assert @result = stream_to(@file, tempfile)
end
should "return a Tempfile" do
@@ -53,9 +47,9 @@ class IOStreamTest < Test::Unit::TestCase
end
- context "that is sent #to_tempfile" do
+ context "that is converted #to_tempfile" do
setup do
- assert @tempfile = @file.to_tempfile
+ assert @tempfile = to_tempfile(@file)
end
should "convert it to a Paperclip Tempfile" do

0 comments on commit 022e1b2

Please sign in to comment.