paperclip 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of paperclip might be problematic. Click here for more details.
- data/LICENSE +26 -0
- data/README +48 -0
- data/Rakefile +84 -0
- data/generators/paperclip/USAGE +5 -0
- data/generators/paperclip/paperclip_generator.rb +27 -0
- data/generators/paperclip/templates/paperclip_migration.rb +17 -0
- data/init.rb +1 -0
- data/lib/paperclip.rb +209 -0
- data/lib/paperclip/attachment.rb +244 -0
- data/lib/paperclip/geometry.rb +109 -0
- data/lib/paperclip/iostream.rb +43 -0
- data/lib/paperclip/storage.rb +113 -0
- data/lib/paperclip/thumbnail.rb +80 -0
- data/lib/paperclip/upfile.rb +32 -0
- data/tasks/paperclip_tasks.rake +36 -0
- data/test/database.yml +5 -0
- data/test/debug.log +676 -0
- data/test/fixtures/12k.png +0 -0
- data/test/fixtures/50x50.png +0 -0
- data/test/fixtures/5k.png +0 -0
- data/test/fixtures/bad.png +1 -0
- data/test/helper.rb +44 -0
- data/test/s3.yml +2 -0
- data/test/test_attachment.rb +207 -0
- data/test/test_geometry.rb +142 -0
- data/test/test_integration.rb +238 -0
- data/test/test_iostream.rb +60 -0
- data/test/test_paperclip.rb +103 -0
- data/test/test_storage.rb +140 -0
- data/test/test_thumbnail.rb +107 -0
- metadata +93 -0
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'stringio'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'shoulda'
|
6
|
+
|
7
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'iostream.rb')
|
8
|
+
|
9
|
+
class IOStreamTest < Test::Unit::TestCase
|
10
|
+
context "IOStream" do
|
11
|
+
should "be included in IO, File, Tempfile, and StringIO" do
|
12
|
+
[IO, File, Tempfile, StringIO].each do |klass|
|
13
|
+
assert klass.included_modules.include?(IOStream), "Not in #{klass}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "A file" do
|
19
|
+
setup do
|
20
|
+
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"))
|
21
|
+
end
|
22
|
+
|
23
|
+
context "that is sent #stream_to" do
|
24
|
+
|
25
|
+
[["/tmp/iostream.string.test", File],
|
26
|
+
[Tempfile.new('iostream.test'), Tempfile]].each do |args|
|
27
|
+
|
28
|
+
context "and given a #{args[0].class.to_s}" do
|
29
|
+
setup do
|
30
|
+
assert @result = @file.stream_to(args[0])
|
31
|
+
end
|
32
|
+
|
33
|
+
should "return a #{args[1].to_s}" do
|
34
|
+
assert @result.is_a?(args[1])
|
35
|
+
end
|
36
|
+
|
37
|
+
should "contain the same data as the original file" do
|
38
|
+
@file.rewind; @result.rewind
|
39
|
+
assert_equal @file.read, @result.read
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "that is sent #to_tempfile" do
|
46
|
+
setup do
|
47
|
+
assert @tempfile = @file.to_tempfile
|
48
|
+
end
|
49
|
+
|
50
|
+
should "convert it to a Tempfile" do
|
51
|
+
assert @tempfile.is_a?(Tempfile)
|
52
|
+
end
|
53
|
+
|
54
|
+
should "have the Tempfile contain the same data as the file" do
|
55
|
+
@file.rewind; @tempfile.rewind
|
56
|
+
assert_equal @file.read, @tempfile.read
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'test/helper.rb'
|
2
|
+
|
3
|
+
class PaperclipTest < Test::Unit::TestCase
|
4
|
+
context "An ActiveRecord model with an 'avatar' attachment" do
|
5
|
+
setup do
|
6
|
+
rebuild_model :path => "tmp/:class/omg/:style.:extension"
|
7
|
+
@file = File.new(File.join(FIXTURES_DIR, "5k.png"))
|
8
|
+
end
|
9
|
+
|
10
|
+
context "that is attr_protected" do
|
11
|
+
setup do
|
12
|
+
Dummy.class_eval do
|
13
|
+
attr_protected :avatar
|
14
|
+
end
|
15
|
+
@dummy = Dummy.new
|
16
|
+
end
|
17
|
+
|
18
|
+
should "not assign the avatar on mass-set" do
|
19
|
+
@dummy.logger.expects(:debug)
|
20
|
+
|
21
|
+
@dummy.attributes = { :other => "I'm set!",
|
22
|
+
:avatar => @file }
|
23
|
+
|
24
|
+
assert_equal "I'm set!", @dummy.other
|
25
|
+
assert ! @dummy.avatar?
|
26
|
+
end
|
27
|
+
|
28
|
+
should "still allow assigment on normal set" do
|
29
|
+
@dummy.logger.expects(:debug).times(0)
|
30
|
+
|
31
|
+
@dummy.other = "I'm set!"
|
32
|
+
@dummy.avatar = @file
|
33
|
+
|
34
|
+
assert_equal "I'm set!", @dummy.other
|
35
|
+
assert @dummy.avatar?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "with a subclass" do
|
40
|
+
setup do
|
41
|
+
class ::SubDummy < Dummy; end
|
42
|
+
end
|
43
|
+
|
44
|
+
should "be able to use the attachment from the subclass" do
|
45
|
+
assert_nothing_raised do
|
46
|
+
@subdummy = SubDummy.create(:avatar => @file)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
should "be able to see the attachment definition from the subclass's class" do
|
51
|
+
assert_equal "tmp/:class/omg/:style.:extension", SubDummy.attachment_definitions[:avatar][:path]
|
52
|
+
end
|
53
|
+
|
54
|
+
teardown do
|
55
|
+
Object.send(:remove_const, "SubDummy") rescue nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
should "have an #avatar method" do
|
60
|
+
assert Dummy.new.respond_to?(:avatar)
|
61
|
+
end
|
62
|
+
|
63
|
+
should "have an #avatar= method" do
|
64
|
+
assert Dummy.new.respond_to?(:avatar=)
|
65
|
+
end
|
66
|
+
|
67
|
+
[[:presence, nil, "5k.png", nil],
|
68
|
+
[:size, {:in => 1..10240}, "5k.png", "12k.png"]].each do |args|
|
69
|
+
context "with #{args[0]} validations" do
|
70
|
+
setup do
|
71
|
+
Dummy.class_eval do
|
72
|
+
send(*[:"validates_attachment_#{args[0]}", :avatar, args[1]].compact)
|
73
|
+
end
|
74
|
+
@dummy = Dummy.new
|
75
|
+
end
|
76
|
+
|
77
|
+
context "and a valid file" do
|
78
|
+
setup do
|
79
|
+
@file = args[2] && File.new(File.join(FIXTURES_DIR, args[2]))
|
80
|
+
end
|
81
|
+
|
82
|
+
should "not have any errors" do
|
83
|
+
@dummy.avatar = @file
|
84
|
+
assert @dummy.avatar.valid?
|
85
|
+
assert_equal 0, @dummy.avatar.errors.length
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "and an invalid file" do
|
90
|
+
setup do
|
91
|
+
@file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
|
92
|
+
end
|
93
|
+
|
94
|
+
should "have errors" do
|
95
|
+
@dummy.avatar = @file
|
96
|
+
assert ! @dummy.avatar.valid?
|
97
|
+
assert_equal 1, @dummy.avatar.errors.length
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'right_aws'
|
5
|
+
|
6
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'geometry.rb')
|
7
|
+
|
8
|
+
class S3Test < Test::Unit::TestCase
|
9
|
+
context "Parsing S3 credentials" do
|
10
|
+
setup do
|
11
|
+
rebuild_model :storage => :s3,
|
12
|
+
:bucket => "testing",
|
13
|
+
:s3_credentials => {:not => :important}
|
14
|
+
|
15
|
+
@s3_stub = stub
|
16
|
+
@bucket_stub = stub
|
17
|
+
RightAws::S3.expects(:new).returns(@s3_stub)
|
18
|
+
@s3_stub.expects(:bucket).returns(@bucket_stub)
|
19
|
+
@dummy = Dummy.new
|
20
|
+
@avatar = @dummy.avatar
|
21
|
+
|
22
|
+
@current_env = ENV['RAILS_ENV']
|
23
|
+
end
|
24
|
+
|
25
|
+
teardown do
|
26
|
+
ENV['RAILS_ENV'] = @current_env
|
27
|
+
end
|
28
|
+
|
29
|
+
should "get the correct credentials when RAILS_ENV is production" do
|
30
|
+
ENV['RAILS_ENV'] = 'production'
|
31
|
+
assert_equal({:key => "12345"},
|
32
|
+
@avatar.parse_credentials('production' => {:key => '12345'},
|
33
|
+
:development => {:key => "54321"}))
|
34
|
+
end
|
35
|
+
|
36
|
+
should "get the correct credentials when RAILS_ENV is development" do
|
37
|
+
ENV['RAILS_ENV'] = 'development'
|
38
|
+
assert_equal({:key => "54321"},
|
39
|
+
@avatar.parse_credentials('production' => {:key => '12345'},
|
40
|
+
:development => {:key => "54321"}))
|
41
|
+
end
|
42
|
+
|
43
|
+
should "return the argument if the key does not exist" do
|
44
|
+
ENV['RAILS_ENV'] = "not really an env"
|
45
|
+
assert_equal({:test => "12345"}, @avatar.parse_credentials(:test => "12345"))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "An attachment with S3 storage" do
|
50
|
+
setup do
|
51
|
+
rebuild_model :storage => :s3,
|
52
|
+
:bucket => "testing",
|
53
|
+
:s3_credentials => {
|
54
|
+
'access_key_id' => "12345",
|
55
|
+
'secret_access_key' => "54321"
|
56
|
+
}
|
57
|
+
|
58
|
+
@s3_mock = stub
|
59
|
+
@bucket_mock = stub
|
60
|
+
RightAws::S3.expects(:new).
|
61
|
+
with("12345", "54321", {}).
|
62
|
+
returns(@s3_mock)
|
63
|
+
@s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock)
|
64
|
+
end
|
65
|
+
|
66
|
+
should "be extended by the S3 module" do
|
67
|
+
assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
|
68
|
+
end
|
69
|
+
|
70
|
+
should "not be extended by the Filesystem module" do
|
71
|
+
assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem)
|
72
|
+
end
|
73
|
+
|
74
|
+
context "when assigned" do
|
75
|
+
setup do
|
76
|
+
@file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'))
|
77
|
+
@dummy = Dummy.new
|
78
|
+
@dummy.avatar = @file
|
79
|
+
end
|
80
|
+
|
81
|
+
should "still return a Tempfile when sent #to_io" do
|
82
|
+
assert_equal Tempfile, @dummy.avatar.to_io.class
|
83
|
+
end
|
84
|
+
|
85
|
+
context "and saved" do
|
86
|
+
setup do
|
87
|
+
@key_mock = stub
|
88
|
+
@bucket_mock.expects(:key).returns(@key_mock)
|
89
|
+
@key_mock.expects(:data=)
|
90
|
+
@key_mock.expects(:put)
|
91
|
+
@dummy.save
|
92
|
+
end
|
93
|
+
|
94
|
+
should "succeed" do
|
95
|
+
assert true
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
unless ENV["S3_TEST_BUCKET"].blank?
|
102
|
+
context "Using S3 for real, an attachment with S3 storage" do
|
103
|
+
setup do
|
104
|
+
rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
|
105
|
+
:storage => :s3,
|
106
|
+
:bucket => ENV["S3_TEST_BUCKET"],
|
107
|
+
:path => ":class/:attachment/:id/:style.:extension",
|
108
|
+
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml"))
|
109
|
+
|
110
|
+
Dummy.delete_all
|
111
|
+
@dummy = Dummy.new
|
112
|
+
end
|
113
|
+
|
114
|
+
should "be extended by the S3 module" do
|
115
|
+
assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
|
116
|
+
end
|
117
|
+
|
118
|
+
context "when assigned" do
|
119
|
+
setup do
|
120
|
+
@file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'))
|
121
|
+
@dummy.avatar = @file
|
122
|
+
end
|
123
|
+
|
124
|
+
should "still return a Tempfile when sent #to_io" do
|
125
|
+
assert_equal Tempfile, @dummy.avatar.to_io.class
|
126
|
+
end
|
127
|
+
|
128
|
+
context "and saved" do
|
129
|
+
setup do
|
130
|
+
@dummy.save
|
131
|
+
end
|
132
|
+
|
133
|
+
should "be on S3" do
|
134
|
+
assert true
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'mocha'
|
5
|
+
require 'tempfile'
|
6
|
+
|
7
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'geometry.rb')
|
8
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'thumbnail.rb')
|
9
|
+
|
10
|
+
class ThumbnailTest < Test::Unit::TestCase
|
11
|
+
|
12
|
+
context "A Paperclip Tempfile" do
|
13
|
+
setup do
|
14
|
+
@tempfile = Paperclip::Tempfile.new("file.jpg")
|
15
|
+
end
|
16
|
+
|
17
|
+
should "have its path contain a real extension" do
|
18
|
+
assert_equal ".jpg", File.extname(@tempfile.path)
|
19
|
+
end
|
20
|
+
|
21
|
+
should "be a real Tempfile" do
|
22
|
+
assert @tempfile.is_a?(::Tempfile)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "Another Paperclip Tempfile" do
|
27
|
+
setup do
|
28
|
+
@tempfile = Paperclip::Tempfile.new("file")
|
29
|
+
end
|
30
|
+
|
31
|
+
should "not have an extension if not given one" do
|
32
|
+
assert_equal "", File.extname(@tempfile.path)
|
33
|
+
end
|
34
|
+
|
35
|
+
should "still be a real Tempfile" do
|
36
|
+
assert @tempfile.is_a?(::Tempfile)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "An image" do
|
41
|
+
setup do
|
42
|
+
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"))
|
43
|
+
end
|
44
|
+
|
45
|
+
[["600x600>", "434x66"],
|
46
|
+
["400x400>", "400x61"],
|
47
|
+
["32x32<", "434x66"]
|
48
|
+
].each do |args|
|
49
|
+
context "being thumbnailed with a geometry of #{args[0]}" do
|
50
|
+
setup do
|
51
|
+
@thumb = Paperclip::Thumbnail.new(@file, args[0])
|
52
|
+
end
|
53
|
+
|
54
|
+
should "start with dimensions of 434x66" do
|
55
|
+
cmd = %Q[identify -format "%wx%h" #{@file.path}]
|
56
|
+
assert_equal "434x66", `#{cmd}`.chomp
|
57
|
+
end
|
58
|
+
|
59
|
+
should "report the correct target geometry" do
|
60
|
+
assert_equal args[0], @thumb.target_geometry.to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
context "when made" do
|
64
|
+
setup do
|
65
|
+
@thumb_result = @thumb.make
|
66
|
+
end
|
67
|
+
|
68
|
+
should "be the size we expect it to be" do
|
69
|
+
cmd = %Q[identify -format "%wx%h" #{@thumb_result.path}]
|
70
|
+
assert_equal args[1], `#{cmd}`.chomp
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "being thumbnailed at 100x50 with cropping" do
|
77
|
+
setup do
|
78
|
+
@thumb = Paperclip::Thumbnail.new(@file, "100x50#")
|
79
|
+
end
|
80
|
+
|
81
|
+
should "report its correct current and target geometries" do
|
82
|
+
assert_equal "100x50#", @thumb.target_geometry.to_s
|
83
|
+
assert_equal "434x66", @thumb.current_geometry.to_s
|
84
|
+
end
|
85
|
+
|
86
|
+
should "report its correct format" do
|
87
|
+
assert_nil @thumb.format
|
88
|
+
end
|
89
|
+
|
90
|
+
should "have whiny_thumbnails turned on by default" do
|
91
|
+
assert @thumb.whiny_thumbnails
|
92
|
+
end
|
93
|
+
|
94
|
+
should "send the right command to convert when sent #make" do
|
95
|
+
@thumb.expects(:system).with do |arg|
|
96
|
+
arg.match %r{convert\s+"#{File.expand_path(@thumb.file.path)}"\s+-scale\s+\"x50\"\s+-crop\s+\"100x50\+114\+0\"\s+\+repage\s+".*?"}
|
97
|
+
end
|
98
|
+
@thumb.make
|
99
|
+
end
|
100
|
+
|
101
|
+
should "create the thumbnail when sent #make" do
|
102
|
+
dst = @thumb.make
|
103
|
+
assert_match /100x50/, `identify #{dst.path}`
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paperclip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jon Yurek
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-04-18 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: jyurek@thoughtbot.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- README
|
26
|
+
- LICENSE
|
27
|
+
- Rakefile
|
28
|
+
- init.rb
|
29
|
+
- generators/paperclip
|
30
|
+
- generators/paperclip/paperclip_generator.rb
|
31
|
+
- generators/paperclip/templates
|
32
|
+
- generators/paperclip/templates/paperclip_migration.rb
|
33
|
+
- generators/paperclip/USAGE
|
34
|
+
- lib/paperclip
|
35
|
+
- lib/paperclip/attachment.rb
|
36
|
+
- lib/paperclip/geometry.rb
|
37
|
+
- lib/paperclip/iostream.rb
|
38
|
+
- lib/paperclip/storage.rb
|
39
|
+
- lib/paperclip/thumbnail.rb
|
40
|
+
- lib/paperclip/upfile.rb
|
41
|
+
- lib/paperclip.rb
|
42
|
+
- tasks/paperclip_tasks.rake
|
43
|
+
- test/database.yml
|
44
|
+
- test/debug.log
|
45
|
+
- test/fixtures
|
46
|
+
- test/fixtures/12k.png
|
47
|
+
- test/fixtures/50x50.png
|
48
|
+
- test/fixtures/5k.png
|
49
|
+
- test/fixtures/bad.png
|
50
|
+
- test/helper.rb
|
51
|
+
- test/s3.yml
|
52
|
+
- test/test_attachment.rb
|
53
|
+
- test/test_geometry.rb
|
54
|
+
- test/test_integration.rb
|
55
|
+
- test/test_iostream.rb
|
56
|
+
- test/test_paperclip.rb
|
57
|
+
- test/test_storage.rb
|
58
|
+
- test/test_thumbnail.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://www.thoughtbot.com/
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --line-numbers
|
64
|
+
- --inline-source
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
version:
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
version:
|
79
|
+
requirements:
|
80
|
+
- ImageMagick
|
81
|
+
rubyforge_project: paperclip
|
82
|
+
rubygems_version: 1.0.1
|
83
|
+
signing_key:
|
84
|
+
specification_version: 2
|
85
|
+
summary: File attachments as attributes for ActiveRecord
|
86
|
+
test_files:
|
87
|
+
- test/test_attachment.rb
|
88
|
+
- test/test_geometry.rb
|
89
|
+
- test/test_integration.rb
|
90
|
+
- test/test_iostream.rb
|
91
|
+
- test/test_paperclip.rb
|
92
|
+
- test/test_storage.rb
|
93
|
+
- test/test_thumbnail.rb
|