jnicklas-carrierwave 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/{README.md → README.rdoc} +63 -78
- data/Rakefile +12 -6
- data/lib/carrierwave/mount.rb +133 -52
- data/lib/carrierwave/orm/activerecord.rb +52 -3
- data/lib/carrierwave/orm/datamapper.rb +5 -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 +71 -45
- data/lib/carrierwave/storage/abstract.rb +23 -9
- data/lib/carrierwave/storage/file.rb +17 -9
- data/lib/carrierwave/storage/s3.rb +49 -22
- data/lib/carrierwave/test/matchers.rb +112 -0
- data/lib/carrierwave/uploader.rb +231 -112
- data/lib/carrierwave.rb +32 -17
- 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 +86 -22
- metadata +8 -4
data/LICENSE
CHANGED
data/{README.md → README.rdoc}
RENAMED
@@ -1,8 +1,8 @@
|
|
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
8
|
|
@@ -20,7 +20,7 @@ In Rails, add it to your environment.rb:
|
|
20
20
|
|
21
21
|
config.gem "carrierwave"
|
22
22
|
|
23
|
-
|
23
|
+
== Quick Start
|
24
24
|
|
25
25
|
Start off by generating an uploader:
|
26
26
|
|
@@ -36,7 +36,9 @@ this should give you a file in:
|
|
36
36
|
|
37
37
|
Check out this file for some hints on how you can customize your uploader. It should look something like this:
|
38
38
|
|
39
|
-
class AvatarUploader
|
39
|
+
class AvatarUploader
|
40
|
+
include CarrierWave::Uploader
|
41
|
+
|
40
42
|
storage :file
|
41
43
|
end
|
42
44
|
|
@@ -48,42 +50,27 @@ You can use your uploader class to store and retrieve files like this:
|
|
48
50
|
|
49
51
|
uploader.retrieve_from_store!('my_file.png')
|
50
52
|
|
51
|
-
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.
|
52
54
|
|
53
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:
|
54
56
|
|
55
|
-
|
57
|
+
=== ActiveRecord, DataMapper, Sequel
|
56
58
|
|
57
|
-
First require the
|
59
|
+
First require the relevant extension:
|
58
60
|
|
59
|
-
require 'carrierwave/orm/activerecord
|
61
|
+
require 'carrierwave/orm/activerecord'
|
62
|
+
require 'carrierwave/orm/datamapper'
|
63
|
+
require 'carrierwave/orm/sequel'
|
60
64
|
|
61
65
|
You don't need to do this if you are using Merb or Rails.
|
62
66
|
|
63
|
-
Open your model file,
|
67
|
+
Open your model file, for ActiveRecord do something like:
|
64
68
|
|
65
69
|
class User < ActiveRecord::Base
|
66
70
|
mount_uploader :avatar, AvatarUploader
|
67
71
|
end
|
68
72
|
|
69
|
-
|
70
|
-
|
71
|
-
u = User.new
|
72
|
-
u.avatar = params[:file]
|
73
|
-
u.avatar = File.open('somewhere')
|
74
|
-
u.save!
|
75
|
-
u.avatar.url # => '/url/to/file.png'
|
76
|
-
u.avatar.current_path # => 'path/to/file.png'
|
77
|
-
|
78
|
-
### DataMapper
|
79
|
-
|
80
|
-
First require the activerecord extension:
|
81
|
-
|
82
|
-
require 'carrierwave/orm/datamapper
|
83
|
-
|
84
|
-
You don't need to do this if you are using Merb or Rails.
|
85
|
-
|
86
|
-
Open your model file, and do something like:
|
73
|
+
Or for DataMapper:
|
87
74
|
|
88
75
|
class User
|
89
76
|
include DataMapper::Resource
|
@@ -91,7 +78,13 @@ Open your model file, and do something like:
|
|
91
78
|
mount_uploader :avatar, AvatarUploader
|
92
79
|
end
|
93
80
|
|
94
|
-
|
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.
|
95
88
|
|
96
89
|
u = User.new
|
97
90
|
u.avatar = params[:file]
|
@@ -100,11 +93,13 @@ Now you can upload files!
|
|
100
93
|
u.avatar.url # => '/url/to/file.png'
|
101
94
|
u.avatar.current_path # => 'path/to/file.png'
|
102
95
|
|
103
|
-
|
96
|
+
== Changing the storage directory
|
104
97
|
|
105
|
-
In order to change where uploaded files are put, just override the
|
98
|
+
In order to change where uploaded files are put, just override the +store_dir+ method:
|
99
|
+
|
100
|
+
class MyUploader
|
101
|
+
include CarrierWave::Uploader
|
106
102
|
|
107
|
-
class MyUploader < CarrierWave::Uploader
|
108
103
|
def store_dir
|
109
104
|
'public/my/upload/directory'
|
110
105
|
end
|
@@ -112,11 +107,12 @@ In order to change where uploaded files are put, just override the `store_dir` m
|
|
112
107
|
|
113
108
|
This works for the file storage as well as Amazon S3.
|
114
109
|
|
115
|
-
|
110
|
+
== Adding versions
|
116
111
|
|
117
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:
|
118
113
|
|
119
|
-
class MyUploader
|
114
|
+
class MyUploader
|
115
|
+
include CarrierWave::Uploader
|
120
116
|
include CarrierWave::RMagick
|
121
117
|
|
122
118
|
process :resize => [800, 800]
|
@@ -137,11 +133,23 @@ When this uploader is used, an uploaded image would be scaled to be no larger th
|
|
137
133
|
|
138
134
|
One important thing to remember is that process is called *before* versions are created. This can cut down on processing cost.
|
139
135
|
|
140
|
-
|
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
|
141
149
|
|
142
150
|
Often you'll notice that uploaded files disappear when a validation
|
143
151
|
fails. CarrierWave has a feature that makes it easy to remember the
|
144
|
-
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+.
|
145
153
|
In Rails, this would look like this:
|
146
154
|
|
147
155
|
<% form_for @user do |f| %>
|
@@ -164,27 +172,7 @@ in the case of images, a small thumbnail would be a good indicator:
|
|
164
172
|
</p>
|
165
173
|
<% end %>
|
166
174
|
|
167
|
-
|
168
|
-
|
169
|
-
The fact that uploaders are separate classes in CarrierWave is a big advantage. What this means for you is:
|
170
|
-
|
171
|
-
#### Less magic
|
172
|
-
|
173
|
-
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.
|
174
|
-
|
175
|
-
#### Easier to test
|
176
|
-
|
177
|
-
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.
|
178
|
-
|
179
|
-
#### More Flexible
|
180
|
-
|
181
|
-
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.
|
182
|
-
|
183
|
-
#### Easy to extend
|
184
|
-
|
185
|
-
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.
|
186
|
-
|
187
|
-
## Using Amazon S3
|
175
|
+
== Using Amazon S3
|
188
176
|
|
189
177
|
You'll need to configure a bucket, access id and secret key like this:
|
190
178
|
|
@@ -192,31 +180,31 @@ You'll need to configure a bucket, access id and secret key like this:
|
|
192
180
|
CarrierWave.config[:s3][:secret_access_key] = 'xxxxxx'
|
193
181
|
CarrierWave.config[:s3][:bucket] = 'name_of_bucket'
|
194
182
|
|
195
|
-
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.
|
196
184
|
|
197
185
|
And then in your uploader, set the storage to :s3
|
198
186
|
|
199
|
-
class AvatarUploader
|
187
|
+
class AvatarUploader
|
188
|
+
include CarrierWave::Uploader
|
189
|
+
|
200
190
|
storage :s3
|
201
191
|
end
|
202
192
|
|
203
|
-
That's it! You can still use the
|
193
|
+
That's it! You can still use the +CarrierWave::Uploader#url+ method to return the url to the file on Amazon S3
|
204
194
|
|
205
|
-
|
195
|
+
== Using RMagick
|
206
196
|
|
207
|
-
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
|
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:
|
208
198
|
|
209
|
-
|
210
|
-
|
211
|
-
You'll also need to include it in your Uploader:
|
212
|
-
|
213
|
-
class AvatarUploader < CarrierWave::Uploader
|
199
|
+
class AvatarUploader
|
200
|
+
include CarrierWave::Uploader
|
214
201
|
include CarrierWave::RMagick
|
215
202
|
end
|
216
203
|
|
217
|
-
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.
|
218
205
|
|
219
|
-
class AvatarUploader
|
206
|
+
class AvatarUploader
|
207
|
+
include CarrierWave::Uploader
|
220
208
|
include CarrierWave::RMagick
|
221
209
|
|
222
210
|
process :crop_resized => [200, 200]
|
@@ -229,24 +217,21 @@ The RMagick module gives you a few methods, like `CarrierWave::RMagick#crop_resi
|
|
229
217
|
|
230
218
|
Check out the manipulate! method, which makes it easy for you to write your own manipulation methods.
|
231
219
|
|
232
|
-
|
233
|
-
|
234
|
-
ImageScience works the same way as RMagick. As with RMagick you'll need to require it:
|
235
|
-
|
236
|
-
require 'carrierwave/processing/image_science'
|
220
|
+
== Using ImageScience
|
237
221
|
|
238
|
-
|
222
|
+
ImageScience works the same way as RMagick.
|
239
223
|
|
240
|
-
class AvatarUploader
|
224
|
+
class AvatarUploader
|
225
|
+
include CarrierWave::Uploader
|
241
226
|
include CarrierWave::ImageScience
|
242
227
|
|
243
228
|
process :crop_resized => [200, 200]
|
244
229
|
end
|
245
230
|
|
246
|
-
|
231
|
+
== Documentation
|
247
232
|
|
248
|
-
Full
|
233
|
+
Full rdoc documentation is {available at Rubyforge}[http://carrierwave.rubyforge.org/].
|
249
234
|
|
250
|
-
|
235
|
+
== Read the source
|
251
236
|
|
252
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/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,89 +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
|
-
|
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
|
97
153
|
end
|
98
154
|
|
99
155
|
module Extension
|
100
|
-
|
101
|
-
|
102
|
-
|
156
|
+
|
157
|
+
##
|
103
158
|
# overwrite this to read from a serialized attribute
|
159
|
+
#
|
104
160
|
def read_uploader(column); end
|
105
161
|
|
162
|
+
##
|
106
163
|
# overwrite this to write to a serialized attribute
|
164
|
+
#
|
107
165
|
def write_uploader(column, identifier); end
|
108
166
|
|
109
|
-
|
110
|
-
|
167
|
+
private
|
168
|
+
|
169
|
+
def _uploader_get(column)
|
170
|
+
@_uploaders ||= {}
|
171
|
+
@_uploaders[column] ||= self.class.uploaders[column].new(self, column)
|
111
172
|
end
|
112
|
-
|
113
|
-
def
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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]
|
118
181
|
end
|
119
|
-
|
120
|
-
def
|
121
|
-
return
|
182
|
+
|
183
|
+
def _uploader_get_column(column)
|
184
|
+
return _uploader_get(column) unless _uploader_get(column).blank?
|
122
185
|
|
123
186
|
identifier = read_uploader(column)
|
124
|
-
|
187
|
+
|
125
188
|
unless identifier.blank?
|
126
|
-
|
127
|
-
|
128
|
-
uploaders[column]
|
189
|
+
_uploader_get(column).retrieve_from_store!(identifier)
|
190
|
+
_uploader_get(column)
|
129
191
|
end
|
130
192
|
end
|
131
|
-
|
132
|
-
def
|
133
|
-
|
134
|
-
|
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
|
135
208
|
end
|
136
|
-
|
137
|
-
def
|
138
|
-
|
209
|
+
|
210
|
+
def _uploader_set_cache(column, cache_name)
|
211
|
+
_uploader_get(column).retrieve_from_cache(cache_name) unless cache_name.blank?
|
139
212
|
end
|
140
213
|
|
141
|
-
def
|
142
|
-
unless
|
143
|
-
|
144
|
-
|
214
|
+
def _uploader_store!(column)
|
215
|
+
unless _uploader_get(column).blank?
|
216
|
+
_uploader_get(column).store!
|
217
|
+
write_uploader(column, _uploader_get(column).identifier)
|
145
218
|
end
|
146
219
|
end
|
147
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
|
+
|
148
229
|
end # Extension
|
149
|
-
|
230
|
+
|
150
231
|
end # Mount
|
151
|
-
end # CarrierWave
|
232
|
+
end # CarrierWave
|
@@ -4,19 +4,68 @@ module CarrierWave
|
|
4
4
|
module ActiveRecord
|
5
5
|
|
6
6
|
include CarrierWave::Mount
|
7
|
-
|
8
|
-
|
7
|
+
|
8
|
+
##
|
9
|
+
# See +CarrierWave::Mount#mount_uploader+ for documentation
|
10
|
+
#
|
11
|
+
def mount_uploader(column, uploader, options={}, &block)
|
9
12
|
super
|
10
13
|
|
11
14
|
alias_method :read_uploader, :read_attribute
|
12
15
|
alias_method :write_uploader, :write_attribute
|
13
16
|
|
17
|
+
validates_integrity_of column if uploader_options[column.to_sym][:validate_integrity]
|
18
|
+
validates_processing_of column if uploader_options[column.to_sym][:validate_processing]
|
19
|
+
|
14
20
|
before_save do |record|
|
15
21
|
record.send("store_#{column}!")
|
16
22
|
end
|
17
23
|
end
|
18
24
|
|
25
|
+
##
|
26
|
+
# Makes the record invalid if the file couldn't be uploaded due to an integrity error
|
27
|
+
#
|
28
|
+
# Accepts the usual parameters for validations in Rails (:if, :unless, etc...)
|
29
|
+
#
|
30
|
+
# === Note
|
31
|
+
#
|
32
|
+
# Set this key in your translations file for I18n:
|
33
|
+
#
|
34
|
+
# carrierwave:
|
35
|
+
# errors:
|
36
|
+
# integrity: 'Here be an error message'
|
37
|
+
#
|
38
|
+
def validates_integrity_of(*attrs)
|
39
|
+
options = attrs.last.is_a?(Hash) ? attrs.last : {}
|
40
|
+
options[:message] ||= I18n.t('carrierwave.errors.integrity', :default => 'is not an allowed type of file.')
|
41
|
+
validates_each(*attrs) do |record, attr, value|
|
42
|
+
record.errors.add attr, options[:message] if record.send("#{attr}_integrity_error")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# Makes the record invalid if the file couldn't be processed (assuming the process failed
|
48
|
+
# with a CarrierWave::ProcessingError)
|
49
|
+
#
|
50
|
+
# Accepts the usual parameters for validations in Rails (:if, :unless, etc...)
|
51
|
+
#
|
52
|
+
# === Note
|
53
|
+
#
|
54
|
+
# Set this key in your translations file for I18n:
|
55
|
+
#
|
56
|
+
# carrierwave:
|
57
|
+
# errors:
|
58
|
+
# processing: 'Here be an error message'
|
59
|
+
#
|
60
|
+
def validates_processing_of(*attrs)
|
61
|
+
options = attrs.last.is_a?(Hash) ? attrs.last : {}
|
62
|
+
options[:message] ||= I18n.t('carrierwave.errors.processing', :default => 'failed to be processed.')
|
63
|
+
validates_each(*attrs) do |record, attr, value|
|
64
|
+
record.errors.add attr, options[:message] if record.send("#{attr}_processing_error")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
19
68
|
end # ActiveRecord
|
20
69
|
end # CarrierWave
|
21
70
|
|
22
|
-
ActiveRecord::Base.send(:extend, CarrierWave::ActiveRecord)
|
71
|
+
ActiveRecord::Base.send(:extend, CarrierWave::ActiveRecord)
|
@@ -5,7 +5,10 @@ module CarrierWave
|
|
5
5
|
|
6
6
|
include CarrierWave::Mount
|
7
7
|
|
8
|
-
|
8
|
+
##
|
9
|
+
# See +CarrierWave::Mount#mount_uploader+ for documentation
|
10
|
+
#
|
11
|
+
def mount_uploader(column, uploader, options={}, &block)
|
9
12
|
super
|
10
13
|
|
11
14
|
alias_method :read_uploader, :attribute_get
|
@@ -19,4 +22,4 @@ module CarrierWave
|
|
19
22
|
end # DataMapper
|
20
23
|
end # CarrierWave
|
21
24
|
|
22
|
-
DataMapper::Model.send(:include, CarrierWave::DataMapper)
|
25
|
+
DataMapper::Model.send(:include, CarrierWave::DataMapper)
|