organicinternet-paperclip 2.3.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/LICENSE +26 -0
  2. data/README.md +217 -0
  3. data/Rakefile +80 -0
  4. data/generators/paperclip/USAGE +5 -0
  5. data/generators/paperclip/paperclip_generator.rb +27 -0
  6. data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
  7. data/init.rb +1 -0
  8. data/lib/generators/paperclip/USAGE +8 -0
  9. data/lib/generators/paperclip/paperclip_generator.rb +31 -0
  10. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
  11. data/lib/paperclip.rb +376 -0
  12. data/lib/paperclip/attachment.rb +350 -0
  13. data/lib/paperclip/callback_compatability.rb +61 -0
  14. data/lib/paperclip/command_line.rb +80 -0
  15. data/lib/paperclip/geometry.rb +115 -0
  16. data/lib/paperclip/interpolations.rb +114 -0
  17. data/lib/paperclip/iostream.rb +45 -0
  18. data/lib/paperclip/matchers.rb +33 -0
  19. data/lib/paperclip/matchers/have_attached_file_matcher.rb +57 -0
  20. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +75 -0
  21. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +54 -0
  22. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +95 -0
  23. data/lib/paperclip/processor.rb +58 -0
  24. data/lib/paperclip/railtie.rb +24 -0
  25. data/lib/paperclip/storage.rb +2 -0
  26. data/lib/paperclip/storage/filesystem.rb +73 -0
  27. data/lib/paperclip/storage/s3.rb +192 -0
  28. data/lib/paperclip/style.rb +90 -0
  29. data/lib/paperclip/thumbnail.rb +79 -0
  30. data/lib/paperclip/upfile.rb +55 -0
  31. data/lib/paperclip/version.rb +3 -0
  32. data/lib/tasks/paperclip.rake +72 -0
  33. data/rails/init.rb +2 -0
  34. data/shoulda_macros/paperclip.rb +118 -0
  35. data/test/attachment_test.rb +804 -0
  36. data/test/command_line_test.rb +133 -0
  37. data/test/database.yml +4 -0
  38. data/test/fixtures/12k.png +0 -0
  39. data/test/fixtures/50x50.png +0 -0
  40. data/test/fixtures/5k.png +0 -0
  41. data/test/fixtures/bad.png +1 -0
  42. data/test/fixtures/s3.yml +8 -0
  43. data/test/fixtures/text.txt +0 -0
  44. data/test/fixtures/twopage.pdf +0 -0
  45. data/test/geometry_test.rb +177 -0
  46. data/test/helper.rb +146 -0
  47. data/test/integration_test.rb +498 -0
  48. data/test/interpolations_test.rb +127 -0
  49. data/test/iostream_test.rb +71 -0
  50. data/test/matchers/have_attached_file_matcher_test.rb +24 -0
  51. data/test/matchers/validate_attachment_content_type_matcher_test.rb +47 -0
  52. data/test/matchers/validate_attachment_presence_matcher_test.rb +26 -0
  53. data/test/matchers/validate_attachment_size_matcher_test.rb +51 -0
  54. data/test/paperclip_test.rb +271 -0
  55. data/test/processor_test.rb +10 -0
  56. data/test/storage_test.rb +386 -0
  57. data/test/style_test.rb +141 -0
  58. data/test/thumbnail_test.rb +227 -0
  59. data/test/upfile_test.rb +36 -0
  60. metadata +224 -0
