samlown-carrierwave 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/Generators +4 -0
  2. data/History.txt +125 -0
  3. data/Manifest.txt +110 -0
  4. data/README.rdoc +524 -0
  5. data/Rakefile +39 -0
  6. data/carrierwave.gemspec +85 -0
  7. data/cucumber.yml +2 -0
  8. data/features/caching.feature +28 -0
  9. data/features/download.feature +20 -0
  10. data/features/file_storage.feature +37 -0
  11. data/features/file_storage_overridden_filename.feature +38 -0
  12. data/features/file_storage_overridden_store_dir.feature +38 -0
  13. data/features/file_storage_reversing_processor.feature +43 -0
  14. data/features/fixtures/bork.txt +1 -0
  15. data/features/fixtures/monkey.txt +1 -0
  16. data/features/grid_fs_storage.feature +32 -0
  17. data/features/mount_activerecord.feature +46 -0
  18. data/features/mount_datamapper.feature +46 -0
  19. data/features/step_definitions/activerecord_steps.rb +22 -0
  20. data/features/step_definitions/caching_steps.rb +14 -0
  21. data/features/step_definitions/datamapper_steps.rb +29 -0
  22. data/features/step_definitions/download_steps.rb +4 -0
  23. data/features/step_definitions/file_steps.rb +53 -0
  24. data/features/step_definitions/general_steps.rb +85 -0
  25. data/features/step_definitions/mount_steps.rb +19 -0
  26. data/features/step_definitions/store_steps.rb +18 -0
  27. data/features/support/activerecord.rb +30 -0
  28. data/features/support/datamapper.rb +7 -0
  29. data/features/support/env.rb +22 -0
  30. data/features/versions_basics.feature +50 -0
  31. data/features/versions_nested_versions.feature +70 -0
  32. data/features/versions_overridden_filename.feature +51 -0
  33. data/features/versions_overriden_store_dir.feature +41 -0
  34. data/lib/carrierwave.rb +98 -0
  35. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  36. data/lib/carrierwave/core_ext/blank.rb +46 -0
  37. data/lib/carrierwave/core_ext/file.rb +11 -0
  38. data/lib/carrierwave/core_ext/inheritable_attributes.rb +108 -0
  39. data/lib/carrierwave/core_ext/module_setup.rb +51 -0
  40. data/lib/carrierwave/mount.rb +359 -0
  41. data/lib/carrierwave/orm/activerecord.rb +73 -0
  42. data/lib/carrierwave/orm/datamapper.rb +27 -0
  43. data/lib/carrierwave/orm/mongoid.rb +23 -0
  44. data/lib/carrierwave/orm/mongomapper.rb +27 -0
  45. data/lib/carrierwave/orm/sequel.rb +45 -0
  46. data/lib/carrierwave/processing/image_science.rb +101 -0
  47. data/lib/carrierwave/processing/mini_magick.rb +265 -0
  48. data/lib/carrierwave/processing/rmagick.rb +282 -0
  49. data/lib/carrierwave/sanitized_file.rb +273 -0
  50. data/lib/carrierwave/storage/abstract.rb +30 -0
  51. data/lib/carrierwave/storage/cloud_files.rb +169 -0
  52. data/lib/carrierwave/storage/file.rb +48 -0
  53. data/lib/carrierwave/storage/grid_fs.rb +97 -0
  54. data/lib/carrierwave/storage/right_s3.rb +3 -0
  55. data/lib/carrierwave/storage/s3.rb +206 -0
  56. data/lib/carrierwave/test/matchers.rb +128 -0
  57. data/lib/carrierwave/uploader.rb +44 -0
  58. data/lib/carrierwave/uploader/cache.rb +145 -0
  59. data/lib/carrierwave/uploader/callbacks.rb +42 -0
  60. data/lib/carrierwave/uploader/configuration.rb +132 -0
  61. data/lib/carrierwave/uploader/default_url.rb +19 -0
  62. data/lib/carrierwave/uploader/download.rb +59 -0
  63. data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
  64. data/lib/carrierwave/uploader/mountable.rb +39 -0
  65. data/lib/carrierwave/uploader/processing.rb +83 -0
  66. data/lib/carrierwave/uploader/proxy.rb +62 -0
  67. data/lib/carrierwave/uploader/remove.rb +22 -0
  68. data/lib/carrierwave/uploader/store.rb +89 -0
  69. data/lib/carrierwave/uploader/url.rb +33 -0
  70. data/lib/carrierwave/uploader/versions.rb +146 -0
  71. data/merb_generators/uploader_generator.rb +22 -0
  72. data/rails_generators/uploader/USAGE +2 -0
  73. data/rails_generators/uploader/templates/uploader.rb +47 -0
  74. data/rails_generators/uploader/uploader_generator.rb +21 -0
  75. data/script/console +10 -0
  76. data/script/destroy +14 -0
  77. data/script/generate +14 -0
  78. data/spec/compatibility/paperclip_spec.rb +52 -0
  79. data/spec/fixtures/bork.txt +1 -0
  80. data/spec/fixtures/landscape.jpg +0 -0
  81. data/spec/fixtures/portrait.jpg +0 -0
  82. data/spec/fixtures/test.jpeg +1 -0
  83. data/spec/fixtures/test.jpg +1 -0
  84. data/spec/mount_spec.rb +538 -0
  85. data/spec/orm/activerecord_spec.rb +271 -0
  86. data/spec/orm/datamapper_spec.rb +168 -0
  87. data/spec/orm/mongoid_spec.rb +202 -0
  88. data/spec/orm/mongomapper_spec.rb +202 -0
  89. data/spec/orm/sequel_spec.rb +183 -0
  90. data/spec/processing/image_science_spec.rb +56 -0
  91. data/spec/processing/mini_magick_spec.rb +76 -0
  92. data/spec/processing/rmagick_spec.rb +75 -0
  93. data/spec/sanitized_file_spec.rb +623 -0
  94. data/spec/spec_helper.rb +92 -0
  95. data/spec/storage/cloudfiles_spec.rb +78 -0
  96. data/spec/storage/grid_fs_spec.rb +83 -0
  97. data/spec/storage/s3_spec.rb +118 -0
  98. data/spec/uploader/cache_spec.rb +209 -0
  99. data/spec/uploader/configuration_spec.rb +105 -0
  100. data/spec/uploader/default_url_spec.rb +85 -0
  101. data/spec/uploader/download_spec.rb +75 -0
  102. data/spec/uploader/extension_whitelist_spec.rb +44 -0
  103. data/spec/uploader/mountable_spec.rb +33 -0
  104. data/spec/uploader/paths_spec.rb +22 -0
  105. data/spec/uploader/processing_spec.rb +73 -0
  106. data/spec/uploader/proxy_spec.rb +54 -0
  107. data/spec/uploader/remove_spec.rb +70 -0
  108. data/spec/uploader/store_spec.rb +264 -0
  109. data/spec/uploader/url_spec.rb +102 -0
  110. data/spec/uploader/versions_spec.rb +298 -0
  111. metadata +433 -0
