jnicklas-carrierwave 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/README.rdoc +35 -20
  2. data/Rakefile +1 -1
  3. data/lib/carrierwave/compatibility/paperclip.rb +91 -0
  4. data/lib/carrierwave/core_ext/inheritable_attributes.rb +102 -0
  5. data/lib/carrierwave/core_ext/module_setup.rb +49 -0
  6. data/lib/carrierwave/mount.rb +119 -103
  7. data/lib/carrierwave/orm/activerecord.rb +6 -1
  8. data/lib/carrierwave/orm/sequel.rb +15 -2
  9. data/lib/carrierwave/processing/rmagick.rb +8 -7
  10. data/lib/carrierwave/storage/abstract.rb +16 -1
  11. data/lib/carrierwave/storage/file.rb +20 -1
  12. data/lib/carrierwave/uploader/cache.rb +114 -0
  13. data/lib/carrierwave/uploader/callbacks.rb +40 -0
  14. data/lib/carrierwave/uploader/default_path.rb +21 -0
  15. data/lib/carrierwave/uploader/extension_whitelist.rb +35 -0
  16. data/lib/carrierwave/uploader/mountable.rb +37 -0
  17. data/lib/carrierwave/uploader/paths.rb +25 -0
  18. data/lib/carrierwave/uploader/processing.rb +79 -0
  19. data/lib/carrierwave/uploader/proxy.rb +60 -0
  20. data/lib/carrierwave/uploader/remove.rb +21 -0
  21. data/lib/carrierwave/uploader/store.rb +154 -0
  22. data/lib/carrierwave/uploader/url.rb +22 -0
  23. data/lib/carrierwave/uploader/versions.rb +145 -0
  24. data/lib/carrierwave/uploader.rb +31 -593
  25. data/lib/carrierwave.rb +55 -7
  26. data/lib/generators/uploader_generator.rb +1 -1
  27. data/rails_generators/uploader/templates/uploader.rb +12 -8
  28. data/spec/compatibility/paperclip_spec.rb +41 -0
  29. data/spec/mount_spec.rb +88 -25
  30. data/spec/orm/activerecord_spec.rb +7 -9
  31. data/spec/orm/datamapper_spec.rb +7 -9
  32. data/spec/orm/sequel_spec.rb +47 -32
  33. data/spec/spec_helper.rb +13 -0
  34. data/spec/uploader/cache_spec.rb +194 -0
  35. data/spec/uploader/default_path_spec.rb +66 -0
  36. data/spec/uploader/extension_whitelist_spec.rb +42 -0
  37. data/spec/uploader/mountable_spec.rb +31 -0
  38. data/spec/uploader/paths_spec.rb +20 -0
  39. data/spec/uploader/processing_spec.rb +60 -0
  40. data/spec/uploader/proxy_spec.rb +52 -0
  41. data/spec/uploader/remove_spec.rb +65 -0
  42. data/spec/uploader/store_spec.rb +260 -0
  43. data/spec/uploader/url_spec.rb +85 -0
  44. data/spec/uploader/versions_spec.rb +275 -0
  45. metadata +34 -3
  46. data/spec/uploader_spec.rb +0 -887
