ryansch-paperclip 2.3.10
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +26 -0
- data/README.md +239 -0
- data/Rakefile +80 -0
- data/generators/paperclip/USAGE +5 -0
- data/generators/paperclip/paperclip_generator.rb +27 -0
- data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/init.rb +1 -0
- data/lib/generators/paperclip/USAGE +8 -0
- data/lib/generators/paperclip/paperclip_generator.rb +31 -0
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/lib/paperclip.rb +384 -0
- data/lib/paperclip/attachment.rb +378 -0
- data/lib/paperclip/callback_compatability.rb +61 -0
- data/lib/paperclip/command_line.rb +86 -0
- data/lib/paperclip/geometry.rb +115 -0
- data/lib/paperclip/interpolations.rb +130 -0
- data/lib/paperclip/iostream.rb +45 -0
- data/lib/paperclip/matchers.rb +33 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +57 -0
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +75 -0
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +54 -0
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +95 -0
- data/lib/paperclip/processor.rb +58 -0
- data/lib/paperclip/railtie.rb +24 -0
- data/lib/paperclip/storage.rb +3 -0
- data/lib/paperclip/storage/filesystem.rb +74 -0
- data/lib/paperclip/storage/fog.rb +98 -0
- data/lib/paperclip/storage/s3.rb +192 -0
- data/lib/paperclip/style.rb +91 -0
- data/lib/paperclip/thumbnail.rb +79 -0
- data/lib/paperclip/upfile.rb +55 -0
- data/lib/paperclip/version.rb +3 -0
- data/lib/tasks/paperclip.rake +72 -0
- data/rails/init.rb +2 -0
- data/shoulda_macros/paperclip.rb +118 -0
- data/test/attachment_test.rb +939 -0
- data/test/command_line_test.rb +138 -0
- data/test/database.yml +4 -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/fixtures/s3.yml +8 -0
- data/test/fixtures/text.txt +0 -0
- data/test/fixtures/twopage.pdf +0 -0
- data/test/fixtures/uppercase.PNG +0 -0
- data/test/fog_test.rb +107 -0
- data/test/geometry_test.rb +177 -0
- data/test/helper.rb +146 -0
- data/test/integration_test.rb +570 -0
- data/test/interpolations_test.rb +143 -0
- data/test/iostream_test.rb +71 -0
- data/test/matchers/have_attached_file_matcher_test.rb +24 -0
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +47 -0
- data/test/matchers/validate_attachment_presence_matcher_test.rb +26 -0
- data/test/matchers/validate_attachment_size_matcher_test.rb +51 -0
- data/test/paperclip_test.rb +306 -0
- data/test/processor_test.rb +10 -0
- data/test/storage_test.rb +416 -0
- data/test/style_test.rb +163 -0
- data/test/thumbnail_test.rb +227 -0
- data/test/upfile_test.rb +36 -0
- metadata +233 -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,416 @@
|
|
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 secure 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
|
+
:s3_permissions => "private",
|
127
|
+
:path => ":attachment/:basename.:extension",
|
128
|
+
:url => ":s3_alias_url"
|
129
|
+
|
130
|
+
rails_env("production")
|
131
|
+
|
132
|
+
@dummy = Dummy.new
|
133
|
+
@dummy.avatar = StringIO.new(".")
|
134
|
+
|
135
|
+
AWS::S3::S3Object.expects(:url_for).with("avatars/stringio.txt", "prod_bucket", { :expires_in => 3600, :use_ssl => true })
|
136
|
+
|
137
|
+
@dummy.avatar.expiring_url
|
138
|
+
end
|
139
|
+
|
140
|
+
should "should succeed" do
|
141
|
+
assert true
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
context "Generating a url with an expiration" do
|
146
|
+
setup do
|
147
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
148
|
+
rebuild_model :storage => :s3,
|
149
|
+
:s3_credentials => {
|
150
|
+
:production => { :bucket => "prod_bucket" },
|
151
|
+
:development => { :bucket => "dev_bucket" }
|
152
|
+
},
|
153
|
+
:s3_host_alias => "something.something.com",
|
154
|
+
:path => ":attachment/:style/:basename.:extension",
|
155
|
+
:url => ":s3_alias_url"
|
156
|
+
|
157
|
+
rails_env("production")
|
158
|
+
|
159
|
+
@dummy = Dummy.new
|
160
|
+
@dummy.avatar = StringIO.new(".")
|
161
|
+
|
162
|
+
AWS::S3::S3Object.expects(:url_for).with("avatars/original/stringio.txt", "prod_bucket", { :expires_in => 3600, :use_ssl => false })
|
163
|
+
@dummy.avatar.expiring_url
|
164
|
+
|
165
|
+
AWS::S3::S3Object.expects(:url_for).with("avatars/thumb/stringio.txt", "prod_bucket", { :expires_in => 1800, :use_ssl => false })
|
166
|
+
@dummy.avatar.expiring_url(1800, :thumb)
|
167
|
+
end
|
168
|
+
|
169
|
+
should "should succeed" do
|
170
|
+
assert true
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
context "Parsing S3 credentials with a bucket in them" do
|
175
|
+
setup do
|
176
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
177
|
+
rebuild_model :storage => :s3,
|
178
|
+
:s3_credentials => {
|
179
|
+
:production => { :bucket => "prod_bucket" },
|
180
|
+
:development => { :bucket => "dev_bucket" }
|
181
|
+
}
|
182
|
+
@dummy = Dummy.new
|
183
|
+
end
|
184
|
+
|
185
|
+
should "get the right bucket in production" do
|
186
|
+
rails_env("production")
|
187
|
+
assert_equal "prod_bucket", @dummy.avatar.bucket_name
|
188
|
+
end
|
189
|
+
|
190
|
+
should "get the right bucket in development" do
|
191
|
+
rails_env("development")
|
192
|
+
assert_equal "dev_bucket", @dummy.avatar.bucket_name
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
context "An attachment with S3 storage" do
|
197
|
+
setup do
|
198
|
+
rebuild_model :storage => :s3,
|
199
|
+
:bucket => "testing",
|
200
|
+
:path => ":attachment/:style/:basename.:extension",
|
201
|
+
:s3_credentials => {
|
202
|
+
'access_key_id' => "12345",
|
203
|
+
'secret_access_key' => "54321"
|
204
|
+
}
|
205
|
+
end
|
206
|
+
|
207
|
+
should "be extended by the S3 module" do
|
208
|
+
assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
|
209
|
+
end
|
210
|
+
|
211
|
+
should "not be extended by the Filesystem module" do
|
212
|
+
assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem)
|
213
|
+
end
|
214
|
+
|
215
|
+
context "when assigned" do
|
216
|
+
setup do
|
217
|
+
@file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
|
218
|
+
@dummy = Dummy.new
|
219
|
+
@dummy.avatar = @file
|
220
|
+
end
|
221
|
+
|
222
|
+
teardown { @file.close }
|
223
|
+
|
224
|
+
should "not get a bucket to get a URL" do
|
225
|
+
@dummy.avatar.expects(:s3).never
|
226
|
+
@dummy.avatar.expects(:s3_bucket).never
|
227
|
+
assert_match %r{^http://s3\.amazonaws\.com/testing/avatars/original/5k\.png}, @dummy.avatar.url
|
228
|
+
end
|
229
|
+
|
230
|
+
context "and saved" do
|
231
|
+
setup do
|
232
|
+
AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path, anything, 'testing', :content_type => 'image/png', :access => :public_read)
|
233
|
+
@dummy.save
|
234
|
+
end
|
235
|
+
|
236
|
+
should "succeed" do
|
237
|
+
assert true
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
context "and saved without a bucket" do
|
242
|
+
setup do
|
243
|
+
class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
|
244
|
+
# Force the class to be created as a proper subclass of ResponseError thanks to AWS::S3's autocreation of exceptions
|
245
|
+
end
|
246
|
+
AWS::S3::Bucket.expects(:create).with("testing")
|
247
|
+
AWS::S3::S3Object.stubs(:store).raises(AWS::S3::NoSuchBucket.new(:message, :response)).then.returns(true)
|
248
|
+
@dummy.save
|
249
|
+
end
|
250
|
+
|
251
|
+
should "succeed" do
|
252
|
+
assert true
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
context "and remove" do
|
257
|
+
setup do
|
258
|
+
AWS::S3::S3Object.stubs(:exists?).returns(true)
|
259
|
+
AWS::S3::S3Object.stubs(:delete)
|
260
|
+
@dummy.destroy_attached_files
|
261
|
+
end
|
262
|
+
|
263
|
+
should "succeed" do
|
264
|
+
assert true
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
context "An attachment with S3 storage and bucket defined as a Proc" do
|
271
|
+
setup do
|
272
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
273
|
+
rebuild_model :storage => :s3,
|
274
|
+
:bucket => lambda { |attachment| "bucket_#{attachment.instance.other}" },
|
275
|
+
:s3_credentials => {:not => :important}
|
276
|
+
end
|
277
|
+
|
278
|
+
should "get the right bucket name" do
|
279
|
+
assert "bucket_a", Dummy.new(:other => 'a').avatar.bucket_name
|
280
|
+
assert "bucket_b", Dummy.new(:other => 'b').avatar.bucket_name
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
context "An attachment with S3 storage and specific s3 headers set" do
|
285
|
+
setup do
|
286
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
287
|
+
rebuild_model :storage => :s3,
|
288
|
+
:bucket => "testing",
|
289
|
+
:path => ":attachment/:style/:basename.:extension",
|
290
|
+
:s3_credentials => {
|
291
|
+
'access_key_id' => "12345",
|
292
|
+
'secret_access_key' => "54321"
|
293
|
+
},
|
294
|
+
:s3_headers => {'Cache-Control' => 'max-age=31557600'}
|
295
|
+
end
|
296
|
+
|
297
|
+
context "when assigned" do
|
298
|
+
setup do
|
299
|
+
@file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
|
300
|
+
@dummy = Dummy.new
|
301
|
+
@dummy.avatar = @file
|
302
|
+
end
|
303
|
+
|
304
|
+
teardown { @file.close }
|
305
|
+
|
306
|
+
context "and saved" do
|
307
|
+
setup do
|
308
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
309
|
+
AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path,
|
310
|
+
anything,
|
311
|
+
'testing',
|
312
|
+
:content_type => 'image/png',
|
313
|
+
:access => :public_read,
|
314
|
+
'Cache-Control' => 'max-age=31557600')
|
315
|
+
@dummy.save
|
316
|
+
end
|
317
|
+
|
318
|
+
should "succeed" do
|
319
|
+
assert true
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
context "with S3 credentials supplied as Pathname" do
|
326
|
+
setup do
|
327
|
+
ENV['S3_KEY'] = 'pathname_key'
|
328
|
+
ENV['S3_BUCKET'] = 'pathname_bucket'
|
329
|
+
ENV['S3_SECRET'] = 'pathname_secret'
|
330
|
+
|
331
|
+
rails_env('test')
|
332
|
+
|
333
|
+
rebuild_model :storage => :s3,
|
334
|
+
:s3_credentials => Pathname.new(File.join(File.dirname(__FILE__))).join("fixtures/s3.yml")
|
335
|
+
|
336
|
+
Dummy.delete_all
|
337
|
+
@dummy = Dummy.new
|
338
|
+
end
|
339
|
+
|
340
|
+
should "parse the credentials" do
|
341
|
+
assert_equal 'pathname_bucket', @dummy.avatar.bucket_name
|
342
|
+
assert_equal 'pathname_key', AWS::S3::Base.connection.options[:access_key_id]
|
343
|
+
assert_equal 'pathname_secret', AWS::S3::Base.connection.options[:secret_access_key]
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
context "with S3 credentials in a YAML file" do
|
348
|
+
setup do
|
349
|
+
ENV['S3_KEY'] = 'env_key'
|
350
|
+
ENV['S3_BUCKET'] = 'env_bucket'
|
351
|
+
ENV['S3_SECRET'] = 'env_secret'
|
352
|
+
|
353
|
+
rails_env('test')
|
354
|
+
|
355
|
+
rebuild_model :storage => :s3,
|
356
|
+
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "fixtures/s3.yml"))
|
357
|
+
|
358
|
+
Dummy.delete_all
|
359
|
+
|
360
|
+
@dummy = Dummy.new
|
361
|
+
end
|
362
|
+
|
363
|
+
should "run it the file through ERB" do
|
364
|
+
assert_equal 'env_bucket', @dummy.avatar.bucket_name
|
365
|
+
assert_equal 'env_key', AWS::S3::Base.connection.options[:access_key_id]
|
366
|
+
assert_equal 'env_secret', AWS::S3::Base.connection.options[:secret_access_key]
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
unless ENV["S3_TEST_BUCKET"].blank?
|
371
|
+
context "Using S3 for real, an attachment with S3 storage" do
|
372
|
+
setup do
|
373
|
+
rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
|
374
|
+
:storage => :s3,
|
375
|
+
:bucket => ENV["S3_TEST_BUCKET"],
|
376
|
+
:path => ":class/:attachment/:id/:style.:extension",
|
377
|
+
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml"))
|
378
|
+
|
379
|
+
Dummy.delete_all
|
380
|
+
@dummy = Dummy.new
|
381
|
+
end
|
382
|
+
|
383
|
+
should "be extended by the S3 module" do
|
384
|
+
assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
|
385
|
+
end
|
386
|
+
|
387
|
+
context "when assigned" do
|
388
|
+
setup do
|
389
|
+
@file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
|
390
|
+
@dummy.avatar = @file
|
391
|
+
end
|
392
|
+
|
393
|
+
teardown { @file.close }
|
394
|
+
|
395
|
+
should "still return a Tempfile when sent #to_file" do
|
396
|
+
assert_equal Paperclip::Tempfile, @dummy.avatar.to_file.class
|
397
|
+
end
|
398
|
+
|
399
|
+
context "and saved" do
|
400
|
+
setup do
|
401
|
+
@dummy.save
|
402
|
+
end
|
403
|
+
|
404
|
+
should "be on S3" do
|
405
|
+
assert true
|
406
|
+
end
|
407
|
+
|
408
|
+
should "generate a tempfile with the right name" do
|
409
|
+
file = @dummy.avatar.to_file
|
410
|
+
assert_match /^original.*\.png$/, File.basename(file.path)
|
411
|
+
end
|
412
|
+
end
|
413
|
+
end
|
414
|
+
end
|
415
|
+
end
|
416
|
+
end
|
data/test/style_test.rb
ADDED
@@ -0,0 +1,163 @@
|
|
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
|
+
|
142
|
+
context "A style rule with :processors supplied as procs" do
|
143
|
+
setup do
|
144
|
+
@attachment = attachment :path => ":basename.:extension",
|
145
|
+
:styles => {
|
146
|
+
:foo => {
|
147
|
+
:geometry => "100x100#",
|
148
|
+
:format => :png,
|
149
|
+
:processors => lambda{|a| [:test]}
|
150
|
+
}
|
151
|
+
},
|
152
|
+
:processors => [:thumbnail]
|
153
|
+
end
|
154
|
+
|
155
|
+
should "defer processing of procs until they are needed" do
|
156
|
+
assert_kind_of Proc, @attachment.styles[:foo].instance_variable_get("@processors")
|
157
|
+
end
|
158
|
+
|
159
|
+
should "call procs when they are needed" do
|
160
|
+
assert_equal [:test], @attachment.styles[:foo].processors
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|