@@ -0,0 +1,70 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#remove!' do
17
+ before do
18
+ @file = File.open(file_path('test.jpg'))
19
+
20
+ @stored_file = mock('a stored file')
21
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
22
+ @stored_file.stub!(:url).and_return('http://www.example.com')
23
+ @stored_file.stub!(:identifier).and_return('this-is-me')
24
+ @stored_file.stub!(:delete)
25
+
26
+ @storage = mock('a storage engine')
27
+ @storage.stub!(:store!).and_return(@stored_file)
28
+
29
+ @uploader_class.storage.stub!(:new).and_return(@storage)
30
+ @uploader.store!(@file)
31
+ end
32
+
33
+ it "should reset the current path" do
34
+ @uploader.remove!
35
+ @uploader.current_path.should be_nil
36
+ end
37
+
38
+ it "should not be cached" do
39
+ @uploader.remove!
40
+ @uploader.should_not be_cached
41
+ end
42
+
43
+ it "should reset the url" do
44
+ @uploader.cache!(@file)
45
+ @uploader.remove!
46
+ @uploader.url.should be_nil
47
+ end
48
+
49
+ it "should reset the identifier" do
50
+ @uploader.remove!
51
+ @uploader.identifier.should be_nil
52
+ end
53
+
54
+ it "should delete the file" do
55
+ @stored_file.should_receive(:delete)
56
+ @uploader.remove!
57
+ end
58
+
59
+ it "should reset the cache_name" do
60
+ @uploader.cache!(@file)
61
+ @uploader.remove!
62
+ @uploader.cache_name.should be_nil
63
+ end
64
+
65
+ it "should do nothing when trying to remove an empty file" do
66
+ running { @uploader.remove! }.should_not raise_error
67
+ end
68
+ end
69
+
70
+ end
@@ -0,0 +1,264 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#store_dir' do
17
+ it "should default to the config option" do
18
+ @uploader.store_dir.should == 'uploads'
19
+ end
20
+ end
21
+
22
+ describe '#filename' do
23
+ it "should default to nil" do
24
+ @uploader.filename.should be_nil
25
+ end
26
+ end
27
+
28
+ describe '#store!' do
29
+ before do
30
+ @file = File.open(file_path('test.jpg'))
31
+
32
+ @stored_file = mock('a stored file')
33
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
34
+ @stored_file.stub!(:url).and_return('http://www.example.com')
35
+
36
+ @storage = mock('a storage engine')
37
+ @storage.stub!(:store!).and_return(@stored_file)
38
+ @storage.stub!(:identifier).and_return('this-is-me')
39
+
40
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
41
+ end
42
+
43
+ it "should set the current path" do
44
+ @uploader.store!(@file)
45
+ @uploader.current_path.should == '/path/to/somewhere'
46
+ end
47
+
48
+ it "should not be cached" do
49
+ @uploader.store!(@file)
50
+ @uploader.should_not be_cached
51
+ end
52
+
53
+ it "should set the url" do
54
+ @uploader.store!(@file)
55
+ @uploader.url.should == 'http://www.example.com'
56
+ end
57
+
58
+ it "should set the identifier" do
59
+ @uploader.store!(@file)
60
+ @uploader.identifier.should == 'this-is-me'
61
+ end
62
+
63
+ it "should, if a file is given as argument, cache that file" do
64
+ @uploader.should_receive(:cache!).with(@file)
65
+ @uploader.store!(@file)
66
+ end
67
+
68
+ it "should use a previously cached file if no argument is given" do
69
+ @uploader.cache!(File.open(file_path('test.jpg')))
70
+ @uploader.should_not_receive(:cache!)
71
+ @uploader.store!
72
+ end
73
+
74
+ it "should instruct the storage engine to store the file" do
75
+ @uploader.cache!(@file)
76
+ @storage.should_receive(:store!).with(@uploader.file).and_return(:monkey)
77
+ @uploader.store!
78
+ end
79
+
80
+ it "should reset the cache_name" do
81
+ @uploader.cache!(@file)
82
+ @uploader.store!
83
+ @uploader.cache_name.should be_nil
84
+ end
85
+
86
+ it "should cache the result given by the storage engine" do
87
+ @uploader.store!(@file)
88
+ @uploader.file.should == @stored_file
89
+ end
90
+
91
+ it "should do nothing when trying to store an empty file" do
92
+ @uploader.store!(nil)
93
+ end
94
+
95
+ it "should not re-store a retrieved file" do
96
+ @stored_file = mock('a stored file')
97
+ @storage.stub!(:retrieve!).and_return(@stored_file)
98
+
99
+ @uploader_class.storage.should_not_receive(:store!)
100
+ @uploader.retrieve_from_store!('monkey.txt')
101
+ @uploader.store!
102
+ end
103
+ end
104
+
105
+ describe '#retrieve_from_store!' do
106
+ before do
107
+ @stored_file = mock('a stored file')
108
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
109
+ @stored_file.stub!(:url).and_return('http://www.example.com')
110
+
111
+ @storage = mock('a storage engine')
112
+ @storage.stub!(:retrieve!).and_return(@stored_file)
113
+ @storage.stub!(:identifier).and_return('this-is-me')
114
+
115
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
116
+ end
117
+
118
+ it "should set the current path" do
119
+ @uploader.retrieve_from_store!('monkey.txt')
120
+ @uploader.current_path.should == '/path/to/somewhere'
121
+ end
122
+
123
+ it "should not be cached" do
124
+ @uploader.retrieve_from_store!('monkey.txt')
125
+ @uploader.should_not be_cached
126
+ end
127
+
128
+ it "should set the url" do
129
+ @uploader.retrieve_from_store!('monkey.txt')
130
+ @uploader.url.should == 'http://www.example.com'
131
+ end
132
+
133
+ it "should set the identifier" do
134
+ @uploader.retrieve_from_store!('monkey.txt')
135
+ @uploader.identifier.should == 'this-is-me'
136
+ end
137
+
138
+ it "should instruct the storage engine to retrieve the file and store the result" do
139
+ @storage.should_receive(:retrieve!).with('monkey.txt').and_return(@stored_file)
140
+ @uploader.retrieve_from_store!('monkey.txt')
141
+ @uploader.file.should == @stored_file
142
+ end
143
+
144
+ it "should overwrite a file that has already been cached" do
145
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
146
+ @uploader.retrieve_from_store!('bork.txt')
147
+ @uploader.file.should == @stored_file
148
+ end
149
+ end
150
+
151
+ describe 'with an overridden filename' do
152
+ before do
153
+ @uploader_class.class_eval do
154
+ def filename; "foo.jpg"; end
155
+ end
156
+ end
157
+
158
+ it "should create new files if there is a file" do
159
+ @file = File.open(file_path('test.jpg'))
160
+ @uploader.store!(@file)
161
+ @path = ::File.expand_path(@uploader.store_path, @uploader.root)
162
+ File.exist?(@path).should be_true
163
+ end
164
+
165
+ it "should not create new files if there is no file" do
166
+ @uploader.store!(nil)
167
+ @path = ::File.expand_path(@uploader.store_path, @uploader.root)
168
+ File.exist?(@path).should be_false
169
+ end
170
+ end
171
+
172
+ describe 'without a store dir' do
173
+ before do
174
+ @uploader_class.class_eval do
175
+ def store_dir; nil; end
176
+ end
177
+ end
178
+
179
+ it "should create a new file with a valid path and url" do
180
+ @file = File.open(file_path('test.jpg'))
181
+ @uploader.store!(@file)
182
+ @path = ::File.expand_path(@uploader.store_path, @uploader.root)
183
+ File.exist?(@path).should be_true
184
+ @uploader.url.should == '/test.jpg'
185
+ end
186
+ end
187
+
188
+ describe 'with an overridden, reversing, filename' do
189
+ before do
190
+ @uploader_class.class_eval do
191
+ def filename
192
+ super.reverse unless super.blank?
193
+ end
194
+ end
195
+ end
196
+
197
+ describe '#store!' do
198
+ before do
199
+ @file = File.open(file_path('test.jpg'))
200
+
201
+ @stored_file = mock('a stored file')
202
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
203
+ @stored_file.stub!(:url).and_return('http://www.example.com')
204
+
205
+ @storage = mock('a storage engine')
206
+ @storage.stub!(:store!).and_return(@stored_file)
207
+
208
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
209
+ end
210
+
211
+ it "should set the current path" do
212
+ @uploader.store!(@file)
213
+ @uploader.current_path.should == '/path/to/somewhere'
214
+ end
215
+
216
+ it "should set the url" do
217
+ @uploader.store!(@file)
218
+ @uploader.url.should == 'http://www.example.com'
219
+ end
220
+
221
+ it "should, if a file is given as argument, reverse the filename" do
222
+ @uploader.store!(@file)
223
+ @uploader.filename.should == 'gpj.tset'
224
+ end
225
+
226
+ end
227
+
228
+ describe '#retrieve_from_store!' do
229
+ before do
230
+ @stored_file = mock('a stored file')
231
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
232
+ @stored_file.stub!(:url).and_return('http://www.example.com')
233
+
234
+ @storage = mock('a storage engine')
235
+ @storage.stub!(:retrieve!).and_return(@stored_file)
236
+
237
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
238
+ end
239
+
240
+ it "should set the current path" do
241
+ @uploader.retrieve_from_store!('monkey.txt')
242
+ @uploader.current_path.should == '/path/to/somewhere'
243
+ end
244
+
245
+ it "should set the url" do
246
+ @uploader.retrieve_from_store!('monkey.txt')
247
+ @uploader.url.should == 'http://www.example.com'
248
+ end
249
+
250
+ it "should pass the identifier to the storage engine" do
251
+ @storage.should_receive(:retrieve!).with('monkey.txt').and_return(@stored_file)
252
+ @uploader.retrieve_from_store!('monkey.txt')
253
+ @uploader.file.should == @stored_file
254
+ end
255
+
256
+ it "should not set the filename" do
257
+ @uploader.retrieve_from_store!('monkey.txt')
258
+ @uploader.filename.should be_nil
259
+ end
260
+ end
261
+
262
+ end
263
+
264
+ end
@@ -0,0 +1,102 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#url' do
17
+ before do
18
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
19
+ end
20
+
21
+ it "should default to nil" do
22
+ @uploader.url.should be_nil
23
+ end
24
+
25
+ it "should raise ArgumentError when version doesn't exist" do
26
+ lambda { @uploader.url(:thumb) }.should raise_error(ArgumentError)
27
+ end
28
+
29
+ it "should not raise ArgumentError when versions version exists" do
30
+ @uploader_class.version(:thumb)
31
+ lambda { @uploader.url(:thumb) }.should_not raise_error(ArgumentError)
32
+ end
33
+
34
+ it "should get the directory relative to public, prepending a slash" do
35
+ @uploader.cache!(File.open(file_path('test.jpg')))
36
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
37
+ end
38
+
39
+ it "should get the directory relative to public for a specific version" do
40
+ @uploader_class.version(:thumb)
41
+ @uploader.cache!(File.open(file_path('test.jpg')))
42
+ @uploader.url(:thumb).should == '/uploads/tmp/20071201-1234-345-2255/thumb_test.jpg'
43
+ end
44
+
45
+ it "should get the directory relative to public for a nested version" do
46
+ @uploader_class.version(:thumb) do
47
+ version(:mini)
48
+ end
49
+ @uploader.cache!(File.open(file_path('test.jpg')))
50
+ @uploader.url(:thumb, :mini).should == '/uploads/tmp/20071201-1234-345-2255/thumb_mini_test.jpg'
51
+ end
52
+
53
+ it "should return file#url if available" do
54
+ @uploader.cache!(File.open(file_path('test.jpg')))
55
+ @uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
56
+ @uploader.url.should == 'http://www.example.com/someurl.jpg'
57
+ end
58
+
59
+ it "should get the directory relative to public, if file#url is blank" do
60
+ @uploader.cache!(File.open(file_path('test.jpg')))
61
+ @uploader.file.stub!(:url).and_return('')
62
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
63
+ end
64
+ end
65
+
66
+ describe '#to_json' do
67
+ before do
68
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
69
+ end
70
+
71
+ it "should return a hash with a blank URL" do
72
+ JSON.parse(@uploader.to_json)['url'].should be_nil
73
+ end
74
+
75
+ it "should return a hash including a cached URL" do
76
+ @uploader.cache!(File.open(file_path('test.jpg')))
77
+ JSON.parse(@uploader.to_json)['url'].should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
78
+ end
79
+ end
80
+
81
+ describe '#to_s' do
82
+ before do
83
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
84
+ end
85
+
86
+ it "should default to nil" do
87
+ @uploader.to_s.should be_nil
88
+ end
89
+
90
+ it "should get the directory relative to public, prepending a slash" do
91
+ @uploader.cache!(File.open(file_path('test.jpg')))
92
+ @uploader.to_s.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
93
+ end
94
+
95
+ it "should return file#url if available" do
96
+ @uploader.cache!(File.open(file_path('test.jpg')))
97
+ @uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
98
+ @uploader.to_s.should == 'http://www.example.com/someurl.jpg'
99
+ end
100
+ end
101
+
102
+ end
@@ -0,0 +1,298 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
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 'with a version' do
106
+ before do
107
+ @uploader_class.version(:thumb)
108
+ end
109
+
110
+ describe '#cache!' do
111
+
112
+ before do
113
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
114
+ end
115
+
116
+ it "should set store_path with versions" do
117
+ @uploader.cache!(File.open(file_path('test.jpg')))
118
+ @uploader.store_path.should == 'uploads/test.jpg'
119
+ @uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
120
+ @uploader.thumb.store_path('kebab.png').should == 'uploads/thumb_kebab.png'
121
+ end
122
+
123
+ it "should move it to the tmp dir with the filename prefixed" do
124
+ @uploader.cache!(File.open(file_path('test.jpg')))
125
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
126
+ @uploader.thumb.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/thumb_test.jpg')
127
+ @uploader.file.exists?.should be_true
128
+ @uploader.thumb.file.exists?.should be_true
129
+ end
130
+ end
131
+
132
+ describe '#retrieve_from_cache!' do
133
+ it "should set the path to the tmp dir" do
134
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
135
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
136
+ @uploader.thumb.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/thumb_test.jpg')
137
+ end
138
+
139
+ it "should set store_path with versions" do
140
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
141
+ @uploader.store_path.should == 'uploads/test.jpg'
142
+ @uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
143
+ @uploader.thumb.store_path('kebab.png').should == 'uploads/thumb_kebab.png'
144
+ end
145
+ end
146
+
147
+ describe '#store!' do
148
+ before do
149
+ @uploader_class.storage = mock_storage('base')
150
+ @uploader_class.version(:thumb).storage = mock_storage('thumb')
151
+
152
+ @file = File.open(file_path('test.jpg'))
153
+
154
+ @base_stored_file = mock('a stored file')
155
+ @base_stored_file.stub!(:path).and_return('/path/to/somewhere')
156
+ @base_stored_file.stub!(:url).and_return('http://www.example.com')
157
+
158
+ @thumb_stored_file = mock('a thumb version of a stored file')
159
+ @thumb_stored_file.stub!(:path).and_return('/path/to/somewhere/thumb')
160
+ @thumb_stored_file.stub!(:url).and_return('http://www.example.com/thumb')
161
+
162
+ @storage = mock('a storage engine')
163
+ @storage.stub!(:store!).and_return(@base_stored_file)
164
+
165
+ @thumb_storage = mock('a storage engine for thumbnails')
166
+ @thumb_storage.stub!(:store!).and_return(@thumb_stored_file)
167
+
168
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
169
+ @uploader_class.version(:thumb).storage.stub!(:new).with(@uploader.thumb).and_return(@thumb_storage)
170
+ end
171
+
172
+ it "should set the current path for the version" do
173
+ @uploader.store!(@file)
174
+ @uploader.current_path.should == '/path/to/somewhere'
175
+ @uploader.thumb.current_path.should == '/path/to/somewhere/thumb'
176
+ end
177
+
178
+ it "should set the url" do
179
+ @uploader.store!(@file)
180
+ @uploader.url.should == 'http://www.example.com'
181
+ @uploader.thumb.url.should == 'http://www.example.com/thumb'
182
+ end
183
+
184
+ it "should, if a file is given as argument, set the store_path" do
185
+ @uploader.store!(@file)
186
+ @uploader.store_path.should == 'uploads/test.jpg'
187
+ @uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
188
+ @uploader.thumb.store_path('kebab.png').should == 'uploads/thumb_kebab.png'
189
+ end
190
+
191
+ it "should instruct the storage engine to store the file and its version" do
192
+ @uploader.cache!(@file)
193
+ @storage.should_receive(:store!).with(@uploader.file).and_return(:monkey)
194
+ @thumb_storage.should_receive(:store!).with(@uploader.thumb.file).and_return(:gorilla)
195
+ @uploader.store!
196
+ end
197
+
198
+ end
199
+
200
+ describe '#remove!' do
201
+ before do
202
+ @uploader_class.storage = mock_storage('base')
203
+ @uploader_class.version(:thumb).storage = mock_storage('thumb')
204
+
205
+ @file = File.open(file_path('test.jpg'))
206
+
207
+ @base_stored_file = mock('a stored file')
208
+ @thumb_stored_file = mock('a thumb version of a stored file')
209
+
210
+ @storage = mock('a storage engine')
211
+ @storage.stub!(:store!).and_return(@base_stored_file)
212
+
213
+ @thumb_storage = mock('a storage engine for thumbnails')
214
+ @thumb_storage.stub!(:store!).and_return(@thumb_stored_file)
215
+
216
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
217
+ @uploader_class.version(:thumb).storage.stub!(:new).with(@uploader.thumb).and_return(@thumb_storage)
218
+
219
+ @base_stored_file.stub!(:delete)
220
+ @thumb_stored_file.stub!(:delete)
221
+
222
+ @uploader.store!(@file)
223
+ end
224
+
225
+ it "should reset the current path for the version" do
226
+ @uploader.remove!
227
+ @uploader.current_path.should be_nil
228
+ @uploader.thumb.current_path.should be_nil
229
+ end
230
+
231
+ it "should reset the url" do
232
+ @uploader.remove!
233
+ @uploader.url.should be_nil
234
+ @uploader.thumb.url.should be_nil
235
+ end
236
+
237
+ it "should delete all the files" do
238
+ @base_stored_file.should_receive(:delete)
239
+ @thumb_stored_file.should_receive(:delete)
240
+ @uploader.remove!
241
+ end
242
+
243
+ end
244
+
245
+
246
+ describe '#retrieve_from_store!' do
247
+ before do
248
+ @uploader_class.storage = mock_storage('base')
249
+ @uploader_class.version(:thumb).storage = mock_storage('thumb')
250
+
251
+ @file = File.open(file_path('test.jpg'))
252
+
253
+ @base_stored_file = mock('a stored file')
254
+ @base_stored_file.stub!(:path).and_return('/path/to/somewhere')
255
+ @base_stored_file.stub!(:url).and_return('http://www.example.com')
256
+
257
+ @thumb_stored_file = mock('a thumb version of a stored file')
258
+ @thumb_stored_file.stub!(:path).and_return('/path/to/somewhere/thumb')
259
+ @thumb_stored_file.stub!(:url).and_return('http://www.example.com/thumb')
260
+
261
+ @storage = mock('a storage engine')
262
+ @storage.stub!(:retrieve!).and_return(@base_stored_file)
263
+
264
+ @thumb_storage = mock('a storage engine for thumbnails')
265
+ @thumb_storage.stub!(:retrieve!).and_return(@thumb_stored_file)
266
+
267
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
268
+ @uploader_class.version(:thumb).storage.stub!(:new).with(@uploader.thumb).and_return(@thumb_storage)
269
+ end
270
+
271
+ it "should set the current path" do
272
+ @uploader.retrieve_from_store!('monkey.txt')
273
+ @uploader.current_path.should == '/path/to/somewhere'
274
+ @uploader.thumb.current_path.should == '/path/to/somewhere/thumb'
275
+ end
276
+
277
+ it "should set the url" do
278
+ @uploader.retrieve_from_store!('monkey.txt')
279
+ @uploader.url.should == 'http://www.example.com'
280
+ @uploader.thumb.url.should == 'http://www.example.com/thumb'
281
+ end
282
+
283
+ it "should pass the identifier to the storage engine" do
284
+ @storage.should_receive(:retrieve!).with('monkey.txt').and_return(@base_stored_file)
285
+ @thumb_storage.should_receive(:retrieve!).with('monkey.txt').and_return(@thumb_stored_file)
286
+ @uploader.retrieve_from_store!('monkey.txt')
287
+ @uploader.file.should == @base_stored_file
288
+ @uploader.thumb.file.should == @thumb_stored_file
289
+ end
290
+
291
+ it "should not set the filename" do
292
+ @uploader.retrieve_from_store!('monkey.txt')
293
+ @uploader.filename.should be_nil
294
+ end
295
+ end
296
+ end
297
+
298
+ end