|
| 1 | +Delayed::Paperclip [](https://travis-ci.org/jrgifford/delayed_paperclip) [](https://codeclimate.com/github/jrgifford/delayed_paperclip) |
| 2 | +====================================================================================== |
| 3 | + |
| 4 | + |
| 5 | +DelayedPaperclip lets you process your [Paperclip](http://github.com/thoughtbot/paperclip) attachments in a |
| 6 | +background task with [DelayedJob](https://github.com/collectiveidea/delayed_job), [Resque](https://github.com/resque/resque) or [Sidekiq](https://github.com/mperham/sidekiq). |
| 7 | + |
| 8 | +Why? |
| 9 | +---- |
| 10 | + |
| 11 | +The most common use case for Paperclip is to easily attach image files |
| 12 | +to ActiveRecord models. Most of the time these image files will have |
| 13 | +multiple styles and will need to be resized when they are created. This |
| 14 | +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 |
| 15 | +background task. |
| 16 | + |
| 17 | +I’m sure that everyone knows this, this gem just makes it easy to do. |
| 18 | + |
| 19 | +Installation |
| 20 | +------------ |
| 21 | + |
| 22 | +Install the gem: |
| 23 | + |
| 24 | +```` |
| 25 | +gem install delayed_paperclip |
| 26 | +```` |
| 27 | + |
| 28 | +Or even better, add it to your Gemfile. |
| 29 | + |
| 30 | +```` |
| 31 | +source "https://rubygems.org" |
| 32 | +gem 'delayed_paperclip' |
| 33 | +```` |
| 34 | + |
| 35 | +Dependencies: |
| 36 | + |
| 37 | +- Paperclip |
| 38 | +- DJ, Resque or Sidekiq |
| 39 | + |
| 40 | +Usage |
| 41 | +----- |
| 42 | + |
| 43 | +In your model: |
| 44 | + |
| 45 | +````ruby |
| 46 | +class User < ActiveRecord::Base |
| 47 | + has_attached_file :avatar, styles: { |
| 48 | + medium: "300x300>", |
| 49 | + thumb: "100x100>" |
| 50 | + } |
| 51 | + |
| 52 | + process_in_background :avatar |
| 53 | +end |
| 54 | +```` |
| 55 | + |
| 56 | +Use your Paperclip attachment just like always in controllers and views. |
| 57 | + |
| 58 | +To select between using Resque or Delayed::Job, just install and |
| 59 | +configure your choice properly within your application, and |
| 60 | +delayed_paperclip will do the rest. It will detect which library is |
| 61 | +loaded and make a decision about which sort of job to enqueue at that |
| 62 | +time. |
| 63 | + |
| 64 | +### Resque |
| 65 | + |
| 66 | +Make sure that you have [Resque](https://github.com/resque/resque) up and running. The jobs will be |
| 67 | +dispatched to the <code>:paperclip</code> queue, so you can correctly |
| 68 | +dispatch your worker. Configure resque and your workers exactly as you |
| 69 | +would otherwise. |
| 70 | + |
| 71 | +### DJ |
| 72 | + |
| 73 | +Just make sure that you have DJ up and running. |
| 74 | + |
| 75 | +### Sidekiq |
| 76 | + |
| 77 | +Make sure that [Sidekiq](http://github.com/mperham/sidekiq) is running and listening to the |
| 78 | +`paperclip` queue, either by adding it to your |
| 79 | +`sidekiq.yml` config file under `- queues:` or by |
| 80 | +passing the command line argument `-q paperclip` to Sidekiq. |
| 81 | + |
| 82 | +### Displaying images during processing |
| 83 | + |
| 84 | +In the default setup, when you upload an image for the first time and |
| 85 | +try to display it before the job has been completed, Paperclip will be |
| 86 | +none the wiser and output the url of the image which is yet to be |
| 87 | +processed, which will result in a broken image link being displayed on |
| 88 | +the page. |
| 89 | + |
| 90 | +To have the missing image url be outputted by paperclip while the image is being processed, all you need to do is add a |
| 91 | +`#{attachment_name}_processing` column to the specific model you want |
| 92 | +to enable this feature for. This feature gracefully degrades and will not affect models which do not have the column added to them. |
| 93 | + |
| 94 | +````ruby |
| 95 | +class AddAvatarProcessingToUser < ActiveRecord::Migration |
| 96 | + def self.up |
| 97 | + add_column :users, :avatar_processing, :boolean |
| 98 | + end |
| 99 | + |
| 100 | + def self.down |
| 101 | + remove_column :users, :avatar_processing |
| 102 | + end |
| 103 | +end |
| 104 | + |
| 105 | +@user = User.new(avatar: File.new(...)) |
| 106 | +@user.save |
| 107 | +@user.avatar.url #=> "/images/original/missing.png" |
| 108 | +Delayed::Worker.new.work_off |
| 109 | + |
| 110 | +@user.reload |
| 111 | +@user.avatar.url #=> "/system/images/3/original/IMG_2772.JPG?1267562148" |
| 112 | +```` |
| 113 | + |
| 114 | +#### Custom image for processing |
| 115 | + |
| 116 | +This is useful if you have a difference between missing images and |
| 117 | +images currently being processed. |
| 118 | + |
| 119 | +````ruby |
| 120 | +class User < ActiveRecord::Base |
| 121 | + has_attached_file :avatar |
| 122 | + |
| 123 | + process_in_background :avatar, processing_image_url: "/images/original/processing.jpg" |
| 124 | +end |
| 125 | + |
| 126 | +@user = User.new(avatar: File.new(...)) |
| 127 | +@user.save |
| 128 | +@user.avatar.url #=> "/images/original/processing.png" |
| 129 | +Delayed::Worker.new.work_off |
| 130 | + |
| 131 | +@user.reload |
| 132 | +@user.avatar.url #=> "/system/images/3/original/IMG_2772.JPG?1267562148" |
| 133 | +```` |
| 134 | + |
| 135 | +You can also define a custom logic for `processing_image_url`, for |
| 136 | +example to display the original\ |
| 137 | +picture while specific formats are being processed. |
| 138 | + |
| 139 | +````ruby |
| 140 | +class Item < ActiveRecord::Base |
| 141 | + has_attached_file :photo |
| 142 | + |
| 143 | + process_in_background :photo, processing_image_url: :processing_image_fallback |
| 144 | + |
| 145 | + def processing_image_fallback |
| 146 | + options = photo.options |
| 147 | + options[:interpolator].interpolate(options[:url], photo, :original) |
| 148 | + end |
| 149 | +end |
| 150 | +```` |
| 151 | + |
| 152 | +#### Have processing? status available, but construct image URLs as if delayed_paperclip wasn’t present |
| 153 | + |
| 154 | +If you define the `#{attachment_name}_processing` column, but set the |
| 155 | +`url_with_processing` option to false, this opens up other options (other than modifying the url that paperclip returns) for giving feedback to the user while the image is processing. This is useful for advanced situations, for example when dealing with caching systems. |
| 156 | + |
| 157 | +Note especially the method #processing? which passes through the value |
| 158 | +of the boolean created via migration. |
| 159 | + |
| 160 | +````ruby |
| 161 | +class User < ActiveRecord::Base |
| 162 | + has_attached_file :avatar |
| 163 | + |
| 164 | + process_in_background :avatar, url_with_processing: false |
| 165 | +end |
| 166 | + |
| 167 | +@user = User.new(avatar: File.new(...)) |
| 168 | +@user.save |
| 169 | +@user.avatar.url #=> "/system/images/3/original/IMG_2772.JPG?1267562148" |
| 170 | +@user.avatar.processing? #=> true |
| 171 | +Delayed::Worker.new.work_off |
| 172 | + |
| 173 | +@user.reload |
| 174 | +@user.avatar.url #=> "/system/images/3/original/IMG_2772.JPG?1267562148" |
| 175 | +@user.avatar.processing? #=> false |
| 176 | +```` |
| 177 | + |
| 178 | +#### Only process certain styles |
| 179 | + |
| 180 | +This is useful if you don’t want the background job to reprocess all |
| 181 | +styles. |
| 182 | + |
| 183 | +````ruby |
| 184 | +class User < ActiveRecord::Base |
| 185 | + has_attached_file :avatar, styles: { small: "25x25#", medium: "50x50x" } |
| 186 | + |
| 187 | + process_in_background :avatar, only_process: [:small] |
| 188 | +end |
| 189 | +```` |
| 190 | + |
| 191 | +Like paperclip, you could also supply a lambda function to define |
| 192 | +`only_process` dynamically. |
| 193 | + |
| 194 | +````ruby |
| 195 | +class User < ActiveRecord::Base |
| 196 | + has_attached_file :avatar, styles: { small: "25x25#", medium: "50x50x" } |
| 197 | + |
| 198 | + process_in_background :avatar, only_process: lambda { |a| a.instance.small_supported? ? [:small, :large] : [:large] } |
| 199 | +end |
| 200 | +```` |
| 201 | + |
| 202 | +#### Split processing |
| 203 | + |
| 204 | +You can process some styles in the foreground and some in the background |
| 205 | +by setting `only_process` on both `has_attached_file` and |
| 206 | +`process_in_background`. |
| 207 | + |
| 208 | +````ruby |
| 209 | +class User < ActiveRecord::Base |
| 210 | + has_attached_file :avatar, styles: { small: "25x25#", medium: "50x50x" }, only_process: [:small] |
| 211 | + |
| 212 | + process_in_background :avatar, only_process: [:medium] |
| 213 | +end |
| 214 | +```` |
| 215 | + |
| 216 | +#### Reprocess Without Delay |
| 217 | + |
| 218 | +This is useful if you don’t want the background job. It accepts |
| 219 | +individual styles to. Take note, normal `reprocess!` does not accept styles as arguments anymore. It will delegate to DelayedPaperclip and |
| 220 | +reprocess all styles. |
| 221 | + |
| 222 | +````ruby |
| 223 | +class User < ActiveRecord::Base |
| 224 | + has_attached_file :avatar, styles: { small: "25x25#", medium: "50x50x" } |
| 225 | + |
| 226 | + process_in_background :avatar |
| 227 | +end |
| 228 | + |
| 229 | +@user.avatar.url #=> "/system/images/3/original/IMG_2772.JPG?1267562148" |
| 230 | +@user.avatar.reprocess_without_delay!(:medium) |
| 231 | +```` |
| 232 | + |
| 233 | +Defaults |
| 234 | +-------- |
| 235 | + |
| 236 | +Global defaults for all delayed_paperclip instances in your app can be |
| 237 | +defined by changing the DelayedPaperclip.options Hash, this can be useful for setting a default ‘processing image,’ so you won’t have to define it in every `process_in_background` definition. |
| 238 | + |
| 239 | +If you’re using Rails you can define a Hash with default options in |
| 240 | +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: |
| 241 | + |
| 242 | +```` |
| 243 | +module YourApp |
| 244 | + class Application < Rails::Application |
| 245 | + # Other code... |
| 246 | +
|
| 247 | + config.delayed_paperclip_defaults = { |
| 248 | + url_with_processing: true, |
| 249 | + processing_image_url: 'custom_processing.png' |
| 250 | + } |
| 251 | + end |
| 252 | +end |
| 253 | +```` |
| 254 | + |
| 255 | +What if I’m not using images? |
| 256 | +----------------------------- |
| 257 | + |
| 258 | +This library works no matter what kind of post-processing you are doing |
| 259 | +with Paperclip. |
| 260 | + |
| 261 | +Does it work with s3? |
| 262 | +--------------------- |
| 263 | + |
| 264 | +Yes. |
| 265 | + |
| 266 | +Contributing |
| 267 | +------------ |
| 268 | + |
| 269 | +Checkout out CONTRIBUTING. In short, you’ll need a redis server running |
| 270 | +for testing. Run all tests with |
| 271 | + |
| 272 | +```` |
| 273 | +# Rspec on all versions |
| 274 | +rake appraisal spec |
| 275 | +# Test Unit on all versions (deprecated) |
| 276 | +rake appraisal test |
| 277 | +```` |
0 commit comments