@@ -1,887 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- describe CarrierWave::Uploader do
4
-
5
- before do
6
- @uploader_class = Class.new do
7
- include CarrierWave::Uploader
8
- end
9
- @uploader = @uploader_class.new
10
- end
11
-
12
- after do
13
- FileUtils.rm_rf(public_path)
14
- end
15
-
16
- describe '.version' do
17
- it "should add it to .versions" do
18
- @uploader_class.version :thumb
19
- @uploader_class.versions[:thumb].should be_a(Class)
20
- @uploader_class.versions[:thumb].ancestors.should include(@uploader_class)
21
- end
22
-
23
- it "should add an accessor which returns the version" do
24
- @uploader_class.version :thumb
25
- @uploader.thumb.should be_a(@uploader_class)
26
- end
27
-
28
- it "should add it to #versions which returns the version" do
29
- @uploader_class.version :thumb
30
- @uploader.versions[:thumb].should be_a(@uploader_class)
31
- end
32
-
33
- it "should set the version name" do
34
- @uploader_class.version :thumb
35
- @uploader.version_name.should == nil
36
- @uploader.thumb.version_name.should == :thumb
37
- end
38
-
39
- it "should set the version names on the class" do
40
- @uploader_class.version :thumb
41
- @uploader.class.version_names.should == []
42
- @uploader.thumb.class.version_names.should == [:thumb]
43
- end
44
-
45
- it "should remember mount options" do
46
- model = mock('a model')
47
- @uploader_class.version :thumb
48
- @uploader = @uploader_class.new(model, :gazelle)
49
-
50
- @uploader.thumb.model.should == model
51
- @uploader.thumb.mounted_as.should == :gazelle
52
- end
53
-
54
- it "should apply any overrides given in a block" do
55
- @uploader_class.version :thumb do
56
- def store_dir
57
- public_path('monkey/apache')
58
- end
59
- end
60
- @uploader.store_dir.should == 'uploads'
61
- @uploader.thumb.store_dir.should == public_path('monkey/apache')
62
- end
63
-
64
- it "should reopen the same class when called multiple times" do
65
- @uploader_class.version :thumb do
66
- def self.monkey
67
- "monkey"
68
- end
69
- end
70
- @uploader_class.version :thumb do
71
- def self.llama
72
- "llama"
73
- end
74
- end
75
- @uploader_class.version(:thumb).monkey.should == "monkey"
76
- @uploader_class.version(:thumb).llama.should == "llama"
77
- end
78
-
79
- describe 'with nested versions' do
80
- before do
81
- @uploader_class.version :thumb do
82
- version :mini
83
- version :micro
84
- end
85
- end
86
-
87
- it "should add an array of version names" do
88
- @uploader.class.version_names.should == []
89
- @uploader.thumb.class.version_names.should == [:thumb]
90
- @uploader.thumb.mini.class.version_names.should == [:thumb, :mini]
91
- @uploader.thumb.micro.class.version_names.should == [:thumb, :micro]
92
- end
93
-
94
- it "should set the version name for the instances" do
95
- @uploader.version_name.should be_nil
96
- @uploader.thumb.version_name.should == :thumb
97
- @uploader.thumb.mini.version_name.should == :thumb_mini
98
- @uploader.thumb.micro.version_name.should == :thumb_micro
99
- end
100
-
101
- end
102
-
103
- end
104
-
105
- describe '.process' do
106
- it "should add a single processor when a symbol is given" do
107
- @uploader_class.process :sepiatone
108
- @uploader.should_receive(:sepiatone)
109
- @uploader.process!
110
- end
111
-
112
- it "should add multiple processors when an array of symbols is given" do
113
- @uploader_class.process :sepiatone, :desaturate, :invert
114
- @uploader.should_receive(:sepiatone)
115
- @uploader.should_receive(:desaturate)
116
- @uploader.should_receive(:invert)
117
- @uploader.process!
118
- end
119
-
120
- it "should add a single processor with an argument when a hash is given" do
121
- @uploader_class.process :format => 'png'
122
- @uploader.should_receive(:format).with('png')
123
- @uploader.process!
124
- end
125
-
126
- it "should add a single processor with several argument when a hash is given" do
127
- @uploader_class.process :resize => [200, 300]
128
- @uploader.should_receive(:resize).with(200, 300)
129
- @uploader.process!
130
- end
131
-
132
- it "should add multiple processors when an hash with multiple keys is given" do
133
- @uploader_class.process :resize => [200, 300], :format => 'png'
134
- @uploader.should_receive(:resize).with(200, 300)
135
- @uploader.should_receive(:format).with('png')
136
- @uploader.process!
137
- end
138
- end
139
-
140
- describe ".storage" do
141
- before do
142
- CarrierWave::Storage::File.stub!(:setup!)
143
- CarrierWave::Storage::S3.stub!(:setup!)
144
- end
145
-
146
- it "should set the storage if an argument is given" do
147
- storage = mock('some kind of storage')
148
- storage.should_receive(:setup!)
149
- @uploader_class.storage storage
150
- @uploader_class.storage.should == storage
151
- end
152
-
153
- it "should default to file" do
154
- @uploader_class.storage.should == CarrierWave::Storage::File
155
- end
156
-
157
- it "should set the storage from the configured shortcuts if a symbol is given" do
158
- @uploader_class.storage :file
159
- @uploader_class.storage.should == CarrierWave::Storage::File
160
- end
161
-
162
- it "should remember the storage when inherited" do
163
- @uploader_class.storage :s3
164
- subclass = Class.new(@uploader_class)
165
- subclass.storage.should == CarrierWave::Storage::S3
166
- end
167
-
168
- it "should be changeable when inherited" do
169
- @uploader_class.storage :s3
170
- subclass = Class.new(@uploader_class)
171
- subclass.storage.should == CarrierWave::Storage::S3
172
- subclass.storage :file
173
- subclass.storage.should == CarrierWave::Storage::File
174
- end
175
- end
176
-
177
- describe '#blank?' do
178
- it "should be true when nothing has been done" do
179
- @uploader.should be_blank
180
- end
181
-
182
- it "should not be true when the file is empty" do
183
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
184
- @uploader.should be_blank
185
- end
186
-
187
- it "should not be true when a file has been cached" do
188
- @uploader.cache!(File.open(file_path('test.jpg')))
189
- @uploader.should_not be_blank
190
- end
191
- end
192
-
193
- describe '#read' do
194
- it "should be nil by default" do
195
- @uploader.read.should be_nil
196
- end
197
-
198
- it "should read the contents of a cached file" do
199
- @uploader.cache!(File.open(file_path('test.jpg')))
200
- @uploader.read.should == "this is stuff"
201
- end
202
- end
203
-
204
- describe '#size' do
205
- it "should be zero by default" do
206
- @uploader.size.should == 0
207
- end
208
-
209
- it "should get the size of a cached file" do
210
- @uploader.cache!(File.open(file_path('test.jpg')))
211
- @uploader.size.should == 13
212
- end
213
- end
214
-
215
- describe '#store_dir' do
216
- it "should default to the config option" do
217
- @uploader.store_dir.should == 'uploads'
218
- end
219
- end
220
-
221
- describe '#cache_dir' do
222
- it "should default to the config option" do
223
- @uploader.cache_dir.should == 'uploads/tmp'
224
- end
225
- end
226
-
227
- describe '#root' do
228
- it "should default to the config option" do
229
- @uploader.root.should == public_path('..')
230
- end
231
- end
232
-
233
- describe '#filename' do
234
- it "should default to nil" do
235
- @uploader.filename.should be_nil
236
- end
237
- end
238
-
239
- describe '#model' do
240
- it "should be remembered from initialization" do
241
- model = mock('a model object')
242
- @uploader = @uploader_class.new(model)
243
- @uploader.model.should == model
244
- end
245
- end
246
-
247
- describe '#mounted_as' do
248
- it "should be remembered from initialization" do
249
- model = mock('a model object')
250
- @uploader = @uploader_class.new(model, :llama)
251
- @uploader.model.should == model
252
- @uploader.mounted_as.should == :llama
253
- end
254
- end
255
-
256
- describe '#url' do
257
- before do
258
- CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
259
- end
260
-
261
- it "should default to nil" do
262
- @uploader.url.should be_nil
263
- end
264
-
265
- it "should get the directory relative to public, prepending a slash" do
266
- @uploader.cache!(File.open(file_path('test.jpg')))
267
- @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
268
- end
269
-
270
- it "should get the directory relative to public for a specific version" do
271
- @uploader_class.version(:thumb)
272
- @uploader.cache!(File.open(file_path('test.jpg')))
273
- @uploader.url(:thumb).should == '/uploads/tmp/20071201-1234-345-2255/thumb_test.jpg'
274
- end
275
-
276
- it "should get the directory relative to public for a nested version" do
277
- @uploader_class.version(:thumb) do
278
- version(:mini)
279
- end
280
- @uploader.cache!(File.open(file_path('test.jpg')))
281
- @uploader.url(:thumb, :mini).should == '/uploads/tmp/20071201-1234-345-2255/thumb_mini_test.jpg'
282
- end
283
-
284
- it "should return file#url if available" do
285
- @uploader.cache!(File.open(file_path('test.jpg')))
286
- @uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
287
- @uploader.url.should == 'http://www.example.com/someurl.jpg'
288
- end
289
-
290
- it "should get the directory relative to public, if file#url is blank" do
291
- @uploader.cache!(File.open(file_path('test.jpg')))
292
- @uploader.file.stub!(:url).and_return('')
293
- @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
294
- end
295
- end
296
-
297
- describe '#to_s' do
298
- before do
299
- CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
300
- end
301
-
302
- it "should default to nil" do
303
- @uploader.to_s.should be_nil
304
- end
305
-
306
- it "should get the directory relative to public, prepending a slash" do
307
- @uploader.cache!(File.open(file_path('test.jpg')))
308
- @uploader.to_s.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
309
- end
310
-
311
- it "should return file#url if available" do
312
- @uploader.cache!(File.open(file_path('test.jpg')))
313
- @uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
314
- @uploader.to_s.should == 'http://www.example.com/someurl.jpg'
315
- end
316
- end
317
-
318
- describe '#cache!' do
319
-
320
- before do
321
- CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
322
- end
323
-
324
- it "should cache a file" do
325
- @uploader.cache!(File.open(file_path('test.jpg')))
326
- @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
327
- end
328
-
329
- it "should be cached" do
330
- @uploader.cache!(File.open(file_path('test.jpg')))
331
- @uploader.should be_cached
332
- end
333
-
334
- it "should store the cache name" do
335
- @uploader.cache!(File.open(file_path('test.jpg')))
336
- @uploader.cache_name.should == '20071201-1234-345-2255/test.jpg'
337
- end
338
-
339
- it "should set the filename to the file's sanitized filename" do
340
- @uploader.cache!(File.open(file_path('test.jpg')))
341
- @uploader.filename.should == 'test.jpg'
342
- end
343
-
344
- it "should move it to the tmp dir" do
345
- @uploader.cache!(File.open(file_path('test.jpg')))
346
- @uploader.file.path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
347
- @uploader.file.exists?.should be_true
348
- end
349
-
350
- it "should not move it if cache_to_cache_dir is false" do
351
- CarrierWave.config[:cache_to_cache_dir] = false
352
- path = file_path('test.jpg')
353
- @uploader.cache!(File.open(path))
354
- @uploader.current_path.should == path
355
- @uploader.file.exists?.should be_true
356
- CarrierWave.config[:cache_to_cache_dir] = true
357
- end
358
-
359
- it "should set the url" do
360
- @uploader.cache!(File.open(file_path('test.jpg')))
361
- @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
362
- end
363
-
364
- it "should trigger a process!" do
365
- @uploader.should_receive(:process!)
366
- @uploader.cache!(File.open(file_path('test.jpg')))
367
- end
368
-
369
- it "should raise an error when trying to cache a string" do
370
- running {
371
- @uploader.cache!(file_path('test.jpg'))
372
- }.should raise_error(CarrierWave::FormNotMultipart)
373
- end
374
-
375
- it "should raise an error when trying to cache a pathname" do
376
- running {
377
- @uploader.cache!(Pathname.new(file_path('test.jpg')))
378
- }.should raise_error(CarrierWave::FormNotMultipart)
379
- end
380
-
381
- it "should do nothing when trying to cache an empty file" do
382
- @uploader.cache!(nil)
383
- end
384
-
385
- it "should set permissions if options are given" do
386
- old_permissions = CarrierWave.config[:permissions]
387
- CarrierWave.config[:permissions] = 0777
388
-
389
- @uploader.cache!(File.open(file_path('test.jpg')))
390
- @uploader.should have_permissions(0777)
391
-
392
- CarrierWave.config[:permissions] = old_permissions
393
- end
394
-
395
- it "should not raise an integiry error if there is no white list" do
396
- @uploader.stub!(:extension_white_list).and_return(nil)
397
- running {
398
- @uploader.cache!(File.open(file_path('test.jpg')))
399
- }.should_not raise_error(CarrierWave::IntegrityError)
400
- end
401
-
402
- it "should not raise an integiry error if there is a white list and the file is on it" do
403
- @uploader.stub!(:extension_white_list).and_return(%w(jpg gif png))
404
- running {
405
- @uploader.cache!(File.open(file_path('test.jpg')))
406
- }.should_not raise_error(CarrierWave::IntegrityError)
407
- end
408
-
409
- it "should raise an integiry error if there is a white list and the file is not on it" do
410
- @uploader.stub!(:extension_white_list).and_return(%w(txt doc xls))
411
- running {
412
- @uploader.cache!(File.open(file_path('test.jpg')))
413
- }.should raise_error(CarrierWave::IntegrityError)
414
- end
415
- end
416
-
417
- describe '#retrieve_from_cache!' do
418
- it "should cache a file" do
419
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
420
- @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
421
- end
422
-
423
- it "should be cached" do
424
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
425
- @uploader.should be_cached
426
- end
427
-
428
- it "should set the path to the tmp dir" do
429
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
430
- @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpeg')
431
- end
432
-
433
- it "should overwrite a file that has already been cached" do
434
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
435
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/bork.txt')
436
- @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/bork.txt')
437
- end
438
-
439
- it "should store the cache_name" do
440
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
441
- @uploader.cache_name.should == '20071201-1234-345-2255/test.jpeg'
442
- end
443
-
444
- it "should store the filename" do
445
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
446
- @uploader.filename.should == 'test.jpeg'
447
- end
448
-
449
- it "should set the url" do
450
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
451
- @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpeg'
452
- end
453
-
454
- it "should raise an error when the cache_id has an invalid format" do
455
- running {
456
- @uploader.retrieve_from_cache!('12345/test.jpeg')
457
- }.should raise_error(CarrierWave::InvalidParameter)
458
-
459
- @uploader.file.should be_nil
460
- @uploader.filename.should be_nil
461
- @uploader.cache_name.should be_nil
462
- end
463
-
464
- it "should raise an error when the original_filename contains invalid characters" do
465
- running {
466
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/te/st.jpeg')
467
- }.should raise_error(CarrierWave::InvalidParameter)
468
- running {
469
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/te??%st.jpeg')
470
- }.should raise_error(CarrierWave::InvalidParameter)
471
-
472
- @uploader.file.should be_nil
473
- @uploader.filename.should be_nil
474
- @uploader.cache_name.should be_nil
475
- end
476
- end
477
-
478
- describe '#retrieve_from_cache' do
479
- it "should cache a file" do
480
- @uploader.retrieve_from_cache('20071201-1234-345-2255/test.jpeg')
481
- @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
482
- end
483
-
484
- it "should not overwrite a file that has already been cached" do
485
- @uploader.retrieve_from_cache('20071201-1234-345-2255/test.jpeg')
486
- @uploader.retrieve_from_cache('20071201-1234-345-2255/bork.txt')
487
- @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpeg')
488
- end
489
-
490
- it "should do nothing when the cache_id has an invalid format" do
491
- @uploader.retrieve_from_cache('12345/test.jpeg')
492
- @uploader.file.should be_nil
493
- @uploader.filename.should be_nil
494
- @uploader.cache_name.should be_nil
495
- end
496
-
497
- it "should do nothing when the filename contains invalid characters" do
498
- @uploader.retrieve_from_cache('20071201-1234-345-2255/te??%st.jpeg')
499
- @uploader.file.should be_nil
500
- @uploader.filename.should be_nil
501
- @uploader.cache_name.should be_nil
502
- end
503
- end
504
-
505
- describe '#store!' do
506
- before do
507
- @file = File.open(file_path('test.jpg'))
508
-
509
- @stored_file = mock('a stored file')
510
- @stored_file.stub!(:path).and_return('/path/to/somewhere')
511
- @stored_file.stub!(:url).and_return('http://www.example.com')
512
- @stored_file.stub!(:identifier).and_return('this-is-me')
513
-
514
- @uploader_class.storage.stub!(:store!).and_return(@stored_file)
515
- end
516
-
517
- it "should set the current path" do
518
- @uploader.store!(@file)
519
- @uploader.current_path.should == '/path/to/somewhere'
520
- end
521
-
522
- it "should not be cached" do
523
- @uploader.store!(@file)
524
- @uploader.should_not be_cached
525
- end
526
-
527
- it "should set the url" do
528
- @uploader.store!(@file)
529
- @uploader.url.should == 'http://www.example.com'
530
- end
531
-
532
- it "should set the identifier" do
533
- @uploader.store!(@file)
534
- @uploader.identifier.should == 'this-is-me'
535
- end
536
-
537
- it "should, if a file is given as argument, cache that file" do
538
- @uploader.should_receive(:cache!).with(@file)
539
- @uploader.store!(@file)
540
- end
541
-
542
- it "should use a previously cached file if no argument is given" do
543
- @uploader.cache!(File.open(file_path('test.jpg')))
544
- @uploader.should_not_receive(:cache!)
545
- @uploader.store!
546
- end
547
-
548
- it "should instruct the storage engine to store the file" do
549
- @uploader.cache!(@file)
550
- @uploader_class.storage.should_receive(:store!).with(@uploader, @uploader.file).and_return(:monkey)
551
- @uploader.store!
552
- end
553
-
554
- it "should reset the cache_name" do
555
- @uploader.cache!(@file)
556
- @uploader.store!
557
- @uploader.cache_name.should be_nil
558
- end
559
-
560
- it "should cache the result given by the storage engine" do
561
- @uploader.store!(@file)
562
- @uploader.file.should == @stored_file
563
- end
564
-
565
- it "should do nothing when trying to store an empty file" do
566
- @uploader.store!(nil)
567
- end
568
-
569
- it "should not re-store a retrieved file" do
570
- @stored_file = mock('a stored file')
571
- @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
572
-
573
- @uploader_class.storage.should_not_receive(:store!)
574
- @uploader.retrieve_from_store!('monkey.txt')
575
- @uploader.store!
576
- end
577
- end
578
-
579
- describe '#retrieve_from_store!' do
580
- before do
581
- @stored_file = mock('a stored file')
582
- @stored_file.stub!(:path).and_return('/path/to/somewhere')
583
- @stored_file.stub!(:url).and_return('http://www.example.com')
584
- @stored_file.stub!(:identifier).and_return('this-is-me')
585
-
586
- @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
587
- end
588
-
589
- it "should set the current path" do
590
- @uploader.retrieve_from_store!('monkey.txt')
591
- @uploader.current_path.should == '/path/to/somewhere'
592
- end
593
-
594
- it "should not be cached" do
595
- @uploader.retrieve_from_store!('monkey.txt')
596
- @uploader.should_not be_cached
597
- end
598
-
599
- it "should set the url" do
600
- @uploader.retrieve_from_store!('monkey.txt')
601
- @uploader.url.should == 'http://www.example.com'
602
- end
603
-
604
- it "should set the identifier" do
605
- @uploader.retrieve_from_store!('monkey.txt')
606
- @uploader.identifier.should == 'this-is-me'
607
- end
608
-
609
- it "should instruct the storage engine to retrieve the file and store the result" do
610
- @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
611
- @uploader.retrieve_from_store!('monkey.txt')
612
- @uploader.file.should == @stored_file
613
- end
614
-
615
- it "should overwrite a file that has already been cached" do
616
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
617
- @uploader.retrieve_from_store!('bork.txt')
618
- @uploader.file.should == @stored_file
619
- end
620
- end
621
-
622
- describe '#retrieve_from_store' do
623
- before do
624
- @stored_file = mock('a stored file')
625
- @stored_file.stub!(:path).and_return('/path/to/somewhere')
626
- @stored_file.stub!(:url).and_return('http://www.example.com')
627
- @stored_file.stub!(:identifier).and_return('this-is-me')
628
- @stored_file.stub!(:read).and_return('here be content')
629
-
630
- @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
631
- end
632
-
633
- it "should set the current path" do
634
- @uploader.retrieve_from_store('monkey.txt')
635
- @uploader.current_path.should == '/path/to/somewhere'
636
- end
637
-
638
- it "should set the url" do
639
- @uploader.retrieve_from_store('monkey.txt')
640
- @uploader.url.should == 'http://www.example.com'
641
- end
642
-
643
- it "should set the identifier" do
644
- @uploader.retrieve_from_store('monkey.txt')
645
- @uploader.identifier.should == 'this-is-me'
646
- end
647
-
648
- it "should read out the contents" do
649
- @uploader.retrieve_from_store('monkey.txt')
650
- @uploader.read.should == 'here be content'
651
- end
652
-
653
- it "should instruct the storage engine to retrieve the file and store the result" do
654
- @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
655
- @uploader.retrieve_from_store('monkey.txt')
656
- @uploader.file.should == @stored_file
657
- end
658
-
659
- it "should not overwrite a file that has already been cached" do
660
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
661
- @uploader.retrieve_from_store('bork.txt')
662
- @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpeg')
663
- end
664
- end
665
-
666
- describe 'with a version' do
667
- before do
668
- @uploader_class.version(:thumb)
669
- end
670
-
671
- describe '#cache!' do
672
-
673
- before do
674
- CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
675
- end
676
-
677
- it "should set store_path with versions" do
678
- @uploader.cache!(File.open(file_path('test.jpg')))
679
- @uploader.store_path.should == 'uploads/test.jpg'
680
- @uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
681
- @uploader.thumb.store_path('kebab.png').should == 'uploads/thumb_kebab.png'
682
- end
683
-
684
- it "should move it to the tmp dir with the filename prefixed" do
685
- @uploader.cache!(File.open(file_path('test.jpg')))
686
- @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
687
- @uploader.thumb.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/thumb_test.jpg')
688
- @uploader.file.exists?.should be_true
689
- @uploader.thumb.file.exists?.should be_true
690
- end
691
- end
692
-
693
- describe '#retrieve_from_cache!' do
694
- it "should set the path to the tmp dir" do
695
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
696
- @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
697
- @uploader.thumb.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/thumb_test.jpg')
698
- end
699
-
700
- it "should set store_path with versions" do
701
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
702
- @uploader.store_path.should == 'uploads/test.jpg'
703
- @uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
704
- @uploader.thumb.store_path('kebab.png').should == 'uploads/thumb_kebab.png'
705
- end
706
- end
707
-
708
- describe '#store!' do
709
- before do
710
- @file = File.open(file_path('test.jpg'))
711
-
712
- @stored_file = mock('a stored file')
713
- @stored_file.stub!(:path).and_return('/path/to/somewhere')
714
- @stored_file.stub!(:url).and_return('http://www.example.com')
715
-
716
- @uploader_class.storage.stub!(:store!).and_return(@stored_file)
717
- end
718
-
719
- after do
720
- CarrierWave.config[:use_cache] = true
721
- end
722
-
723
- it "should set the current path for the version" do
724
- pending "find a decent way to spec this"
725
- @uploader.store!(@file)
726
- @uploader.current_path.should == '/path/to/somewhere'
727
- @uploader.thumb.current_path.should == '/path/to/somewhere'
728
- end
729
-
730
- it "should set the url" do
731
- pending "find a decent way to spec this"
732
- @uploader.store!(@file)
733
- @uploader.url.should == 'http://www.example.com'
734
- end
735
-
736
- it "should, if a file is given as argument, set the store_path" do
737
- @uploader.store!(@file)
738
- @uploader.store_path.should == 'uploads/test.jpg'
739
- @uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
740
- @uploader.thumb.store_path('kebab.png').should == 'uploads/thumb_kebab.png'
741
- end
742
-
743
- end
744
-
745
- describe '#retrieve_from_store!' do
746
- before do
747
- @stored_file = mock('a stored file')
748
- @stored_file.stub!(:path).and_return('/path/to/somewhere')
749
- @stored_file.stub!(:url).and_return('http://www.example.com')
750
-
751
- @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
752
- end
753
-
754
- it "should set the current path" do
755
- @uploader.retrieve_from_store!('monkey.txt')
756
- @uploader.current_path.should == '/path/to/somewhere'
757
- end
758
-
759
- it "should set the url" do
760
- @uploader.retrieve_from_store!('monkey.txt')
761
- @uploader.url.should == 'http://www.example.com'
762
- end
763
-
764
- it "should pass the identifier to the storage engine" do
765
- @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
766
- @uploader.retrieve_from_store!('monkey.txt')
767
- @uploader.file.should == @stored_file
768
- end
769
-
770
- it "should not set the filename" do
771
- @uploader.retrieve_from_store!('monkey.txt')
772
- @uploader.filename.should be_nil
773
- end
774
- end
775
- end
776
-
777
- describe 'with an overridden, reversing, filename' do
778
- before do
779
- @uploader_class.class_eval do
780
- def filename
781
- super.reverse unless super.blank?
782
- end
783
- end
784
- end
785
-
786
- describe '#cache!' do
787
-
788
- before do
789
- CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
790
- end
791
-
792
- it "should set the filename to the file's reversed filename" do
793
- @uploader.cache!(File.open(file_path('test.jpg')))
794
- @uploader.filename.should == "gpj.tset"
795
- end
796
-
797
- it "should move it to the tmp dir with the filename unreversed" do
798
- @uploader.cache!(File.open(file_path('test.jpg')))
799
- @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
800
- @uploader.file.exists?.should be_true
801
- end
802
- end
803
-
804
- describe '#retrieve_from_cache!' do
805
- it "should set the path to the tmp dir" do
806
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
807
- @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
808
- end
809
-
810
- it "should set the filename to the reversed name of the file" do
811
- @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
812
- @uploader.filename.should == "gpj.tset"
813
- end
814
- end
815
-
816
- describe '#store!' do
817
- before do
818
- @file = File.open(file_path('test.jpg'))
819
-
820
- @stored_file = mock('a stored file')
821
- @stored_file.stub!(:path).and_return('/path/to/somewhere')
822
- @stored_file.stub!(:url).and_return('http://www.example.com')
823
-
824
- @uploader_class.storage.stub!(:store!).and_return(@stored_file)
825
- end
826
-
827
- after do
828
- CarrierWave.config[:use_cache] = true
829
- end
830
-
831
- it "should set the current path" do
832
- @uploader.store!(@file)
833
- @uploader.current_path.should == '/path/to/somewhere'
834
- end
835
-
836
- it "should set the url" do
837
- @uploader.store!(@file)
838
- @uploader.url.should == 'http://www.example.com'
839
- end
840
-
841
- it "should, if a file is given as argument, reverse the filename" do
842
- @uploader.store!(@file)
843
- @uploader.filename.should == 'gpj.tset'
844
- end
845
-
846
- it "should, if a files is given as an argument and use_cache is false, reverse the filename" do
847
- CarrierWave.config[:use_cache] = false
848
- @uploader.store!(@file)
849
- @uploader.filename.should == 'gpj.tset'
850
- end
851
-
852
- end
853
-
854
- describe '#retrieve_from_store!' do
855
- before do
856
- @stored_file = mock('a stored file')
857
- @stored_file.stub!(:path).and_return('/path/to/somewhere')
858
- @stored_file.stub!(:url).and_return('http://www.example.com')
859
-
860
- @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
861
- end
862
-
863
- it "should set the current path" do
864
- @uploader.retrieve_from_store!('monkey.txt')
865
- @uploader.current_path.should == '/path/to/somewhere'
866
- end
867
-
868
- it "should set the url" do
869
- @uploader.retrieve_from_store!('monkey.txt')
870
- @uploader.url.should == 'http://www.example.com'
871
- end
872
-
873
- it "should pass the identifier to the storage engine" do
874
- @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
875
- @uploader.retrieve_from_store!('monkey.txt')
876
- @uploader.file.should == @stored_file
877
- end
878
-
879
- it "should not set the filename" do
880
- @uploader.retrieve_from_store!('monkey.txt')
881
- @uploader.filename.should be_nil
882
- end
883
- end
884
-
885
- end
886
-
887
- end