carrierwave 0.2.1 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of carrierwave might be problematic. Click here for more details.
- data/README.rdoc +35 -20
- data/Rakefile +1 -1
- data/lib/carrierwave.rb +55 -7
- data/lib/carrierwave/compatibility/paperclip.rb +91 -0
- data/lib/carrierwave/core_ext/inheritable_attributes.rb +102 -0
- data/lib/carrierwave/core_ext/module_setup.rb +49 -0
- data/lib/carrierwave/mount.rb +119 -103
- data/lib/carrierwave/orm/activerecord.rb +6 -1
- data/lib/carrierwave/orm/sequel.rb +15 -2
- data/lib/carrierwave/processing/rmagick.rb +8 -7
- data/lib/carrierwave/storage/abstract.rb +16 -1
- data/lib/carrierwave/storage/file.rb +20 -1
- data/lib/carrierwave/uploader.rb +31 -593
- data/lib/carrierwave/uploader/cache.rb +114 -0
- data/lib/carrierwave/uploader/callbacks.rb +40 -0
- data/lib/carrierwave/uploader/default_path.rb +21 -0
- data/lib/carrierwave/uploader/extension_whitelist.rb +35 -0
- data/lib/carrierwave/uploader/mountable.rb +37 -0
- data/lib/carrierwave/uploader/paths.rb +25 -0
- data/lib/carrierwave/uploader/processing.rb +79 -0
- data/lib/carrierwave/uploader/proxy.rb +60 -0
- data/lib/carrierwave/uploader/remove.rb +21 -0
- data/lib/carrierwave/uploader/store.rb +154 -0
- data/lib/carrierwave/uploader/url.rb +22 -0
- data/lib/carrierwave/uploader/versions.rb +145 -0
- data/lib/generators/uploader_generator.rb +1 -1
- data/rails_generators/uploader/templates/uploader.rb +12 -8
- data/spec/compatibility/paperclip_spec.rb +41 -0
- data/spec/mount_spec.rb +88 -25
- data/spec/orm/activerecord_spec.rb +7 -9
- data/spec/orm/datamapper_spec.rb +7 -9
- data/spec/orm/sequel_spec.rb +47 -32
- data/spec/spec_helper.rb +13 -0
- data/spec/test.log +1717 -0
- data/spec/uploader/cache_spec.rb +194 -0
- data/spec/uploader/default_path_spec.rb +66 -0
- data/spec/uploader/extension_whitelist_spec.rb +42 -0
- data/spec/uploader/mountable_spec.rb +31 -0
- data/spec/uploader/paths_spec.rb +20 -0
- data/spec/uploader/processing_spec.rb +60 -0
- data/spec/uploader/proxy_spec.rb +52 -0
- data/spec/uploader/remove_spec.rb +65 -0
- data/spec/uploader/store_spec.rb +260 -0
- data/spec/uploader/url_spec.rb +85 -0
- data/spec/uploader/versions_spec.rb +275 -0
- metadata +30 -3
- data/spec/uploader_spec.rb +0 -887
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe CarrierWave::Uploader do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@uploader_class = Class.new(CarrierWave::Uploader::Base)
|
7
|
+
@uploader = @uploader_class.new
|
8
|
+
end
|
9
|
+
|
10
|
+
after do
|
11
|
+
FileUtils.rm_rf(public_path)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#remove!' do
|
15
|
+
before do
|
16
|
+
@file = File.open(file_path('test.jpg'))
|
17
|
+
|
18
|
+
@stored_file = mock('a stored file')
|
19
|
+
@stored_file.stub!(:path).and_return('/path/to/somewhere')
|
20
|
+
@stored_file.stub!(:url).and_return('http://www.example.com')
|
21
|
+
@stored_file.stub!(:identifier).and_return('this-is-me')
|
22
|
+
|
23
|
+
@uploader_class.storage.stub!(:store!).and_return(@stored_file)
|
24
|
+
@uploader_class.storage.stub!(:destroy!)
|
25
|
+
@uploader.store!(@file)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should reset the current path" do
|
29
|
+
@uploader.remove!
|
30
|
+
@uploader.current_path.should be_nil
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should not be cached" do
|
34
|
+
@uploader.remove!
|
35
|
+
@uploader.should_not be_cached
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should reset the url" do
|
39
|
+
@uploader.cache!(@file)
|
40
|
+
@uploader.remove!
|
41
|
+
@uploader.url.should be_nil
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should reset the identifier" do
|
45
|
+
@uploader.remove!
|
46
|
+
@uploader.identifier.should be_nil
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should instruct the storage engine to remove the file" do
|
50
|
+
@uploader_class.storage.should_receive(:destroy!).with(@uploader, @uploader.file)
|
51
|
+
@uploader.remove!
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should reset the cache_name" do
|
55
|
+
@uploader.cache!(@file)
|
56
|
+
@uploader.remove!
|
57
|
+
@uploader.cache_name.should be_nil
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should do nothing when trying to remove an empty file" do
|
61
|
+
running { @uploader.remove! }.should_not raise_error
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,260 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe CarrierWave::Uploader do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@uploader_class = Class.new(CarrierWave::Uploader::Base)
|
7
|
+
@uploader = @uploader_class.new
|
8
|
+
end
|
9
|
+
|
10
|
+
after do
|
11
|
+
FileUtils.rm_rf(public_path)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe ".storage" do
|
15
|
+
before do
|
16
|
+
CarrierWave::Storage::File.stub!(:setup!)
|
17
|
+
CarrierWave::Storage::S3.stub!(:setup!)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should set the storage if an argument is given" do
|
21
|
+
storage = mock('some kind of storage')
|
22
|
+
storage.should_receive(:setup!)
|
23
|
+
@uploader_class.storage storage
|
24
|
+
@uploader_class.storage.should == storage
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should default to file" do
|
28
|
+
@uploader_class.storage.should == CarrierWave::Storage::File
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should set the storage from the configured shortcuts if a symbol is given" do
|
32
|
+
@uploader_class.storage :file
|
33
|
+
@uploader_class.storage.should == CarrierWave::Storage::File
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should remember the storage when inherited" do
|
37
|
+
@uploader_class.storage :s3
|
38
|
+
subclass = Class.new(@uploader_class)
|
39
|
+
subclass.storage.should == CarrierWave::Storage::S3
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should be changeable when inherited" do
|
43
|
+
@uploader_class.storage :s3
|
44
|
+
subclass = Class.new(@uploader_class)
|
45
|
+
subclass.storage.should == CarrierWave::Storage::S3
|
46
|
+
subclass.storage :file
|
47
|
+
subclass.storage.should == CarrierWave::Storage::File
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#store_dir' do
|
52
|
+
it "should default to the config option" do
|
53
|
+
@uploader.store_dir.should == 'uploads'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#filename' do
|
58
|
+
it "should default to nil" do
|
59
|
+
@uploader.filename.should be_nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe '#store!' do
|
64
|
+
before do
|
65
|
+
@file = File.open(file_path('test.jpg'))
|
66
|
+
|
67
|
+
@stored_file = mock('a stored file')
|
68
|
+
@stored_file.stub!(:path).and_return('/path/to/somewhere')
|
69
|
+
@stored_file.stub!(:url).and_return('http://www.example.com')
|
70
|
+
@stored_file.stub!(:identifier).and_return('this-is-me')
|
71
|
+
|
72
|
+
@uploader_class.storage.stub!(:store!).and_return(@stored_file)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should set the current path" do
|
76
|
+
@uploader.store!(@file)
|
77
|
+
@uploader.current_path.should == '/path/to/somewhere'
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should not be cached" do
|
81
|
+
@uploader.store!(@file)
|
82
|
+
@uploader.should_not be_cached
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should set the url" do
|
86
|
+
@uploader.store!(@file)
|
87
|
+
@uploader.url.should == 'http://www.example.com'
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should set the identifier" do
|
91
|
+
@uploader.store!(@file)
|
92
|
+
@uploader.identifier.should == 'this-is-me'
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should, if a file is given as argument, cache that file" do
|
96
|
+
@uploader.should_receive(:cache!).with(@file)
|
97
|
+
@uploader.store!(@file)
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should use a previously cached file if no argument is given" do
|
101
|
+
@uploader.cache!(File.open(file_path('test.jpg')))
|
102
|
+
@uploader.should_not_receive(:cache!)
|
103
|
+
@uploader.store!
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should instruct the storage engine to store the file" do
|
107
|
+
@uploader.cache!(@file)
|
108
|
+
@uploader_class.storage.should_receive(:store!).with(@uploader, @uploader.file).and_return(:monkey)
|
109
|
+
@uploader.store!
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should reset the cache_name" do
|
113
|
+
@uploader.cache!(@file)
|
114
|
+
@uploader.store!
|
115
|
+
@uploader.cache_name.should be_nil
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should cache the result given by the storage engine" do
|
119
|
+
@uploader.store!(@file)
|
120
|
+
@uploader.file.should == @stored_file
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should do nothing when trying to store an empty file" do
|
124
|
+
@uploader.store!(nil)
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should not re-store a retrieved file" do
|
128
|
+
@stored_file = mock('a stored file')
|
129
|
+
@uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
|
130
|
+
|
131
|
+
@uploader_class.storage.should_not_receive(:store!)
|
132
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
133
|
+
@uploader.store!
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe '#retrieve_from_store!' do
|
138
|
+
before do
|
139
|
+
@stored_file = mock('a stored file')
|
140
|
+
@stored_file.stub!(:path).and_return('/path/to/somewhere')
|
141
|
+
@stored_file.stub!(:url).and_return('http://www.example.com')
|
142
|
+
@stored_file.stub!(:identifier).and_return('this-is-me')
|
143
|
+
|
144
|
+
@uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should set the current path" do
|
148
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
149
|
+
@uploader.current_path.should == '/path/to/somewhere'
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should not be cached" do
|
153
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
154
|
+
@uploader.should_not be_cached
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should set the url" do
|
158
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
159
|
+
@uploader.url.should == 'http://www.example.com'
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should set the identifier" do
|
163
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
164
|
+
@uploader.identifier.should == 'this-is-me'
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should instruct the storage engine to retrieve the file and store the result" do
|
168
|
+
@uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
|
169
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
170
|
+
@uploader.file.should == @stored_file
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should overwrite a file that has already been cached" do
|
174
|
+
@uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
|
175
|
+
@uploader.retrieve_from_store!('bork.txt')
|
176
|
+
@uploader.file.should == @stored_file
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
describe 'with an overridden, reversing, filename' do
|
181
|
+
before do
|
182
|
+
@uploader_class.class_eval do
|
183
|
+
def filename
|
184
|
+
super.reverse unless super.blank?
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
describe '#store!' do
|
190
|
+
before do
|
191
|
+
@file = File.open(file_path('test.jpg'))
|
192
|
+
|
193
|
+
@stored_file = mock('a stored file')
|
194
|
+
@stored_file.stub!(:path).and_return('/path/to/somewhere')
|
195
|
+
@stored_file.stub!(:url).and_return('http://www.example.com')
|
196
|
+
|
197
|
+
@uploader_class.storage.stub!(:store!).and_return(@stored_file)
|
198
|
+
end
|
199
|
+
|
200
|
+
after do
|
201
|
+
CarrierWave.config[:use_cache] = true
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should set the current path" do
|
205
|
+
@uploader.store!(@file)
|
206
|
+
@uploader.current_path.should == '/path/to/somewhere'
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should set the url" do
|
210
|
+
@uploader.store!(@file)
|
211
|
+
@uploader.url.should == 'http://www.example.com'
|
212
|
+
end
|
213
|
+
|
214
|
+
it "should, if a file is given as argument, reverse the filename" do
|
215
|
+
@uploader.store!(@file)
|
216
|
+
@uploader.filename.should == 'gpj.tset'
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should, if a files is given as an argument and use_cache is false, reverse the filename" do
|
220
|
+
CarrierWave.config[:use_cache] = false
|
221
|
+
@uploader.store!(@file)
|
222
|
+
@uploader.filename.should == 'gpj.tset'
|
223
|
+
end
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
describe '#retrieve_from_store!' do
|
228
|
+
before do
|
229
|
+
@stored_file = mock('a stored file')
|
230
|
+
@stored_file.stub!(:path).and_return('/path/to/somewhere')
|
231
|
+
@stored_file.stub!(:url).and_return('http://www.example.com')
|
232
|
+
|
233
|
+
@uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should set the current path" do
|
237
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
238
|
+
@uploader.current_path.should == '/path/to/somewhere'
|
239
|
+
end
|
240
|
+
|
241
|
+
it "should set the url" do
|
242
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
243
|
+
@uploader.url.should == 'http://www.example.com'
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should pass the identifier to the storage engine" do
|
247
|
+
@uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
|
248
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
249
|
+
@uploader.file.should == @stored_file
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should not set the filename" do
|
253
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
254
|
+
@uploader.filename.should be_nil
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
end
|
259
|
+
|
260
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe CarrierWave::Uploader do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@uploader_class = Class.new(CarrierWave::Uploader::Base)
|
7
|
+
@uploader = @uploader_class.new
|
8
|
+
end
|
9
|
+
|
10
|
+
after do
|
11
|
+
FileUtils.rm_rf(public_path)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#url' do
|
15
|
+
before do
|
16
|
+
CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should default to nil" do
|
20
|
+
@uploader.url.should be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should raise ArgumentError when version doesn't exist" do
|
24
|
+
lambda { @uploader.url(:thumb) }.should raise_error ArgumentError
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should not raise ArgumentError when versions version exists" do
|
28
|
+
@uploader_class.version(:thumb)
|
29
|
+
lambda { @uploader.url(:thumb) }.should_not raise_error ArgumentError
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should get the directory relative to public, prepending a slash" do
|
33
|
+
@uploader.cache!(File.open(file_path('test.jpg')))
|
34
|
+
@uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should get the directory relative to public for a specific version" do
|
38
|
+
@uploader_class.version(:thumb)
|
39
|
+
@uploader.cache!(File.open(file_path('test.jpg')))
|
40
|
+
@uploader.url(:thumb).should == '/uploads/tmp/20071201-1234-345-2255/thumb_test.jpg'
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should get the directory relative to public for a nested version" do
|
44
|
+
@uploader_class.version(:thumb) do
|
45
|
+
version(:mini)
|
46
|
+
end
|
47
|
+
@uploader.cache!(File.open(file_path('test.jpg')))
|
48
|
+
@uploader.url(:thumb, :mini).should == '/uploads/tmp/20071201-1234-345-2255/thumb_mini_test.jpg'
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return file#url if available" do
|
52
|
+
@uploader.cache!(File.open(file_path('test.jpg')))
|
53
|
+
@uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
|
54
|
+
@uploader.url.should == 'http://www.example.com/someurl.jpg'
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should get the directory relative to public, if file#url is blank" do
|
58
|
+
@uploader.cache!(File.open(file_path('test.jpg')))
|
59
|
+
@uploader.file.stub!(:url).and_return('')
|
60
|
+
@uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#to_s' do
|
65
|
+
before do
|
66
|
+
CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should default to nil" do
|
70
|
+
@uploader.to_s.should be_nil
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should get the directory relative to public, prepending a slash" do
|
74
|
+
@uploader.cache!(File.open(file_path('test.jpg')))
|
75
|
+
@uploader.to_s.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should return file#url if available" do
|
79
|
+
@uploader.cache!(File.open(file_path('test.jpg')))
|
80
|
+
@uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
|
81
|
+
@uploader.to_s.should == 'http://www.example.com/someurl.jpg'
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -0,0 +1,275 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe CarrierWave::Uploader do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@uploader_class = Class.new(CarrierWave::Uploader::Base)
|
7
|
+
@uploader = @uploader_class.new
|
8
|
+
end
|
9
|
+
|
10
|
+
after do
|
11
|
+
FileUtils.rm_rf(public_path)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '.version' do
|
15
|
+
it "should add it to .versions" do
|
16
|
+
@uploader_class.version :thumb
|
17
|
+
@uploader_class.versions[:thumb].should be_a(Class)
|
18
|
+
@uploader_class.versions[:thumb].ancestors.should include(@uploader_class)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should add an accessor which returns the version" do
|
22
|
+
@uploader_class.version :thumb
|
23
|
+
@uploader.thumb.should be_a(@uploader_class)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should add it to #versions which returns the version" do
|
27
|
+
@uploader_class.version :thumb
|
28
|
+
@uploader.versions[:thumb].should be_a(@uploader_class)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should set the version name" do
|
32
|
+
@uploader_class.version :thumb
|
33
|
+
@uploader.version_name.should == nil
|
34
|
+
@uploader.thumb.version_name.should == :thumb
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should set the version names on the class" do
|
38
|
+
@uploader_class.version :thumb
|
39
|
+
@uploader.class.version_names.should == []
|
40
|
+
@uploader.thumb.class.version_names.should == [:thumb]
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should remember mount options" do
|
44
|
+
model = mock('a model')
|
45
|
+
@uploader_class.version :thumb
|
46
|
+
@uploader = @uploader_class.new(model, :gazelle)
|
47
|
+
|
48
|
+
@uploader.thumb.model.should == model
|
49
|
+
@uploader.thumb.mounted_as.should == :gazelle
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should apply any overrides given in a block" do
|
53
|
+
@uploader_class.version :thumb do
|
54
|
+
def store_dir
|
55
|
+
public_path('monkey/apache')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
@uploader.store_dir.should == 'uploads'
|
59
|
+
@uploader.thumb.store_dir.should == public_path('monkey/apache')
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should reopen the same class when called multiple times" do
|
63
|
+
@uploader_class.version :thumb do
|
64
|
+
def self.monkey
|
65
|
+
"monkey"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
@uploader_class.version :thumb do
|
69
|
+
def self.llama
|
70
|
+
"llama"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
@uploader_class.version(:thumb).monkey.should == "monkey"
|
74
|
+
@uploader_class.version(:thumb).llama.should == "llama"
|
75
|
+
end
|
76
|
+
|
77
|
+
describe 'with nested versions' do
|
78
|
+
before do
|
79
|
+
@uploader_class.version :thumb do
|
80
|
+
version :mini
|
81
|
+
version :micro
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should add an array of version names" do
|
86
|
+
@uploader.class.version_names.should == []
|
87
|
+
@uploader.thumb.class.version_names.should == [:thumb]
|
88
|
+
@uploader.thumb.mini.class.version_names.should == [:thumb, :mini]
|
89
|
+
@uploader.thumb.micro.class.version_names.should == [:thumb, :micro]
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should set the version name for the instances" do
|
93
|
+
@uploader.version_name.should be_nil
|
94
|
+
@uploader.thumb.version_name.should == :thumb
|
95
|
+
@uploader.thumb.mini.version_name.should == :thumb_mini
|
96
|
+
@uploader.thumb.micro.version_name.should == :thumb_micro
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
describe 'with a version' do
|
104
|
+
before do
|
105
|
+
@uploader_class.version(:thumb)
|
106
|
+
end
|
107
|
+
|
108
|
+
describe '#cache!' do
|
109
|
+
|
110
|
+
before do
|
111
|
+
CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should set store_path with versions" do
|
115
|
+
@uploader.cache!(File.open(file_path('test.jpg')))
|
116
|
+
@uploader.store_path.should == 'uploads/test.jpg'
|
117
|
+
@uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
|
118
|
+
@uploader.thumb.store_path('kebab.png').should == 'uploads/thumb_kebab.png'
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should move it to the tmp dir with the filename prefixed" do
|
122
|
+
@uploader.cache!(File.open(file_path('test.jpg')))
|
123
|
+
@uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
|
124
|
+
@uploader.thumb.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/thumb_test.jpg')
|
125
|
+
@uploader.file.exists?.should be_true
|
126
|
+
@uploader.thumb.file.exists?.should be_true
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe '#retrieve_from_cache!' do
|
131
|
+
it "should set the path to the tmp dir" do
|
132
|
+
@uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
|
133
|
+
@uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
|
134
|
+
@uploader.thumb.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/thumb_test.jpg')
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should set store_path with versions" do
|
138
|
+
@uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
|
139
|
+
@uploader.store_path.should == 'uploads/test.jpg'
|
140
|
+
@uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
|
141
|
+
@uploader.thumb.store_path('kebab.png').should == 'uploads/thumb_kebab.png'
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe '#store!' do
|
146
|
+
before do
|
147
|
+
@uploader_class.storage = mock_storage('base')
|
148
|
+
@uploader_class.version(:thumb).storage = mock_storage('thumb')
|
149
|
+
|
150
|
+
@file = File.open(file_path('test.jpg'))
|
151
|
+
|
152
|
+
@base_stored_file = mock('a stored file')
|
153
|
+
@base_stored_file.stub!(:path).and_return('/path/to/somewhere')
|
154
|
+
@base_stored_file.stub!(:url).and_return('http://www.example.com')
|
155
|
+
|
156
|
+
@thumb_stored_file = mock('a thumb version of a stored file')
|
157
|
+
@thumb_stored_file.stub!(:path).and_return('/path/to/somewhere/thumb')
|
158
|
+
@thumb_stored_file.stub!(:url).and_return('http://www.example.com/thumb')
|
159
|
+
|
160
|
+
@uploader_class.storage.stub!(:store!).and_return(@base_stored_file)
|
161
|
+
@uploader_class.version(:thumb).storage.stub!(:store!).and_return(@thumb_stored_file)
|
162
|
+
end
|
163
|
+
|
164
|
+
after do
|
165
|
+
CarrierWave.config[:use_cache] = true
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should set the current path for the version" do
|
169
|
+
@uploader.store!(@file)
|
170
|
+
@uploader.current_path.should == '/path/to/somewhere'
|
171
|
+
@uploader.thumb.current_path.should == '/path/to/somewhere/thumb'
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should set the url" do
|
175
|
+
@uploader.store!(@file)
|
176
|
+
@uploader.url.should == 'http://www.example.com'
|
177
|
+
@uploader.thumb.url.should == 'http://www.example.com/thumb'
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should, if a file is given as argument, set the store_path" do
|
181
|
+
@uploader.store!(@file)
|
182
|
+
@uploader.store_path.should == 'uploads/test.jpg'
|
183
|
+
@uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
|
184
|
+
@uploader.thumb.store_path('kebab.png').should == 'uploads/thumb_kebab.png'
|
185
|
+
end
|
186
|
+
|
187
|
+
it "should instruct the storage engine to store the file and its version" do
|
188
|
+
@uploader.cache!(@file)
|
189
|
+
@uploader_class.storage.should_receive(:store!).with(@uploader, @uploader.file).and_return(:monkey)
|
190
|
+
@uploader_class.version(:thumb).storage.should_receive(:store!).with(@uploader.thumb, @uploader.thumb.file).and_return(:gorilla)
|
191
|
+
@uploader.store!
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
describe '#remove!' do
|
197
|
+
before do
|
198
|
+
@uploader_class.storage = mock_storage('base')
|
199
|
+
@uploader_class.version(:thumb).storage = mock_storage('thumb')
|
200
|
+
|
201
|
+
@file = File.open(file_path('test.jpg'))
|
202
|
+
|
203
|
+
@base_stored_file = mock('a stored file')
|
204
|
+
@thumb_stored_file = mock('a thumb version of a stored file')
|
205
|
+
|
206
|
+
@uploader_class.storage.stub!(:store!).and_return(@base_stored_file)
|
207
|
+
@uploader_class.version(:thumb).storage.stub!(:store!).and_return(@thumb_stored_file)
|
208
|
+
|
209
|
+
@uploader_class.storage.stub!(:store!).and_return(@base_stored_file)
|
210
|
+
@uploader_class.version(:thumb).storage.stub!(:store!).and_return(@thumb_stored_file)
|
211
|
+
|
212
|
+
@uploader_class.storage.stub!(:destroy!)
|
213
|
+
@uploader_class.version(:thumb).storage.stub!(:destroy!)
|
214
|
+
|
215
|
+
@uploader.store!(@file)
|
216
|
+
end
|
217
|
+
|
218
|
+
after do
|
219
|
+
CarrierWave.config[:use_cache] = true
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should reset the current path for the version" do
|
223
|
+
@uploader.remove!
|
224
|
+
@uploader.current_path.should be_nil
|
225
|
+
@uploader.thumb.current_path.should be_nil
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should reset the url" do
|
229
|
+
@uploader.remove!
|
230
|
+
@uploader.url.should be_nil
|
231
|
+
@uploader.thumb.url.should be_nil
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should instruct the storage engine to remove the file and its versions" do
|
235
|
+
@uploader_class.storage.should_receive(:destroy!).with(@uploader, @uploader.file)
|
236
|
+
@uploader_class.version(:thumb).storage.should_receive(:destroy!).with(@uploader.thumb, @uploader.thumb.file)
|
237
|
+
@uploader.remove!
|
238
|
+
end
|
239
|
+
|
240
|
+
end
|
241
|
+
|
242
|
+
|
243
|
+
describe '#retrieve_from_store!' do
|
244
|
+
before do
|
245
|
+
@stored_file = mock('a stored file')
|
246
|
+
@stored_file.stub!(:path).and_return('/path/to/somewhere')
|
247
|
+
@stored_file.stub!(:url).and_return('http://www.example.com')
|
248
|
+
|
249
|
+
@uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should set the current path" do
|
253
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
254
|
+
@uploader.current_path.should == '/path/to/somewhere'
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should set the url" do
|
258
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
259
|
+
@uploader.url.should == 'http://www.example.com'
|
260
|
+
end
|
261
|
+
|
262
|
+
it "should pass the identifier to the storage engine" do
|
263
|
+
@uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
|
264
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
265
|
+
@uploader.file.should == @stored_file
|
266
|
+
end
|
267
|
+
|
268
|
+
it "should not set the filename" do
|
269
|
+
@uploader.retrieve_from_store!('monkey.txt')
|
270
|
+
@uploader.filename.should be_nil
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
end
|