@@ -0,0 +1,10 @@
1
+ require './test/helper'
2
+
3
+ class ProcessorTest < Test::Unit::TestCase
4
+ should "instantiate and call #make when sent #make to the class" do
5
+ processor = mock
6
+ processor.expects(:make).with()
7
+ Paperclip::Processor.expects(:new).with(:one, :two, :three).returns(processor)
8
+ Paperclip::Processor.make(:one, :two, :three)
9
+ end
10
+ end
@@ -0,0 +1,386 @@
1
+ require './test/helper'
2
+ require 'aws/s3'
3
+
4
+ class StorageTest < Test::Unit::TestCase
5
+ def rails_env(env)
6
+ silence_warnings do
7
+ Object.const_set(:Rails, stub('Rails', :env => env))
8
+ end
9
+ end
10
+
11
+ context "filesystem" do
12
+ setup do
13
+ rebuild_model :styles => { :thumbnail => "25x25#" }
14
+ @dummy = Dummy.create!
15
+
16
+ @dummy.avatar = File.open(File.join(File.dirname(__FILE__), "fixtures", "5k.png"))
17
+ end
18
+
19
+ should "allow file assignment" do
20
+ assert @dummy.save
21
+ end
22
+
23
+ should "store the original" do
24
+ @dummy.save
25
+ assert File.exists?(@dummy.avatar.path)
26
+ end
27
+
28
+ should "store the thumbnail" do
29
+ @dummy.save
30
+ assert File.exists?(@dummy.avatar.path(:thumbnail))
31
+ end
32
+ end
33
+
34
+ context "Parsing S3 credentials" do
35
+ setup do
36
+ AWS::S3::Base.stubs(:establish_connection!)
37
+ rebuild_model :storage => :s3,
38
+ :bucket => "testing",
39
+ :s3_credentials => {:not => :important}
40
+
41
+ @dummy = Dummy.new
42
+ @avatar = @dummy.avatar
43
+ end
44
+
45
+ should "get the correct credentials when RAILS_ENV is production" do
46
+ rails_env("production")
47
+ assert_equal({:key => "12345"},
48
+ @avatar.parse_credentials('production' => {:key => '12345'},
49
+ :development => {:key => "54321"}))
50
+ end
51
+
52
+ should "get the correct credentials when RAILS_ENV is development" do
53
+ rails_env("development")
54
+ assert_equal({:key => "54321"},
55
+ @avatar.parse_credentials('production' => {:key => '12345'},
56
+ :development => {:key => "54321"}))
57
+ end
58
+
59
+ should "return the argument if the key does not exist" do
60
+ rails_env("not really an env")
61
+ assert_equal({:test => "12345"}, @avatar.parse_credentials(:test => "12345"))
62
+ end
63
+ end
64
+
65
+ context "" do
66
+ setup do
67
+ AWS::S3::Base.stubs(:establish_connection!)
68
+ rebuild_model :storage => :s3,
69
+ :s3_credentials => {},
70
+ :bucket => "bucket",
71
+ :path => ":attachment/:basename.:extension",
72
+ :url => ":s3_path_url"
73
+ @dummy = Dummy.new
74
+ @dummy.avatar = StringIO.new(".")
75
+ end
76
+
77
+ should "return a url based on an S3 path" do
78
+ assert_match %r{^http://s3.amazonaws.com/bucket/avatars/stringio.txt}, @dummy.avatar.url
79
+ end
80
+ end
81
+ context "" do
82
+ setup do
83
+ AWS::S3::Base.stubs(:establish_connection!)
84
+ rebuild_model :storage => :s3,
85
+ :s3_credentials => {},
86
+ :bucket => "bucket",
87
+ :path => ":attachment/:basename.:extension",
88
+ :url => ":s3_domain_url"
89
+ @dummy = Dummy.new
90
+ @dummy.avatar = StringIO.new(".")
91
+ end
92
+
93
+ should "return a url based on an S3 subdomain" do
94
+ assert_match %r{^http://bucket.s3.amazonaws.com/avatars/stringio.txt}, @dummy.avatar.url
95
+ end
96
+ end
97
+ context "" do
98
+ setup do
99
+ AWS::S3::Base.stubs(:establish_connection!)
100
+ rebuild_model :storage => :s3,
101
+ :s3_credentials => {
102
+ :production => { :bucket => "prod_bucket" },
103
+ :development => { :bucket => "dev_bucket" }
104
+ },
105
+ :s3_host_alias => "something.something.com",
106
+ :path => ":attachment/:basename.:extension",
107
+ :url => ":s3_alias_url"
108
+ @dummy = Dummy.new
109
+ @dummy.avatar = StringIO.new(".")
110
+ end
111
+
112
+ should "return a url based on the host_alias" do
113
+ assert_match %r{^http://something.something.com/avatars/stringio.txt}, @dummy.avatar.url
114
+ end
115
+ end
116
+
117
+ context "Generating a url with an expiration" do
118
+ setup do
119
+ AWS::S3::Base.stubs(:establish_connection!)
120
+ rebuild_model :storage => :s3,
121
+ :s3_credentials => {
122
+ :production => { :bucket => "prod_bucket" },
123
+ :development => { :bucket => "dev_bucket" }
124
+ },
125
+ :s3_host_alias => "something.something.com",
126
+ :path => ":attachment/:basename.:extension",
127
+ :url => ":s3_alias_url"
128
+
129
+ rails_env("production")
130
+
131
+ @dummy = Dummy.new
132
+ @dummy.avatar = StringIO.new(".")
133
+
134
+ AWS::S3::S3Object.expects(:url_for).with("avatars/stringio.txt", "prod_bucket", { :expires_in => 3600 })
135
+
136
+ @dummy.avatar.expiring_url
137
+ end
138
+
139
+ should "should succeed" do
140
+ assert true
141
+ end
142
+ end
143
+
144
+ context "Parsing S3 credentials with a bucket in them" do
145
+ setup do
146
+ AWS::S3::Base.stubs(:establish_connection!)
147
+ rebuild_model :storage => :s3,
148
+ :s3_credentials => {
149
+ :production => { :bucket => "prod_bucket" },
150
+ :development => { :bucket => "dev_bucket" }
151
+ }
152
+ @dummy = Dummy.new
153
+ end
154
+
155
+ should "get the right bucket in production" do
156
+ rails_env("production")
157
+ assert_equal "prod_bucket", @dummy.avatar.bucket_name
158
+ end
159
+
160
+ should "get the right bucket in development" do
161
+ rails_env("development")
162
+ assert_equal "dev_bucket", @dummy.avatar.bucket_name
163
+ end
164
+ end
165
+
166
+ context "An attachment with S3 storage" do
167
+ setup do
168
+ rebuild_model :storage => :s3,
169
+ :bucket => "testing",
170
+ :path => ":attachment/:style/:basename.:extension",
171
+ :s3_credentials => {
172
+ 'access_key_id' => "12345",
173
+ 'secret_access_key' => "54321"
174
+ }
175
+ end
176
+
177
+ should "be extended by the S3 module" do
178
+ assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
179
+ end
180
+
181
+ should "not be extended by the Filesystem module" do
182
+ assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem)
183
+ end
184
+
185
+ context "when assigned" do
186
+ setup do
187
+ @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
188
+ @dummy = Dummy.new
189
+ @dummy.avatar = @file
190
+ end
191
+
192
+ teardown { @file.close }
193
+
194
+ should "not get a bucket to get a URL" do
195
+ @dummy.avatar.expects(:s3).never
196
+ @dummy.avatar.expects(:s3_bucket).never
197
+ assert_match %r{^http://s3\.amazonaws\.com/testing/avatars/original/5k\.png}, @dummy.avatar.url
198
+ end
199
+
200
+ context "and saved" do
201
+ setup do
202
+ AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path, anything, 'testing', :content_type => 'image/png', :access => :public_read)
203
+ @dummy.save
204
+ end
205
+
206
+ should "succeed" do
207
+ assert true
208
+ end
209
+ end
210
+
211
+ context "and saved without a bucket" do
212
+ setup do
213
+ class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
214
+ # Force the class to be created as a proper subclass of ResponseError thanks to AWS::S3's autocreation of exceptions
215
+ end
216
+ AWS::S3::Bucket.expects(:create).with("testing")
217
+ AWS::S3::S3Object.stubs(:store).raises(AWS::S3::NoSuchBucket.new(:message, :response)).then.returns(true)
218
+ @dummy.save
219
+ end
220
+
221
+ should "succeed" do
222
+ assert true
223
+ end
224
+ end
225
+
226
+ context "and remove" do
227
+ setup do
228
+ AWS::S3::S3Object.stubs(:exists?).returns(true)
229
+ AWS::S3::S3Object.stubs(:delete)
230
+ @dummy.destroy_attached_files
231
+ end
232
+
233
+ should "succeed" do
234
+ assert true
235
+ end
236
+ end
237
+ end
238
+ end
239
+
240
+ context "An attachment with S3 storage and bucket defined as a Proc" do
241
+ setup do
242
+ AWS::S3::Base.stubs(:establish_connection!)
243
+ rebuild_model :storage => :s3,
244
+ :bucket => lambda { |attachment| "bucket_#{attachment.instance.other}" },
245
+ :s3_credentials => {:not => :important}
246
+ end
247
+
248
+ should "get the right bucket name" do
249
+ assert "bucket_a", Dummy.new(:other => 'a').avatar.bucket_name
250
+ assert "bucket_b", Dummy.new(:other => 'b').avatar.bucket_name
251
+ end
252
+ end
253
+
254
+ context "An attachment with S3 storage and specific s3 headers set" do
255
+ setup do
256
+ AWS::S3::Base.stubs(:establish_connection!)
257
+ rebuild_model :storage => :s3,
258
+ :bucket => "testing",
259
+ :path => ":attachment/:style/:basename.:extension",
260
+ :s3_credentials => {
261
+ 'access_key_id' => "12345",
262
+ 'secret_access_key' => "54321"
263
+ },
264
+ :s3_headers => {'Cache-Control' => 'max-age=31557600'}
265
+ end
266
+
267
+ context "when assigned" do
268
+ setup do
269
+ @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
270
+ @dummy = Dummy.new
271
+ @dummy.avatar = @file
272
+ end
273
+
274
+ teardown { @file.close }
275
+
276
+ context "and saved" do
277
+ setup do
278
+ AWS::S3::Base.stubs(:establish_connection!)
279
+ AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path,
280
+ anything,
281
+ 'testing',
282
+ :content_type => 'image/png',
283
+ :access => :public_read,
284
+ 'Cache-Control' => 'max-age=31557600')
285
+ @dummy.save
286
+ end
287
+
288
+ should "succeed" do
289
+ assert true
290
+ end
291
+ end
292
+ end
293
+ end
294
+
295
+ context "with S3 credentials supplied as Pathname" do
296
+ setup do
297
+ ENV['S3_KEY'] = 'pathname_key'
298
+ ENV['S3_BUCKET'] = 'pathname_bucket'
299
+ ENV['S3_SECRET'] = 'pathname_secret'
300
+
301
+ rails_env('test')
302
+
303
+ rebuild_model :storage => :s3,
304
+ :s3_credentials => Pathname.new(File.join(File.dirname(__FILE__))).join("fixtures/s3.yml")
305
+
306
+ Dummy.delete_all
307
+ @dummy = Dummy.new
308
+ end
309
+
310
+ should "parse the credentials" do
311
+ assert_equal 'pathname_bucket', @dummy.avatar.bucket_name
312
+ assert_equal 'pathname_key', AWS::S3::Base.connection.options[:access_key_id]
313
+ assert_equal 'pathname_secret', AWS::S3::Base.connection.options[:secret_access_key]
314
+ end
315
+ end
316
+
317
+ context "with S3 credentials in a YAML file" do
318
+ setup do
319
+ ENV['S3_KEY'] = 'env_key'
320
+ ENV['S3_BUCKET'] = 'env_bucket'
321
+ ENV['S3_SECRET'] = 'env_secret'
322
+
323
+ rails_env('test')
324
+
325
+ rebuild_model :storage => :s3,
326
+ :s3_credentials => File.new(File.join(File.dirname(__FILE__), "fixtures/s3.yml"))
327
+
328
+ Dummy.delete_all
329
+
330
+ @dummy = Dummy.new
331
+ end
332
+
333
+ should "run it the file through ERB" do
334
+ assert_equal 'env_bucket', @dummy.avatar.bucket_name
335
+ assert_equal 'env_key', AWS::S3::Base.connection.options[:access_key_id]
336
+ assert_equal 'env_secret', AWS::S3::Base.connection.options[:secret_access_key]
337
+ end
338
+ end
339
+
340
+ unless ENV["S3_TEST_BUCKET"].blank?
341
+ context "Using S3 for real, an attachment with S3 storage" do
342
+ setup do
343
+ rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
344
+ :storage => :s3,
345
+ :bucket => ENV["S3_TEST_BUCKET"],
346
+ :path => ":class/:attachment/:id/:style.:extension",
347
+ :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml"))
348
+
349
+ Dummy.delete_all
350
+ @dummy = Dummy.new
351
+ end
352
+
353
+ should "be extended by the S3 module" do
354
+ assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
355
+ end
356
+
357
+ context "when assigned" do
358
+ setup do
359
+ @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
360
+ @dummy.avatar = @file
361
+ end
362
+
363
+ teardown { @file.close }
364
+
365
+ should "still return a Tempfile when sent #to_file" do
366
+ assert_equal Paperclip::Tempfile, @dummy.avatar.to_file.class
367
+ end
368
+
369
+ context "and saved" do
370
+ setup do
371
+ @dummy.save
372
+ end
373
+
374
+ should "be on S3" do
375
+ assert true
376
+ end
377
+
378
+ should "generate a tempfile with the right name" do
379
+ file = @dummy.avatar.to_file
380
+ assert_match /^original.*\.png$/, File.basename(file.path)
381
+ end
382
+ end
383
+ end
384
+ end
385
+ end
386
+ end
@@ -0,0 +1,141 @@
1
+ # encoding: utf-8
2
+ require './test/helper'
3
+
4
+ class StyleTest < Test::Unit::TestCase
5
+
6
+ context "A style rule" do
7
+ setup do
8
+ @attachment = attachment :path => ":basename.:extension",
9
+ :styles => { :foo => {:geometry => "100x100#", :format => :png} }
10
+ @style = @attachment.styles[:foo]
11
+ end
12
+
13
+ should "be held as a Style object" do
14
+ assert_kind_of Paperclip::Style, @style
15
+ end
16
+
17
+ should "get processors from the attachment definition" do
18
+ assert_equal [:thumbnail], @style.processors
19
+ end
20
+
21
+ should "have the right geometry" do
22
+ assert_equal "100x100#", @style.geometry
23
+ end
24
+
25
+ should "be whiny if the attachment is" do
26
+ @attachment.expects(:whiny).returns(true)
27
+ assert @style.whiny?
28
+ end
29
+
30
+ should "respond to hash notation" do
31
+ assert_equal [:thumbnail], @style[:processors]
32
+ assert_equal "100x100#", @style[:geometry]
33
+ end
34
+ end
35
+
36
+ context "A style rule with properties supplied as procs" do
37
+ setup do
38
+ @attachment = attachment :path => ":basename.:extension",
39
+ :whiny_thumbnails => true,
40
+ :processors => lambda {|a| [:test]},
41
+ :styles => {
42
+ :foo => lambda{|a| "300x300#"},
43
+ :bar => {
44
+ :geometry => lambda{|a| "300x300#"}
45
+ }
46
+ }
47
+ end
48
+
49
+ should "defer processing of procs until they are needed" do
50
+ assert_kind_of Proc, @attachment.styles[:foo].instance_variable_get("@geometry")
51
+ assert_kind_of Proc, @attachment.styles[:bar].instance_variable_get("@geometry")
52
+ assert_kind_of Proc, @attachment.instance_variable_get("@processors")
53
+ end
54
+
55
+ should "call procs when they are needed" do
56
+ assert_equal "300x300#", @attachment.styles[:foo].geometry
57
+ assert_equal "300x300#", @attachment.styles[:bar].geometry
58
+ assert_equal [:test], @attachment.styles[:foo].processors
59
+ assert_equal [:test], @attachment.styles[:bar].processors
60
+ end
61
+ end
62
+
63
+ context "An attachment with style rules in various forms" do
64
+ setup do
65
+ @attachment = attachment :path => ":basename.:extension",
66
+ :styles => {
67
+ :aslist => ["100x100", :png],
68
+ :ashash => {:geometry => "100x100", :format => :png},
69
+ :asstring => "100x100"
70
+ }
71
+ end
72
+ should "have the right number of styles" do
73
+ assert_kind_of Hash, @attachment.styles
74
+ assert_equal 3, @attachment.styles.size
75
+ end
76
+
77
+ should "have styles as Style objects" do
78
+ [:aslist, :ashash, :aslist].each do |s|
79
+ assert_kind_of Paperclip::Style, @attachment.styles[s]
80
+ end
81
+ end
82
+
83
+ should "have the right geometries" do
84
+ [:aslist, :ashash, :aslist].each do |s|
85
+ assert_equal @attachment.styles[s].geometry, "100x100"
86
+ end
87
+ end
88
+
89
+ should "have the right formats" do
90
+ assert_equal @attachment.styles[:aslist].format, :png
91
+ assert_equal @attachment.styles[:ashash].format, :png
92
+ assert_nil @attachment.styles[:asstring].format
93
+ end
94
+
95
+ end
96
+
97
+ context "An attachment with :convert_options" do
98
+ setup do
99
+ @attachment = attachment :path => ":basename.:extension",
100
+ :styles => {:thumb => "100x100", :large => "400x400"},
101
+ :convert_options => {:all => "-do_stuff", :thumb => "-thumbnailize"}
102
+ @style = @attachment.styles[:thumb]
103
+ @file = StringIO.new("...")
104
+ @file.stubs(:original_filename).returns("file.jpg")
105
+ end
106
+
107
+ before_should "not have called extra_options_for(:thumb/:large) on initialization" do
108
+ @attachment.expects(:extra_options_for).never
109
+ end
110
+
111
+ should "call extra_options_for(:thumb/:large) when convert options are requested" do
112
+ @attachment.expects(:extra_options_for).with(:thumb)
113
+ @attachment.styles[:thumb].convert_options
114
+ end
115
+ end
116
+
117
+ context "A style rule with its own :processors" do
118
+ setup do
119
+ @attachment = attachment :path => ":basename.:extension",
120
+ :styles => {
121
+ :foo => {
122
+ :geometry => "100x100#",
123
+ :format => :png,
124
+ :processors => [:test]
125
+ }
126
+ },
127
+ :processors => [:thumbnail]
128
+ @style = @attachment.styles[:foo]
129
+ end
130
+
131
+ should "not get processors from the attachment" do
132
+ @attachment.expects(:processors).never
133
+ assert_not_equal [:thumbnail], @style.processors
134
+ end
135
+
136
+ should "report its own processors" do
137
+ assert_equal [:test], @style.processors
138
+ end
139
+
140
+ end
141
+ end