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,22 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
module Uploader
|
3
|
+
module Url
|
4
|
+
|
5
|
+
##
|
6
|
+
# === Returns
|
7
|
+
#
|
8
|
+
# [String] the location where this file is accessible via a url
|
9
|
+
#
|
10
|
+
def url
|
11
|
+
if file.respond_to?(:url) and not file.url.blank?
|
12
|
+
file.url
|
13
|
+
elsif current_path
|
14
|
+
File.expand_path(current_path).gsub(File.expand_path(public), '')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
alias_method :to_s, :url
|
19
|
+
|
20
|
+
end # Url
|
21
|
+
end # Uploader
|
22
|
+
end # CarrierWave
|
@@ -0,0 +1,145 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
module Uploader
|
3
|
+
module Versions
|
4
|
+
|
5
|
+
setup do
|
6
|
+
after :cache, :cache_versions!
|
7
|
+
after :store, :store_versions!
|
8
|
+
after :remove, :remove_versions!
|
9
|
+
after :retrieve_from_cache, :retrieve_versions_from_cache!
|
10
|
+
after :retrieve_from_store, :retrieve_versions_from_store!
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
def version_names
|
16
|
+
@version_names ||= []
|
17
|
+
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Adds a new version to this uploader
|
21
|
+
#
|
22
|
+
# === Parameters
|
23
|
+
#
|
24
|
+
# [name (#to_sym)] name of the version
|
25
|
+
# [&block (Proc)] a block to eval on this version of the uploader
|
26
|
+
#
|
27
|
+
def version(name, &block)
|
28
|
+
name = name.to_sym
|
29
|
+
unless versions[name]
|
30
|
+
versions[name] = Class.new(self)
|
31
|
+
versions[name].version_names.push(*version_names)
|
32
|
+
versions[name].version_names.push(name)
|
33
|
+
class_eval <<-RUBY
|
34
|
+
def #{name}
|
35
|
+
versions[:#{name}]
|
36
|
+
end
|
37
|
+
RUBY
|
38
|
+
end
|
39
|
+
versions[name].class_eval(&block) if block
|
40
|
+
versions[name]
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# === Returns
|
45
|
+
#
|
46
|
+
# [Hash{Symbol => Class}] a list of versions available for this uploader
|
47
|
+
#
|
48
|
+
def versions
|
49
|
+
@versions ||= {}
|
50
|
+
end
|
51
|
+
|
52
|
+
end # ClassMethods
|
53
|
+
|
54
|
+
##
|
55
|
+
# Returns a hash mapping the name of each version of the uploader to an instance of it
|
56
|
+
#
|
57
|
+
# === Returns
|
58
|
+
#
|
59
|
+
# [Hash{Symbol => CarrierWave::Uploader}] a list of uploader instances
|
60
|
+
#
|
61
|
+
def versions
|
62
|
+
return @versions if @versions
|
63
|
+
@versions = {}
|
64
|
+
self.class.versions.each do |name, klass|
|
65
|
+
@versions[name] = klass.new(model, mounted_as)
|
66
|
+
end
|
67
|
+
@versions
|
68
|
+
end
|
69
|
+
|
70
|
+
##
|
71
|
+
# === Returns
|
72
|
+
#
|
73
|
+
# [String] the name of this version of the uploader
|
74
|
+
#
|
75
|
+
def version_name
|
76
|
+
self.class.version_names.join('_').to_sym unless self.class.version_names.blank?
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# When given a version name as a parameter, will return the url for that version
|
81
|
+
# This also works with nested versions.
|
82
|
+
#
|
83
|
+
# === Example
|
84
|
+
#
|
85
|
+
# my_uploader.url # => /path/to/my/uploader.gif
|
86
|
+
# my_uploader.url(:thumb) # => /path/to/my/thumb_uploader.gif
|
87
|
+
# my_uploader.url(:thumb, :small) # => /path/to/my/thumb_small_uploader.gif
|
88
|
+
#
|
89
|
+
# === Parameters
|
90
|
+
#
|
91
|
+
# [*args (Symbol)] any number of versions
|
92
|
+
#
|
93
|
+
# === Returns
|
94
|
+
#
|
95
|
+
# [String] the location where this file is accessible via a url
|
96
|
+
#
|
97
|
+
def url(*args)
|
98
|
+
if(args.first)
|
99
|
+
raise ArgumentError, "Version #{args.first} doesn't exist!" if versions[args.first.to_sym].nil?
|
100
|
+
# recursively proxy to version
|
101
|
+
versions[args.first.to_sym].url(*args[1..-1])
|
102
|
+
else
|
103
|
+
super()
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
def full_filename(for_file)
|
110
|
+
[version_name, super(for_file)].compact.join('_')
|
111
|
+
end
|
112
|
+
|
113
|
+
def full_original_filename
|
114
|
+
[version_name, super].compact.join('_')
|
115
|
+
end
|
116
|
+
|
117
|
+
def cache_versions!(new_file)
|
118
|
+
versions.each do |name, v|
|
119
|
+
v.send(:cache_id=, cache_id)
|
120
|
+
v.cache!(new_file)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def store_versions!(new_file)
|
125
|
+
versions.each { |name, v| v.store!(new_file) }
|
126
|
+
end
|
127
|
+
|
128
|
+
def remove_versions!
|
129
|
+
versions.each do |name, v|
|
130
|
+
CarrierWave.logger.info "CarrierWave: removing file for version #{v.version_name}"
|
131
|
+
v.remove!
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def retrieve_versions_from_cache!(cache_name)
|
136
|
+
versions.each { |name, v| v.retrieve_from_cache!(cache_name) }
|
137
|
+
end
|
138
|
+
|
139
|
+
def retrieve_versions_from_store!(identifier)
|
140
|
+
versions.each { |name, v| v.retrieve_from_store!(identifier) }
|
141
|
+
end
|
142
|
+
|
143
|
+
end # Versions
|
144
|
+
end # Uploader
|
145
|
+
end # CarrierWave
|
@@ -1,6 +1,4 @@
|
|
1
|
-
class <%= class_name %>Uploader
|
2
|
-
|
3
|
-
include CarrierWave::Uploader
|
1
|
+
class <%= class_name %>Uploader < CarrierWave::Uploader::Base
|
4
2
|
|
5
3
|
# Include RMagick or ImageScience support
|
6
4
|
# include CarrierWave::RMagick
|
@@ -10,6 +8,17 @@ class <%= class_name %>Uploader
|
|
10
8
|
storage :file
|
11
9
|
# storage :s3
|
12
10
|
|
11
|
+
# Override the directory where uploaded files will be stored
|
12
|
+
# This is a sensible default for uploaders that are meant to be mounted:
|
13
|
+
def store_dir
|
14
|
+
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
|
15
|
+
end
|
16
|
+
|
17
|
+
# Provide a default path as a default if there hasn't been a file uploaded
|
18
|
+
# def default_path
|
19
|
+
# "images/fallback/" + [version_name, "default.png"].compact.join('_')
|
20
|
+
# end
|
21
|
+
|
13
22
|
# Process files as they are uploaded.
|
14
23
|
# process :scale => [200, 300]
|
15
24
|
#
|
@@ -33,9 +42,4 @@ class <%= class_name %>Uploader
|
|
33
42
|
# "something.jpg"
|
34
43
|
# end
|
35
44
|
|
36
|
-
# Override the directory where uploaded files will be stored
|
37
|
-
# This is a sensible default for uploaders that are meant to be mounted:
|
38
|
-
def store_dir
|
39
|
-
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
|
40
|
-
end
|
41
45
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
require 'carrierwave/orm/activerecord'
|
4
|
+
|
5
|
+
describe CarrierWave::Compatibility::Paperclip do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@uploader_class = Class.new(CarrierWave::Uploader::Base) do
|
9
|
+
include CarrierWave::Compatibility::Paperclip
|
10
|
+
end
|
11
|
+
@model = mock('a model')
|
12
|
+
@model.stub!(:id).and_return(23)
|
13
|
+
@uploader = @uploader_class.new(@model, :monkey)
|
14
|
+
end
|
15
|
+
|
16
|
+
after do
|
17
|
+
FileUtils.rm_rf(public_path)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#store_path' do
|
21
|
+
it "should mimics paperclip default" do
|
22
|
+
@uploader.store_path("monkey.png").should == CarrierWave.config[:root] + "/public/system/monkeys/23/original/monkey.png"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should interpolate the root path" do
|
26
|
+
@uploader.stub!(:paperclip_path).and_return(":rails_root/foo/bar")
|
27
|
+
@uploader.store_path("monkey.png").should == CarrierWave.config[:root] + "/foo/bar"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should interpolate the attachment" do
|
31
|
+
@uploader.stub!(:paperclip_path).and_return("/foo/:attachment/bar")
|
32
|
+
@uploader.store_path("monkey.png").should == "/foo/monkeys/bar"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should interpolate the id" do
|
36
|
+
@uploader.stub!(:paperclip_path).and_return("/foo/:id/bar")
|
37
|
+
@uploader.store_path("monkey.png").should == "/foo/23/bar"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/spec/mount_spec.rb
CHANGED
@@ -12,13 +12,29 @@ describe CarrierWave::Mount do
|
|
12
12
|
@class = Class.new
|
13
13
|
@class.send(:extend, CarrierWave::Mount)
|
14
14
|
|
15
|
-
@uploader = Class.new
|
16
|
-
include CarrierWave::Uploader
|
17
|
-
end
|
15
|
+
@uploader = Class.new(CarrierWave::Uploader::Base)
|
18
16
|
|
19
17
|
@class.mount_uploader(:image, @uploader)
|
20
18
|
@instance = @class.new
|
21
19
|
end
|
20
|
+
|
21
|
+
it "should maintain the ability to super" do
|
22
|
+
pending "I can't make this work with datamapper" do
|
23
|
+
@class.class_eval do
|
24
|
+
def image_uploader
|
25
|
+
super
|
26
|
+
end
|
27
|
+
|
28
|
+
def image=(val)
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
@instance.image_uploader.should be_an_instance_of(@uploader)
|
34
|
+
@instance.image = stub_file('test.jpg')
|
35
|
+
@instance.image.should be_an_instance_of(@uploader)
|
36
|
+
end
|
37
|
+
end
|
22
38
|
|
23
39
|
describe '#image_uploader' do
|
24
40
|
it "should return the uploader" do
|
@@ -45,14 +61,16 @@ describe CarrierWave::Mount do
|
|
45
61
|
|
46
62
|
describe '#image' do
|
47
63
|
|
48
|
-
it "should return
|
49
|
-
@instance.should_receive(:read_uploader).with(:image).and_return(nil)
|
50
|
-
@instance.image.should
|
64
|
+
it "should return a blank uploader when nothing has been assigned" do
|
65
|
+
@instance.should_receive(:read_uploader).with(:image).twice.and_return(nil)
|
66
|
+
@instance.image.should be_an_instance_of(@uploader)
|
67
|
+
@instance.image.should be_blank
|
51
68
|
end
|
52
69
|
|
53
|
-
it "should return
|
54
|
-
@instance.should_receive(:read_uploader).with(:image).and_return('')
|
55
|
-
@instance.image.should
|
70
|
+
it "should return a blank uploader when an empty string has been assigned" do
|
71
|
+
@instance.should_receive(:read_uploader).with(:image).twice.and_return('')
|
72
|
+
@instance.image.should be_an_instance_of(@uploader)
|
73
|
+
@instance.image.should be_blank
|
56
74
|
end
|
57
75
|
|
58
76
|
it "should retrieve a file from the storage if a value is stored in the database" do
|
@@ -96,7 +114,7 @@ describe CarrierWave::Mount do
|
|
96
114
|
end
|
97
115
|
end
|
98
116
|
@instance.image = stub_file('test.jpg')
|
99
|
-
@instance.image.should
|
117
|
+
@instance.image.should be_blank
|
100
118
|
end
|
101
119
|
|
102
120
|
it "should fail silently if the image fails to be processed" do
|
@@ -111,6 +129,25 @@ describe CarrierWave::Mount do
|
|
111
129
|
|
112
130
|
end
|
113
131
|
|
132
|
+
describe '#image?' do
|
133
|
+
|
134
|
+
it "should be false when nothing has been assigned" do
|
135
|
+
@instance.should_receive(:read_uploader).with(:image).and_return(nil)
|
136
|
+
@instance.image?.should be_false
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should be false when an empty string has been assigned" do
|
140
|
+
@instance.should_receive(:read_uploader).with(:image).and_return('')
|
141
|
+
@instance.image?.should be_false
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should be true when a file has been cached" do
|
145
|
+
@instance.image = stub_file('test.jpg')
|
146
|
+
@instance.image?.should be_true
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
|
114
151
|
describe '#image_url' do
|
115
152
|
|
116
153
|
it "should return nil when nothing has been assigned" do
|
@@ -175,12 +212,12 @@ describe CarrierWave::Mount do
|
|
175
212
|
|
176
213
|
it "should do nothing when nil is assigned" do
|
177
214
|
@instance.image_cache = nil
|
178
|
-
@instance.image.should
|
215
|
+
@instance.image.should be_blank
|
179
216
|
end
|
180
217
|
|
181
218
|
it "should do nothing when an empty string is assigned" do
|
182
219
|
@instance.image_cache = ''
|
183
|
-
@instance.image.should
|
220
|
+
@instance.image.should be_blank
|
184
221
|
end
|
185
222
|
|
186
223
|
it "retrieve from cache when a cache name is assigned" do
|
@@ -204,7 +241,7 @@ describe CarrierWave::Mount do
|
|
204
241
|
|
205
242
|
it "should do nothing when no file has been uploaded" do
|
206
243
|
@instance.store_image!
|
207
|
-
@instance.image.should
|
244
|
+
@instance.image.should be_blank
|
208
245
|
end
|
209
246
|
|
210
247
|
it "store an assigned file" do
|
@@ -220,10 +257,34 @@ describe CarrierWave::Mount do
|
|
220
257
|
end
|
221
258
|
|
222
259
|
it "should remove an uploaded file when remove_image? returns true" do
|
260
|
+
@instance.should_receive(:write_uploader).with(:image, "")
|
223
261
|
@instance.image = stub_file('test.jpg')
|
262
|
+
path = @instance.image.current_path
|
224
263
|
@instance.remove_image = true
|
225
264
|
@instance.store_image!
|
226
|
-
@instance.image.should
|
265
|
+
@instance.image.should be_blank
|
266
|
+
File.exist?(path).should be_false
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
describe '#remove_image!' do
|
271
|
+
|
272
|
+
before do
|
273
|
+
@instance.stub!(:write_uploader)
|
274
|
+
@instance.stub!(:read_uploader).and_return(nil)
|
275
|
+
end
|
276
|
+
|
277
|
+
it "should do nothing when no file has been uploaded" do
|
278
|
+
@instance.remove_image!
|
279
|
+
@instance.image.should be_blank
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should remove an uploaded file" do
|
283
|
+
@instance.image = stub_file('test.jpg')
|
284
|
+
path = @instance.image.current_path
|
285
|
+
@instance.remove_image!
|
286
|
+
@instance.image.should be_blank
|
287
|
+
File.exist?(path).should be_false
|
227
288
|
end
|
228
289
|
end
|
229
290
|
|
@@ -331,8 +392,8 @@ describe CarrierWave::Mount do
|
|
331
392
|
@instance.stub!(:read_uploader).and_return('test.jpg')
|
332
393
|
end
|
333
394
|
|
334
|
-
it "should return an instance of a subclass of CarrierWave::Uploader" do
|
335
|
-
@instance.image.should be_a(CarrierWave::Uploader)
|
395
|
+
it "should return an instance of a subclass of CarrierWave::Uploader::Base" do
|
396
|
+
@instance.image.should be_a(CarrierWave::Uploader::Base)
|
336
397
|
end
|
337
398
|
|
338
399
|
it "should set the path to the store dir" do
|
@@ -353,9 +414,7 @@ describe CarrierWave::Mount do
|
|
353
414
|
@class = Class.new
|
354
415
|
@class.send(:extend, CarrierWave::Mount)
|
355
416
|
|
356
|
-
@uploader = Class.new
|
357
|
-
include CarrierWave::Uploader
|
358
|
-
end
|
417
|
+
@uploader = Class.new(CarrierWave::Uploader::Base)
|
359
418
|
|
360
419
|
@class.mount_uploader(:image, @uploader, :ignore_integrity_errors => false)
|
361
420
|
@instance = @class.new
|
@@ -380,9 +439,7 @@ describe CarrierWave::Mount do
|
|
380
439
|
@class = Class.new
|
381
440
|
@class.send(:extend, CarrierWave::Mount)
|
382
441
|
|
383
|
-
@uploader = Class.new
|
384
|
-
include CarrierWave::Uploader
|
385
|
-
end
|
442
|
+
@uploader = Class.new(CarrierWave::Uploader::Base)
|
386
443
|
|
387
444
|
@class.mount_uploader(:image, @uploader, :ignore_processing_errors => false)
|
388
445
|
@instance = @class.new
|
@@ -409,9 +466,7 @@ describe CarrierWave::Mount do
|
|
409
466
|
@class = Class.new
|
410
467
|
@class.send(:extend, CarrierWave::Mount)
|
411
468
|
|
412
|
-
@uploader = Class.new
|
413
|
-
include CarrierWave::Uploader
|
414
|
-
end
|
469
|
+
@uploader = Class.new(CarrierWave::Uploader::Base)
|
415
470
|
|
416
471
|
@class.mount_uploader(:image, @uploader, :mount_on => :monkey)
|
417
472
|
@instance = @class.new
|
@@ -433,6 +488,14 @@ describe CarrierWave::Mount do
|
|
433
488
|
@instance.image = stub_file('test.jpg')
|
434
489
|
@instance.store_image!
|
435
490
|
end
|
491
|
+
|
492
|
+
it "should remove from the given column when remove_image is true" do
|
493
|
+
@instance.image = stub_file('test.jpg')
|
494
|
+
@instance.store_image!
|
495
|
+
@instance.remove_image = true
|
496
|
+
@instance.should_receive(:write_uploader).with(:monkey, "")
|
497
|
+
@instance.store_image!
|
498
|
+
end
|
436
499
|
end
|
437
500
|
end
|
438
501
|
|