@@ -3,19 +3,15 @@ Delayed::Paperclip [
6
- attachments in a background task with
7
- [ ActiveJob] ( https://github.com/rails/rails/tree/master/activejob ) ,
8
- [ DelayedJob] ( https://github.com/collectiveidea/delayed_job ) ,
9
- [ Resque] ( https://github.com/resque/resque ) or [ Sidekiq] ( https://github.com/mperham/sidekiq ) .
6
+ attachments in a background task with [ ActiveJob] ( https://github.com/rails/rails/tree/master/activejob )
10
7
11
8
Why?
12
9
----
13
10
14
11
The most common use case for Paperclip is to easily attach image files
15
12
to ActiveRecord models. Most of the time these image files will have
16
13
multiple styles and will need to be resized when they are created. This
17
- is usually a pretty [ slow operation] ( http://www.jstorimer.com/ruby/2010/01/05/speep-up-your-paperclip-tests.html ) and should be handled in a
18
- background task.
14
+ is usually a pretty slow operation and should be handled in a background task.
19
15
20
16
I’m sure that everyone knows this, this gem just makes it easy to do.
21
17
@@ -24,28 +20,23 @@ Installation
24
20
25
21
Install the gem:
26
22
27
- ````
23
+ ```
28
24
gem install delayed_paperclip
29
- ````
25
+ ```
30
26
31
27
Or even better, add it to your Gemfile.
32
28
33
- ````
29
+ ```
34
30
source "https://rubygems.org"
35
- gem 'delayed_paperclip'
36
- ````
37
-
38
- Dependencies:
39
-
40
- - Paperclip
41
- - DJ, Resque or Sidekiq
31
+ gem "delayed_paperclip"
32
+ ```
42
33
43
34
Usage
44
35
-----
45
36
46
37
In your model:
47
38
48
- ```` ruby
39
+ ``` ruby
49
40
class User < ActiveRecord ::Base
50
41
has_attached_file :avatar , styles: {
51
42
medium: " 300x300>" ,
@@ -54,57 +45,10 @@ class User < ActiveRecord::Base
54
45
55
46
process_in_background :avatar
56
47
end
57
- ````
48
+ ```
58
49
59
50
Use your Paperclip attachment just like always in controllers and views.
60
51
61
- To select between using Resque or Delayed::Job, just install and
62
- configure your choice properly within your application, and
63
- delayed_paperclip will do the rest. It will detect which library is
64
- loaded and make a decision about which sort of job to enqueue at that
65
- time.
66
-
67
- ### Active Job
68
-
69
- [ Active Job] ( https://github.com/rails/rails/tree/master/activejob ) will take
70
- precedence over any other installed library. Since it is installed as a
71
- dependency with Rails 4.2.1 this might cause some confusion, so make sure that
72
- Active Job is configured to use the correct queue adapter:
73
-
74
- ```` ruby
75
- module YourApp
76
- class Application < Rails ::Application
77
- # Other code...
78
-
79
- config.active_job.queue_adapter = :resque # Or :delayed_job or :sidekiq
80
- end
81
- end
82
- ````
83
-
84
- ### Resque
85
-
86
- Resque adapter is deprecated. Please use ActiveJob one.
87
-
88
- Make sure that you have [ Resque] ( https://github.com/resque/resque ) up and running. The jobs will be
89
- dispatched to the <code >: paperclip </code > queue, so you can correctly
90
- dispatch your worker. Configure resque and your workers exactly as you
91
- would otherwise.
92
-
93
- ### DJ
94
-
95
- DelayedJob adapter is deprecated. Please use ActiveJob one.
96
-
97
- Just make sure that you have DJ up and running.
98
-
99
- ### Sidekiq
100
-
101
- Sidekiq adapter is deprecated. Please use ActiveJob one.
102
-
103
- Make sure that [ Sidekiq] ( http://github.com/mperham/sidekiq ) is running and listening to the
104
- ` paperclip ` queue, either by adding it to your
105
- ` sidekiq.yml ` config file under ` - queues: ` or by
106
- passing the command line argument ` -q paperclip ` to Sidekiq.
107
-
108
52
### Displaying images during processing
109
53
110
54
In the default setup, when you upload an image for the first time and
@@ -117,7 +61,7 @@ To have the missing image url be outputted by paperclip while the image is being
117
61
` #{attachment_name}_processing ` column to the specific model you want
118
62
to enable this feature for. This feature gracefully degrades and will not affect models which do not have the column added to them.
119
63
120
- ```` ruby
64
+ ``` ruby
121
65
class AddAvatarProcessingToUser < ActiveRecord ::Migration
122
66
def self .up
123
67
add_column :users , :avatar_processing , :boolean
131
75
@user = User .new (avatar: File .new (...))
132
76
@user .save
133
77
@user .avatar.url # => "/images/original/missing.png"
134
- Delayed ::Worker .new .work_off
78
+
79
+ # Process job
135
80
136
81
@user .reload
137
82
@user .avatar.url # => "/system/images/3/original/IMG_2772.JPG?1267562148"
138
- ````
83
+ ```
139
84
140
85
#### Custom image for processing
141
86
142
87
This is useful if you have a difference between missing images and
143
88
images currently being processed.
144
89
145
- ```` ruby
90
+ ``` ruby
146
91
class User < ActiveRecord ::Base
147
92
has_attached_file :avatar
148
93
152
97
@user = User .new (avatar: File .new (...))
153
98
@user .save
154
99
@user .avatar.url # => "/images/original/processing.png"
155
- Delayed ::Worker .new .work_off
100
+
101
+ # Process job
156
102
157
103
@user .reload
158
104
@user .avatar.url # => "/system/images/3/original/IMG_2772.JPG?1267562148"
159
- ````
105
+ ```
160
106
161
107
You can also define a custom logic for ` processing_image_url ` , for
162
- example to display the original\
163
- picture while specific formats are being processed.
108
+ example to display the original picture while specific formats are being processed.
164
109
165
- ```` ruby
110
+ ``` ruby
166
111
class Item < ActiveRecord ::Base
167
112
has_attached_file :photo
168
113
@@ -173,7 +118,7 @@ class Item < ActiveRecord::Base
173
118
options[:interpolator ].interpolate(options[:url ], photo, :original )
174
119
end
175
120
end
176
- ````
121
+ ```
177
122
178
123
#### Have processing? status available, but construct image URLs as if delayed_paperclip wasn’t present
179
124
@@ -183,7 +128,7 @@ If you define the `#{attachment_name}_processing` column, but set the
183
128
Note especially the method #processing? which passes through the value
184
129
of the boolean created via migration.
185
130
186
- ```` ruby
131
+ ``` ruby
187
132
class User < ActiveRecord ::Base
188
133
has_attached_file :avatar
189
134
@@ -194,58 +139,59 @@ end
194
139
@user .save
195
140
@user .avatar.url # => "/system/images/3/original/IMG_2772.JPG?1267562148"
196
141
@user .avatar.processing? # => true
197
- Delayed ::Worker .new .work_off
142
+
143
+ # Process job
198
144
199
145
@user .reload
200
146
@user .avatar.url # => "/system/images/3/original/IMG_2772.JPG?1267562148"
201
147
@user .avatar.processing? # => false
202
- ````
148
+ ```
203
149
204
150
#### Only process certain styles
205
151
206
152
This is useful if you don’t want the background job to reprocess all
207
153
styles.
208
154
209
- ```` ruby
155
+ ``` ruby
210
156
class User < ActiveRecord ::Base
211
157
has_attached_file :avatar , styles: { small: " 25x25#" , medium: " 50x50#" }
212
158
213
159
process_in_background :avatar , only_process: [:small ]
214
160
end
215
- ````
161
+ ```
216
162
217
163
Like paperclip, you could also supply a lambda function to define
218
164
` only_process ` dynamically.
219
165
220
- ```` ruby
166
+ ``` ruby
221
167
class User < ActiveRecord ::Base
222
168
has_attached_file :avatar , styles: { small: " 25x25#" , medium: " 50x50#" }
223
169
224
170
process_in_background :avatar , only_process: lambda { |a | a.instance.small_supported? ? [:small , :large ] : [:large ] }
225
171
end
226
- ````
172
+ ```
227
173
228
174
#### Split processing
229
175
230
176
You can process some styles in the foreground and some in the background
231
177
by setting ` only_process ` on both ` has_attached_file ` and
232
178
` process_in_background ` .
233
179
234
- ```` ruby
180
+ ``` ruby
235
181
class User < ActiveRecord ::Base
236
182
has_attached_file :avatar , styles: { small: " 25x25#" , medium: " 50x50#" }, only_process: [:small ]
237
183
238
184
process_in_background :avatar , only_process: [:medium ]
239
185
end
240
- ````
186
+ ```
241
187
242
188
#### Reprocess Without Delay
243
189
244
190
This is useful if you don’t want the background job. It accepts
245
191
individual styles too. Take note, normal ` reprocess! ` does not accept styles as arguments anymore. It will delegate to DelayedPaperclip and
246
192
reprocess all styles.
247
193
248
- ```` ruby
194
+ ``` ruby
249
195
class User < ActiveRecord ::Base
250
196
has_attached_file :avatar , styles: { small: " 25x25#" , medium: " 50x50#" }
251
197
254
200
255
201
@user .avatar.url # => "/system/images/3/original/IMG_2772.JPG?1267562148"
256
202
@user .avatar.reprocess_without_delay!(:medium )
257
- ````
203
+ ```
258
204
259
205
#### Set queue name
260
206
@@ -278,7 +224,7 @@ defined by changing the DelayedPaperclip.options Hash, this can be useful for se
278
224
If you’re using Rails you can define a Hash with default options in
279
225
config/application.rb or in any of the config/environments/\* .rb files on ` config.delayed_paperclip_defaults ` , these will get merged into DelayedPaperclip.options as your Rails app boots. An example:
280
226
281
- ```` ruby
227
+ ``` ruby
282
228
module YourApp
283
229
class Application < Rails ::Application
284
230
# Other code...
@@ -289,7 +235,7 @@ module YourApp
289
235
}
290
236
end
291
237
end
292
- ````
238
+ ```
293
239
294
240
What if I’m not using images?
295
241
-----------------------------
@@ -301,7 +247,7 @@ Paperclip Post-processors are not working
301
247
-----------------------------------------
302
248
303
249
If you are using custom [ post-processing processors] ( https://github.com/thoughtbot/paperclip#post-processing )
304
- like this:
250
+ like this:
305
251
306
252
``` ruby
307
253
# ...
@@ -311,14 +257,14 @@ process_in_background :avatar
311
257
312
258
def rotate!
313
259
# ...
314
- avatar.reprocess!
260
+ avatar.reprocess!
315
261
# ...
316
262
end
317
263
318
264
# ...
319
265
```
320
266
321
- ...you may encounter an issue where your post-processors are ignored
267
+ ...you may encounter an issue where your post-processors are ignored
322
268
([ more info] ( https://github.com/jrgifford/delayed_paperclip/issues/171 ) ).
323
269
In order to avoid this use ` reprocess_without_delay! `
324
270
@@ -327,7 +273,7 @@ In order to avoid this use `reprocess_without_delay!`
327
273
328
274
def rotate!
329
275
# ...
330
- avatar.reprocess_without_delay!
276
+ avatar.reprocess_without_delay!
331
277
# ...
332
278
end
333
279
@@ -344,7 +290,7 @@ Contributing
344
290
345
291
Checkout out [ CONTRIBUTING] ( https://github.com/jrgifford/delayed_paperclip/blob/master/CONTRIBUTING ) . Run specs with:
346
292
347
- ````
293
+ ```
348
294
# Rspec on all versions
349
295
bundle exec appraisal install
350
296
bundle exec appraisal rake
@@ -354,4 +300,4 @@ bundle exec rake
354
300
355
301
# Rspec on specific rails version
356
302
bundle exec appraisal 5.0 rake
357
- ````
303
+ ```
0 commit comments