carrierwave 0.1 → 0.2.0
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/LICENSE +1 -1
- data/{README.md → README.rdoc} +73 -78
- data/Rakefile +12 -6
- data/lib/carrierwave.rb +33 -17
- data/lib/carrierwave/mount.rb +133 -54
- data/lib/carrierwave/orm/activerecord.rb +53 -2
- data/lib/carrierwave/orm/datamapper.rb +7 -2
- data/lib/carrierwave/orm/sequel.rb +23 -0
- data/lib/carrierwave/processing/image_science.rb +3 -3
- data/lib/carrierwave/processing/rmagick.rb +103 -47
- data/lib/carrierwave/sanitized_file.rb +74 -48
- data/lib/carrierwave/storage/abstract.rb +24 -9
- data/lib/carrierwave/storage/file.rb +23 -9
- data/lib/carrierwave/storage/s3.rb +67 -16
- data/lib/carrierwave/test/matchers.rb +112 -0
- data/lib/carrierwave/uploader.rb +264 -113
- data/lib/generators/uploader_generator.rb +7 -7
- data/rails_generators/uploader/templates/uploader.rb +9 -1
- data/spec/mount_spec.rb +147 -3
- data/spec/orm/activerecord_spec.rb +24 -3
- data/spec/orm/datamapper_spec.rb +3 -3
- data/spec/orm/sequel_spec.rb +170 -0
- data/spec/sanitized_file_spec.rb +22 -36
- data/spec/spec_helper.rb +44 -80
- data/spec/uploader_spec.rb +107 -22
- metadata +8 -4
data/LICENSE
CHANGED
data/{README.md → README.rdoc}
RENAMED
@@ -1,10 +1,16 @@
|
|
1
|
-
|
1
|
+
= CarrierWave
|
2
2
|
|
3
3
|
This plugin for Merb and Rails provides a simple and extremely flexible way to upload files.
|
4
4
|
|
5
|
-
|
5
|
+
== Getting Started
|
6
6
|
|
7
|
-
|
7
|
+
Install the latest stable release:
|
8
|
+
|
9
|
+
[sudo] gem install carrierwave
|
10
|
+
|
11
|
+
Or the cutting edge development version:
|
12
|
+
|
13
|
+
[sudo] gem install jnicklas-carrierwave --source http://gems.github.com
|
8
14
|
|
9
15
|
In Merb, add it as a dependency to your config/dependencies.rb:
|
10
16
|
|
@@ -14,7 +20,7 @@ In Rails, add it to your environment.rb:
|
|
14
20
|
|
15
21
|
config.gem "carrierwave"
|
16
22
|
|
17
|
-
|
23
|
+
== Quick Start
|
18
24
|
|
19
25
|
Start off by generating an uploader:
|
20
26
|
|
@@ -30,7 +36,9 @@ this should give you a file in:
|
|
30
36
|
|
31
37
|
Check out this file for some hints on how you can customize your uploader. It should look something like this:
|
32
38
|
|
33
|
-
class AvatarUploader
|
39
|
+
class AvatarUploader
|
40
|
+
include CarrierWave::Uploader
|
41
|
+
|
34
42
|
storage :file
|
35
43
|
end
|
36
44
|
|
@@ -42,42 +50,27 @@ You can use your uploader class to store and retrieve files like this:
|
|
42
50
|
|
43
51
|
uploader.retrieve_from_store!('my_file.png')
|
44
52
|
|
45
|
-
CarrierWave gives you a
|
53
|
+
CarrierWave gives you a +store+ for permanent storage, and a +cache+ for temporary storage. You can use different stores, at the moment a filesystem store and an Amazon S3 store are bundled.
|
46
54
|
|
47
55
|
Most of the time you are going to want to use CarrierWave together with an ORM. It is quite simple to mount uploaders on columns in your model, so you can simply assign files and get going:
|
48
56
|
|
49
|
-
|
57
|
+
=== ActiveRecord, DataMapper, Sequel
|
50
58
|
|
51
|
-
First require the
|
59
|
+
First require the relevant extension:
|
52
60
|
|
53
|
-
require 'carrierwave/orm/activerecord
|
61
|
+
require 'carrierwave/orm/activerecord'
|
62
|
+
require 'carrierwave/orm/datamapper'
|
63
|
+
require 'carrierwave/orm/sequel'
|
54
64
|
|
55
65
|
You don't need to do this if you are using Merb or Rails.
|
56
66
|
|
57
|
-
Open your model file,
|
67
|
+
Open your model file, for ActiveRecord do something like:
|
58
68
|
|
59
69
|
class User < ActiveRecord::Base
|
60
70
|
mount_uploader :avatar, AvatarUploader
|
61
71
|
end
|
62
72
|
|
63
|
-
|
64
|
-
|
65
|
-
u = User.new
|
66
|
-
u.avatar = params[:file]
|
67
|
-
u.avatar = File.open('somewhere')
|
68
|
-
u.save!
|
69
|
-
u.avatar.url # => '/url/to/file.png'
|
70
|
-
u.avatar.current_path # => 'path/to/file.png'
|
71
|
-
|
72
|
-
### DataMapper
|
73
|
-
|
74
|
-
First require the activerecord extension:
|
75
|
-
|
76
|
-
require 'carrierwave/orm/datamapper
|
77
|
-
|
78
|
-
You don't need to do this if you are using Merb or Rails.
|
79
|
-
|
80
|
-
Open your model file, and do something like:
|
73
|
+
Or for DataMapper:
|
81
74
|
|
82
75
|
class User
|
83
76
|
include DataMapper::Resource
|
@@ -85,7 +78,13 @@ Open your model file, and do something like:
|
|
85
78
|
mount_uploader :avatar, AvatarUploader
|
86
79
|
end
|
87
80
|
|
88
|
-
|
81
|
+
Or for Sequel
|
82
|
+
|
83
|
+
class User < Sequel::Model
|
84
|
+
mount_uploader :avatar, AvatarUploader
|
85
|
+
end
|
86
|
+
|
87
|
+
Now you can cache files by assigning them to the attribute, they will automatically be stored when the record is saved.
|
89
88
|
|
90
89
|
u = User.new
|
91
90
|
u.avatar = params[:file]
|
@@ -94,11 +93,13 @@ Now you can upload files!
|
|
94
93
|
u.avatar.url # => '/url/to/file.png'
|
95
94
|
u.avatar.current_path # => 'path/to/file.png'
|
96
95
|
|
97
|
-
|
96
|
+
== Changing the storage directory
|
97
|
+
|
98
|
+
In order to change where uploaded files are put, just override the +store_dir+ method:
|
98
99
|
|
99
|
-
|
100
|
+
class MyUploader
|
101
|
+
include CarrierWave::Uploader
|
100
102
|
|
101
|
-
class MyUploader < CarrierWave::Uploader
|
102
103
|
def store_dir
|
103
104
|
'public/my/upload/directory'
|
104
105
|
end
|
@@ -106,11 +107,12 @@ In order to change where uploaded files are put, just override the `store_dir` m
|
|
106
107
|
|
107
108
|
This works for the file storage as well as Amazon S3.
|
108
109
|
|
109
|
-
|
110
|
+
== Adding versions
|
110
111
|
|
111
112
|
Often you'll want to add different versions of the same file. The classic example is image thumbnails. There is built in support for this:
|
112
113
|
|
113
|
-
class MyUploader
|
114
|
+
class MyUploader
|
115
|
+
include CarrierWave::Uploader
|
114
116
|
include CarrierWave::RMagick
|
115
117
|
|
116
118
|
process :resize => [800, 800]
|
@@ -131,11 +133,23 @@ When this uploader is used, an uploaded image would be scaled to be no larger th
|
|
131
133
|
|
132
134
|
One important thing to remember is that process is called *before* versions are created. This can cut down on processing cost.
|
133
135
|
|
134
|
-
|
136
|
+
It is possible to nest versions within versions:
|
137
|
+
|
138
|
+
class MyUploader
|
139
|
+
include CarrierWave::Uploader
|
140
|
+
|
141
|
+
version :animal do
|
142
|
+
version :human
|
143
|
+
version :monkey
|
144
|
+
version :llama
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
== Making uploads work across form redisplays
|
135
149
|
|
136
150
|
Often you'll notice that uploaded files disappear when a validation
|
137
151
|
fails. CarrierWave has a feature that makes it easy to remember the
|
138
|
-
uploaded file even in that case. Suppose your
|
152
|
+
uploaded file even in that case. Suppose your +user+ model has an uploader mounted on +avatar+ file, just add a hidden field called +avatar_cache+.
|
139
153
|
In Rails, this would look like this:
|
140
154
|
|
141
155
|
<% form_for @user do |f| %>
|
@@ -158,27 +172,7 @@ in the case of images, a small thumbnail would be a good indicator:
|
|
158
172
|
</p>
|
159
173
|
<% end %>
|
160
174
|
|
161
|
-
|
162
|
-
|
163
|
-
The fact that uploaders are separate classes in CarrierWave is a big advantage. What this means for you is:
|
164
|
-
|
165
|
-
#### Less magic
|
166
|
-
|
167
|
-
In order to customize your uploader, all you need to do is override methods and use normal, clear and simple Ruby code. That means no `alias_method_chain`'ing to hook into the upload process, no messing around with weird extensions. The code in CarrierWave is very simple and easy because of this.
|
168
|
-
|
169
|
-
#### Easier to test
|
170
|
-
|
171
|
-
How do you test file uploads? I always found this ridiculously hard. A separate class means you can test is separately, which is nicer, easier and more maintainable.
|
172
|
-
|
173
|
-
#### More Flexible
|
174
|
-
|
175
|
-
Many of the things you can do in CarrierWave are hard, or impossible to do in other file upload plugins, and have previously required you to roll your own. Now you can get all the flexibility without having to write low level stuff.
|
176
|
-
|
177
|
-
#### Easy to extend
|
178
|
-
|
179
|
-
CarrierWave has support for a few different image manipulation libraries. These need *no* code to hook into CarrierWave, because they are simple modules. If you want to write your own manipulation library (doesn't need to be for images), you can do the same.
|
180
|
-
|
181
|
-
## Using Amazon S3
|
175
|
+
== Using Amazon S3
|
182
176
|
|
183
177
|
You'll need to configure a bucket, access id and secret key like this:
|
184
178
|
|
@@ -186,31 +180,31 @@ You'll need to configure a bucket, access id and secret key like this:
|
|
186
180
|
CarrierWave.config[:s3][:secret_access_key] = 'xxxxxx'
|
187
181
|
CarrierWave.config[:s3][:bucket] = 'name_of_bucket'
|
188
182
|
|
189
|
-
Do this in an initializer in Rails, and in a
|
183
|
+
Do this in an initializer in Rails, and in a +before_app_loads+ block in Merb.
|
190
184
|
|
191
185
|
And then in your uploader, set the storage to :s3
|
192
186
|
|
193
|
-
class AvatarUploader
|
187
|
+
class AvatarUploader
|
188
|
+
include CarrierWave::Uploader
|
189
|
+
|
194
190
|
storage :s3
|
195
191
|
end
|
196
192
|
|
197
|
-
That's it! You can still use the
|
198
|
-
|
199
|
-
## Using RMagick
|
193
|
+
That's it! You can still use the +CarrierWave::Uploader#url+ method to return the url to the file on Amazon S3
|
200
194
|
|
201
|
-
|
195
|
+
== Using RMagick
|
202
196
|
|
203
|
-
|
197
|
+
If you're uploading images, you'll probably want to manipulate them in some way, you might want to create thumbnail images for example. CarrierWave comes with a small library to make manipulating images with RMagick easier, you'll need to include it in your Uploader:
|
204
198
|
|
205
|
-
|
206
|
-
|
207
|
-
class AvatarUploader < CarrierWave::Uploader
|
199
|
+
class AvatarUploader
|
200
|
+
include CarrierWave::Uploader
|
208
201
|
include CarrierWave::RMagick
|
209
202
|
end
|
210
203
|
|
211
|
-
The RMagick module gives you a few methods, like
|
204
|
+
The RMagick module gives you a few methods, like +CarrierWave::RMagick#crop_resized+ which manipulate the image file in some way. You can set a +process+ callback, which will call that method any time a file is uploaded.
|
212
205
|
|
213
|
-
class AvatarUploader
|
206
|
+
class AvatarUploader
|
207
|
+
include CarrierWave::Uploader
|
214
208
|
include CarrierWave::RMagick
|
215
209
|
|
216
210
|
process :crop_resized => [200, 200]
|
@@ -223,20 +217,21 @@ The RMagick module gives you a few methods, like `CarrierWave::RMagick#crop_resi
|
|
223
217
|
|
224
218
|
Check out the manipulate! method, which makes it easy for you to write your own manipulation methods.
|
225
219
|
|
226
|
-
|
227
|
-
|
228
|
-
ImageScience works the same way as RMagick. As with RMagick you'll need to require it:
|
229
|
-
|
230
|
-
require 'carrierwave/processing/image_science'
|
220
|
+
== Using ImageScience
|
231
221
|
|
232
|
-
|
222
|
+
ImageScience works the same way as RMagick.
|
233
223
|
|
234
|
-
class AvatarUploader
|
224
|
+
class AvatarUploader
|
225
|
+
include CarrierWave::Uploader
|
235
226
|
include CarrierWave::ImageScience
|
236
227
|
|
237
228
|
process :crop_resized => [200, 200]
|
238
229
|
end
|
239
230
|
|
240
|
-
|
231
|
+
== Documentation
|
232
|
+
|
233
|
+
Full rdoc documentation is {available at Rubyforge}[http://carrierwave.rubyforge.org/].
|
234
|
+
|
235
|
+
== Read the source
|
241
236
|
|
242
|
-
CarrierWave is still young, but most of it is pretty well documented. Just dig in and look at the source for more in-depth explanation of what things are doing.
|
237
|
+
CarrierWave is still young, but most of it is pretty well documented. It is also extensively specced, and there are cucumber features for some common use cases. Just dig in and look at the source for more in-depth explanation of what things are doing.
|
data/Rakefile
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake/gempackagetask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
gem 'rdoc', '>=2.4.0'
|
5
|
+
require 'rdoc'
|
3
6
|
|
4
|
-
require 'yard'
|
5
7
|
require 'spec/rake/spectask'
|
6
8
|
require 'cucumber/rake/task'
|
7
9
|
|
8
10
|
NAME = "carrierwave"
|
9
|
-
GEM_VERSION = "0.
|
11
|
+
GEM_VERSION = "0.2.0"
|
10
12
|
AUTHOR = "Jonas Nicklas"
|
11
13
|
EMAIL = "jonas.nicklas@gmail.com"
|
12
14
|
HOMEPAGE = "http://www.example.com"
|
@@ -18,14 +20,14 @@ spec = Gem::Specification.new do |s|
|
|
18
20
|
s.version = GEM_VERSION
|
19
21
|
s.platform = Gem::Platform::RUBY
|
20
22
|
s.has_rdoc = true
|
21
|
-
s.extra_rdoc_files = ["README.
|
23
|
+
s.extra_rdoc_files = ["README.rdoc", "LICENSE", 'TODO']
|
22
24
|
s.summary = SUMMARY
|
23
25
|
s.description = s.summary
|
24
26
|
s.author = AUTHOR
|
25
27
|
s.email = EMAIL
|
26
28
|
s.homepage = HOMEPAGE
|
27
29
|
s.require_path = 'lib'
|
28
|
-
s.files = %w(LICENSE Generators README.
|
30
|
+
s.files = %w(LICENSE Generators README.rdoc Rakefile TODO) + Dir.glob("{lib,spec,rails_generators}/**/*")
|
29
31
|
|
30
32
|
end
|
31
33
|
|
@@ -38,8 +40,12 @@ Cucumber::Rake::Task.new do |t|
|
|
38
40
|
t.cucumber_opts = "--profile #{profile}"
|
39
41
|
end
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
+
Rake::RDocTask.new do |rd|
|
44
|
+
rd.main = "README.rdoc"
|
45
|
+
rd.title = "CarrierWave"
|
46
|
+
rd.options << "--diagram" if ENV["DIAGRAM"]
|
47
|
+
rd.rdoc_dir = File.join(File.dirname(__FILE__), 'doc')
|
48
|
+
rd.rdoc_files.include("README.rdoc", "LICENSE", "TODO", 'lib/carrierwave/**/*.rb')
|
43
49
|
end
|
44
50
|
|
45
51
|
Rake::GemPackageTask.new(spec) do |pkg|
|
data/lib/carrierwave.rb
CHANGED
@@ -4,7 +4,7 @@ module CarrierWave
|
|
4
4
|
class << self
|
5
5
|
attr_accessor :config
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
class UploadError < StandardError; end
|
9
9
|
class NoFileError < UploadError; end
|
10
10
|
class FormNotMultipart < UploadError
|
@@ -12,50 +12,66 @@ module CarrierWave
|
|
12
12
|
"You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed.\n\n If this is a file upload, please check that your upload form is multipart encoded."
|
13
13
|
end
|
14
14
|
end
|
15
|
+
class IntegrityError < UploadError; end
|
15
16
|
class InvalidParameter < UploadError; end
|
16
17
|
# Should be used by methods used as process callbacks.
|
17
18
|
class ProcessingError < UploadError; end
|
18
|
-
end
|
19
19
|
|
20
|
-
|
20
|
+
autoload :SanitizedFile, 'carrierwave/sanitized_file'
|
21
|
+
autoload :Uploader, 'carrierwave/uploader'
|
22
|
+
autoload :Mount, 'carrierwave/mount'
|
23
|
+
autoload :RMagick, 'carrierwave/processing/rmagick'
|
24
|
+
autoload :ImageScience, 'carrierwave/processing/image_science'
|
25
|
+
|
26
|
+
module Storage
|
27
|
+
autoload :Abstract, 'carrierwave/storage/abstract'
|
28
|
+
autoload :File, 'carrierwave/storage/file'
|
29
|
+
autoload :S3, 'carrierwave/storage/s3'
|
30
|
+
end
|
31
|
+
|
32
|
+
module Test
|
33
|
+
autoload :Matchers, 'carrierwave/test/matchers'
|
34
|
+
end
|
21
35
|
|
22
|
-
|
23
|
-
require File.join(dir, 'uploader')
|
24
|
-
require File.join(dir, 'mount')
|
25
|
-
require File.join(dir, 'storage', 'abstract')
|
26
|
-
require File.join(dir, 'storage', 'file')
|
27
|
-
require File.join(dir, 'storage', 's3')
|
36
|
+
end
|
28
37
|
|
29
38
|
CarrierWave.config = {
|
39
|
+
:permissions => 0644,
|
30
40
|
:storage => :file,
|
31
41
|
:use_cache => true,
|
32
42
|
:storage_engines => {
|
33
|
-
:file => CarrierWave::Storage::File,
|
34
|
-
:s3 => CarrierWave::Storage::S3
|
43
|
+
:file => "CarrierWave::Storage::File",
|
44
|
+
:s3 => "CarrierWave::Storage::S3"
|
35
45
|
},
|
36
46
|
:s3 => {
|
37
47
|
:access => :public_read
|
38
48
|
},
|
39
49
|
:store_dir => 'uploads',
|
40
|
-
:cache_dir => 'uploads/tmp'
|
50
|
+
:cache_dir => 'uploads/tmp',
|
51
|
+
:mount => {
|
52
|
+
:ignore_integrity_errors => true,
|
53
|
+
:ignore_processing_errors => true,
|
54
|
+
:validate_integrity => true,
|
55
|
+
:validate_processing => true
|
56
|
+
}
|
41
57
|
}
|
42
58
|
|
43
59
|
if defined?(Merb)
|
44
60
|
CarrierWave.config[:root] = Merb.root
|
45
61
|
CarrierWave.config[:public] = Merb.dir_for(:public)
|
46
|
-
|
62
|
+
|
47
63
|
orm_path = File.dirname(__FILE__) / 'carrierwave' / 'orm' / Merb.orm
|
48
64
|
require orm_path if File.exist?(orm_path + '.rb')
|
49
|
-
|
65
|
+
|
50
66
|
Merb.push_path(:uploader, Merb.root / "app" / "uploaders")
|
51
|
-
|
67
|
+
|
52
68
|
Merb.add_generators File.dirname(__FILE__) / 'generators' / 'uploader_generator'
|
53
69
|
end
|
54
70
|
|
55
71
|
if defined?(Rails)
|
56
72
|
CarrierWave.config[:root] = Rails.root
|
57
73
|
CarrierWave.config[:public] = File.join(Rails.root, 'public')
|
58
|
-
|
74
|
+
|
59
75
|
require File.join(File.dirname(__FILE__), "carrierwave", "orm", 'activerecord')
|
60
76
|
|
61
77
|
ActiveSupport::Dependencies.load_paths << File.join(Rails.root, "app", "uploaders")
|
@@ -64,4 +80,4 @@ end
|
|
64
80
|
if defined?(Sinatra)
|
65
81
|
CarrierWave.config[:root] = Sinatra::Application.root
|
66
82
|
CarrierWave.config[:public] = Sinatra::Application.public
|
67
|
-
end
|
83
|
+
end
|
data/lib/carrierwave/mount.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module CarrierWave
|
2
|
-
|
2
|
+
|
3
3
|
##
|
4
4
|
# If a Class is extended with this module, it gains the mount_uploader
|
5
5
|
# method, which is used for mapping attributes to uploaders and allowing
|
@@ -13,12 +13,23 @@ module CarrierWave
|
|
13
13
|
module Mount
|
14
14
|
|
15
15
|
##
|
16
|
-
#
|
16
|
+
# === Returns
|
17
|
+
#
|
18
|
+
# [Hash{Symbol => CarrierWave}] what uploaders are mounted on which columns
|
17
19
|
#
|
18
20
|
def uploaders
|
19
21
|
@uploaders ||= {}
|
20
22
|
end
|
21
23
|
|
24
|
+
##
|
25
|
+
# === Returns
|
26
|
+
#
|
27
|
+
# [Hash{Symbol => Hash}] options for mounted uploaders
|
28
|
+
#
|
29
|
+
def uploader_options
|
30
|
+
@uploader_options ||= {}
|
31
|
+
end
|
32
|
+
|
22
33
|
##
|
23
34
|
# Mounts the given uploader on the given column. This means that assigning
|
24
35
|
# and reading from the column will upload and retrieve files. Supposing
|
@@ -38,24 +49,50 @@ module CarrierWave
|
|
38
49
|
# but if there is any significatnt logic in the uploader, you should do
|
39
50
|
# the right thing and have it in its own file.
|
40
51
|
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
52
|
+
# === Added instance methods
|
53
|
+
#
|
54
|
+
# Supposing a class has used +mount_uploader+ to mount an uploader on a column
|
55
|
+
# named +image+, in that case the following methods will be added to the class:
|
56
|
+
#
|
57
|
+
# [image] Returns an instance of the uploader only if anything has been uploaded
|
58
|
+
# [image=] Caches the given file
|
59
|
+
# [image_cache] Returns a string that identifies the cache location of the file
|
60
|
+
# [image_cache=] Retrieves the file from the cache based on the given cache name
|
61
|
+
# [image_uploader] Returns an instance of the uploader
|
62
|
+
# [image_uploader=] Sets the uploader (be careful!)
|
63
|
+
# [store_image!] Stores a file that has been assigned with +image=+
|
64
|
+
# [image_integrity_error] Returns an error object if the last file to be assigned caused an integrty error
|
65
|
+
#
|
66
|
+
# === Parameters
|
67
|
+
#
|
68
|
+
# [column (Symbol)] the attribute to mount this uploader on
|
69
|
+
# [uploader (CarrierWave::Uploader)] the uploader class to mount
|
70
|
+
# [options (Hash{Symbol => Object})] a set of options
|
71
|
+
# [&block (Proc)] customize anonymous uploaders
|
72
|
+
#
|
73
|
+
# === Options
|
74
|
+
#
|
75
|
+
# [:ignore_integrity_errors (Boolean)] if set to true, integrity errors will result in caching failing silently
|
76
|
+
#
|
77
|
+
# === Examples
|
78
|
+
#
|
79
|
+
# Mounting uploaders on different columns.
|
80
|
+
#
|
45
81
|
# class Song
|
46
82
|
# mount_uploader :lyrics, LyricsUploader
|
83
|
+
# mount_uploader :alternative_lyrics, LyricsUploader
|
47
84
|
# mount_uploader :file, SongUploader
|
48
85
|
# end
|
49
|
-
#
|
86
|
+
#
|
87
|
+
# This will add an anonymous uploader with only the default settings:
|
88
|
+
#
|
50
89
|
# class Data
|
51
|
-
# # this will add an anonymous uploader with only
|
52
|
-
# # the default settings
|
53
90
|
# mount_uploader :csv
|
54
91
|
# end
|
55
|
-
#
|
92
|
+
#
|
93
|
+
# this will add an anonymous uploader overriding the store_dir:
|
94
|
+
#
|
56
95
|
# class Product
|
57
|
-
# # this will add an anonymous uploader overriding
|
58
|
-
# # the store_dir
|
59
96
|
# mount_uploader :blueprint do
|
60
97
|
# def store_dir
|
61
98
|
# 'blueprints'
|
@@ -63,91 +100,133 @@ module CarrierWave
|
|
63
100
|
# end
|
64
101
|
# end
|
65
102
|
#
|
66
|
-
def mount_uploader(column, uploader=nil, &block)
|
103
|
+
def mount_uploader(column, uploader=nil, options={}, &block)
|
67
104
|
unless uploader
|
68
|
-
uploader = Class.new
|
105
|
+
uploader = Class.new do
|
106
|
+
include CarrierWave::Uploader
|
107
|
+
end
|
69
108
|
uploader.class_eval(&block)
|
70
109
|
end
|
71
110
|
|
72
111
|
uploaders[column.to_sym] = uploader
|
112
|
+
uploader_options[column.to_sym] = CarrierWave.config[:mount].merge(options)
|
73
113
|
|
74
114
|
include CarrierWave::Mount::Extension
|
75
115
|
|
76
|
-
class_eval <<-
|
116
|
+
class_eval <<-RUBY, __FILE__, __LINE__+1
|
117
|
+
def #{column}_uploader # def image_uploader
|
118
|
+
_uploader_get(:#{column}) # _uploader_get(:image)
|
119
|
+
end # end
|
120
|
+
#
|
121
|
+
def #{column}_uploader=(uploader) # def image_uploader=(uploader)
|
122
|
+
_uploader_set(:#{column}, uploader) # _uploader_set(:image, uploader)
|
123
|
+
end # end
|
124
|
+
#
|
77
125
|
def #{column} # def image
|
78
|
-
|
126
|
+
_uploader_get_column(:#{column}) # _uploader_get_column(:image)
|
79
127
|
end # end
|
80
128
|
#
|
81
129
|
def #{column}=(new_file) # def image=(new_file)
|
82
|
-
|
130
|
+
_uploader_set_column(:#{column}, new_file) # _uploader_set_column(:image, new_file)
|
83
131
|
end # end
|
84
132
|
#
|
85
133
|
def #{column}_cache # def image_cache
|
86
|
-
|
134
|
+
_uploader_get_cache(:#{column}) # _uploader_get_cache(:image)
|
87
135
|
end # end
|
88
136
|
#
|
89
137
|
def #{column}_cache=(cache_name) # def image_cache=(cache_name)
|
90
|
-
|
138
|
+
_uploader_set_cache(:#{column}, cache_name) # _uploader_set_cache(:image, cache_name)
|
91
139
|
end # end
|
92
140
|
#
|
93
141
|
def store_#{column}! # def store_image!
|
94
|
-
|
142
|
+
_uploader_store!(:#{column}) # _uploader_store!(:image)
|
95
143
|
end # end
|
96
|
-
|
97
|
-
|
98
|
-
|
144
|
+
#
|
145
|
+
def #{column}_integrity_error # def image_integrity_error
|
146
|
+
_uploader_integrity_errors[:#{column}] # _uploader_integrity_errors[:image]
|
147
|
+
end # end
|
148
|
+
#
|
149
|
+
def #{column}_processing_error # def image_processing_error
|
150
|
+
_uploader_processing_errors[:#{column}] # _uploader_processing_errors[:image]
|
151
|
+
end # end
|
152
|
+
RUBY
|
99
153
|
end
|
100
154
|
|
101
155
|
module Extension
|
102
|
-
|
103
|
-
|
104
|
-
|
156
|
+
|
157
|
+
##
|
105
158
|
# overwrite this to read from a serialized attribute
|
159
|
+
#
|
106
160
|
def read_uploader(column); end
|
107
161
|
|
162
|
+
##
|
108
163
|
# overwrite this to write to a serialized attribute
|
164
|
+
#
|
109
165
|
def write_uploader(column, identifier); end
|
110
166
|
|
111
|
-
|
112
|
-
|
167
|
+
private
|
168
|
+
|
169
|
+
def _uploader_get(column)
|
170
|
+
@_uploaders ||= {}
|
171
|
+
@_uploaders[column] ||= self.class.uploaders[column].new(self, column)
|
113
172
|
end
|
114
|
-
|
115
|
-
def
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
173
|
+
|
174
|
+
def _uploader_set(column, uploader)
|
175
|
+
@_uploaders ||= {}
|
176
|
+
@_uploaders[column] = uploader
|
177
|
+
end
|
178
|
+
|
179
|
+
def _uploader_options(column)
|
180
|
+
self.class.uploader_options[column]
|
120
181
|
end
|
121
|
-
|
122
|
-
def
|
123
|
-
return
|
182
|
+
|
183
|
+
def _uploader_get_column(column)
|
184
|
+
return _uploader_get(column) unless _uploader_get(column).blank?
|
124
185
|
|
125
186
|
identifier = read_uploader(column)
|
126
|
-
|
187
|
+
|
127
188
|
unless identifier.blank?
|
128
|
-
|
129
|
-
|
130
|
-
uploaders[column]
|
189
|
+
_uploader_get(column).retrieve_from_store!(identifier)
|
190
|
+
_uploader_get(column)
|
131
191
|
end
|
132
192
|
end
|
133
|
-
|
134
|
-
def
|
135
|
-
|
136
|
-
|
193
|
+
|
194
|
+
def _uploader_set_column(column, new_file)
|
195
|
+
_uploader_get(column).cache!(new_file)
|
196
|
+
_uploader_integrity_errors[column] = nil
|
197
|
+
_uploader_processing_errors[column] = nil
|
198
|
+
rescue CarrierWave::IntegrityError => e
|
199
|
+
_uploader_integrity_errors[column] = e
|
200
|
+
raise e unless _uploader_options(column)[:ignore_integrity_errors]
|
201
|
+
rescue CarrierWave::ProcessingError => e
|
202
|
+
_uploader_processing_errors[column] = e
|
203
|
+
raise e unless _uploader_options(column)[:ignore_processing_errors]
|
204
|
+
end
|
205
|
+
|
206
|
+
def _uploader_get_cache(column)
|
207
|
+
_uploader_get(column).cache_name
|
137
208
|
end
|
138
|
-
|
139
|
-
def
|
140
|
-
|
209
|
+
|
210
|
+
def _uploader_set_cache(column, cache_name)
|
211
|
+
_uploader_get(column).retrieve_from_cache(cache_name) unless cache_name.blank?
|
141
212
|
end
|
142
213
|
|
143
|
-
def
|
144
|
-
unless
|
145
|
-
|
146
|
-
|
214
|
+
def _uploader_store!(column)
|
215
|
+
unless _uploader_get(column).blank?
|
216
|
+
_uploader_get(column).store!
|
217
|
+
write_uploader(column, _uploader_get(column).identifier)
|
147
218
|
end
|
148
219
|
end
|
149
220
|
|
221
|
+
def _uploader_integrity_errors
|
222
|
+
@_uploader_integrity_errors ||= {}
|
223
|
+
end
|
224
|
+
|
225
|
+
def _uploader_processing_errors
|
226
|
+
@_uploader_processing_errors ||= {}
|
227
|
+
end
|
228
|
+
|
150
229
|
end # Extension
|
151
|
-
|
230
|
+
|
152
231
|
end # Mount
|
153
|
-
end # CarrierWave
|
232
|
+
end # CarrierWave
|