Skip to content
Browse files

Merge https://github.com/andrewkolesnikov/dm-paperclip

Conflicts:
	dm-paperclip.gemspec
	lib/dm-paperclip/attachment.rb
	lib/dm-paperclip/processor.rb
	lib/dm-paperclip/storage.rb
	test/storage_test.rb
  • Loading branch information...
2 parents 8b87813 + fa4fa07 commit 78c6e144fbd91cea9d3ed6e3133badcb7c11b221 @johnny committed Mar 3, 2012
View
10 Gemfile
@@ -0,0 +1,10 @@
+source :rubygems
+gem 'datamapper'
+gem 'extlib'
+
+group :test do
+ gem 'aws-s3'
+ gem "dm-sqlite-adapter"
+ gem 'shoulda'
+ gem 'mocha'
+end
View
75 Gemfile.lock
@@ -0,0 +1,75 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ addressable (2.2.6)
+ aws-s3 (0.6.2)
+ builder
+ mime-types
+ xml-simple
+ bcrypt-ruby (2.1.4)
+ builder (3.0.0)
+ data_objects (0.10.6)
+ addressable (~> 2.1)
+ datamapper (1.1.0)
+ dm-aggregates (= 1.1.0)
+ dm-constraints (= 1.1.0)
+ dm-core (= 1.1.0)
+ dm-migrations (= 1.1.0)
+ dm-serializer (= 1.1.0)
+ dm-timestamps (= 1.1.0)
+ dm-transactions (= 1.1.0)
+ dm-types (= 1.1.0)
+ dm-validations (= 1.1.0)
+ dm-aggregates (1.1.0)
+ dm-core (~> 1.1.0)
+ dm-constraints (1.1.0)
+ dm-core (~> 1.1.0)
+ dm-core (1.1.0)
+ addressable (~> 2.2.4)
+ dm-do-adapter (1.1.0)
+ data_objects (~> 0.10.2)
+ dm-core (~> 1.1.0)
+ dm-migrations (1.1.0)
+ dm-core (~> 1.1.0)
+ dm-serializer (1.1.0)
+ dm-core (~> 1.1.0)
+ fastercsv (~> 1.5.4)
+ json (~> 1.4.6)
+ dm-sqlite-adapter (1.1.0)
+ dm-do-adapter (~> 1.1.0)
+ do_sqlite3 (~> 0.10.2)
+ dm-timestamps (1.1.0)
+ dm-core (~> 1.1.0)
+ dm-transactions (1.1.0)
+ dm-core (~> 1.1.0)
+ dm-types (1.1.0)
+ bcrypt-ruby (~> 2.1.4)
+ dm-core (~> 1.1.0)
+ fastercsv (~> 1.5.4)
+ json (~> 1.4.6)
+ stringex (~> 1.2.0)
+ uuidtools (~> 2.1.2)
+ dm-validations (1.1.0)
+ dm-core (~> 1.1.0)
+ do_sqlite3 (0.10.6)
+ data_objects (= 0.10.6)
+ extlib (0.9.15)
+ fastercsv (1.5.4)
+ json (1.4.6)
+ mime-types (1.16)
+ mocha (0.9.12)
+ shoulda (2.11.3)
+ stringex (1.2.1)
+ uuidtools (2.1.2)
+ xml-simple (1.1.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ aws-s3
+ datamapper
+ dm-sqlite-adapter
+ extlib
+ mocha
+ shoulda
View
2 dm-paperclip.gemspec
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = %q{dm-paperclip}
- s.version = "2.4.1"
+ s.version = "2.4.2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Ken Robertson"]
View
4 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.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)
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
3 lib/dm-paperclip/processor.rb
@@ -41,7 +41,7 @@ def self.make file, options = {}, attachment = nil
# http://marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions
class Tempfile < ::Tempfile
# This is Ruby 1.8.7's implementation.
- if RUBY_VERSION <= "1.8.6"
+ if RUBY_VERSION <= "1.8.6" || RUBY_PLATFORM =~ /java/
def make_tmpname(basename, n)
case basename
when Array
@@ -55,5 +55,4 @@ def make_tmpname(basename, n)
end
end
end
-
end
View
3 test/attachment_test.rb
@@ -1,5 +1,4 @@
-require 'test/helper'
-
+require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
class Dummy
# This is a dummy class
end
View
2 test/geometry_test.rb
@@ -2,7 +2,7 @@
require 'test/unit'
require 'shoulda'
-require File.join(File.dirname(__FILE__), '..', 'lib', 'dm-paperclip', 'geometry.rb')
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'dm-paperclip', 'geometry.rb'))
class GeometryTest < Test::Unit::TestCase
context "Paperclip::Geometry" do
View
2 test/integration_test.rb
@@ -1,4 +1,4 @@
-require 'test/helper.rb'
+require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
class IntegrationTest < Test::Unit::TestCase
context "Many models at once" do
View
20 test/iostream_test.rb
@@ -1,13 +1,7 @@
-require 'test/helper'
+require File.expand_path(File.join(File.dirname(__FILE__), '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
@@ -75,4 +69,4 @@ class IOStreamTest < Test::Unit::TestCase
end
end
end
-end
+end
View
2 test/paperclip_test.rb
@@ -1,4 +1,4 @@
-require 'test/helper.rb'
+require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
class PaperclipTest < Test::Unit::TestCase
context "A DataMapper model with an 'avatar' attachment" do
View
4 test/storage_test.rb
@@ -1,5 +1,4 @@
-if false
-require 'test/helper'
+require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
require 'aws/s3'
class StorageTest < Test::Unit::TestCase
@@ -323,4 +322,3 @@ class StorageTest < Test::Unit::TestCase
end
end
end
-end
View
4 test/thumbnail_test.rb
@@ -4,8 +4,8 @@
require 'mocha'
require 'tempfile'
-require File.join(File.dirname(__FILE__), '..', 'lib', 'dm-paperclip', 'geometry.rb')
-require File.join(File.dirname(__FILE__), '..', 'lib', 'dm-paperclip', 'thumbnail.rb')
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'dm-paperclip', 'geometry.rb'))
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'dm-paperclip', 'thumbnail.rb'))
class ThumbnailTest < Test::Unit::TestCase

0 comments on commit 78c6e14

Please sign in to comment.
Something went wrong with that request. Please try again.