Skip to content

roomorama/delayed_paperclip

 
 

Repository files navigation

Delayed::Paperclip

Delayed_paperclip lets you process your Paperclip attachments in a background task with delayed_job, Resque or Sidekiq.

Why?

The most common use case for Paperclip is to easily attach image files to ActiveRecord models. Most of the time these image files will have multiple styles and will need to be resized when they are created. This is usually a pretty slow operation and should be handled in a background task.

I’m sure that everyone knows this, this gem just makes it easy to do.

Installation

Install the gem:

gem install delayed_paperclip

Or even better, add it to your Gemfile.

source "https://rubygems.org"
gem 'delayed_paperclip'

Dependencies:

  • Paperclip
  • DJ, Resque or Sidekiq

Usage

In your model:


  class User < ActiveRecord::Base
    has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }

    process_in_background :avatar
  end

Use your Paperclip attachment just like always in controllers and views.

To select between using Resque or Delayed::Job, just install and configure your choice properly within your application, and delayed_paperclip will do the rest. It will detect which library is loaded and make a decision about which sort of job to enqueue at that time.

Resque

Make sure that you have Resque up and running. The jobs will be dispatched to the :paperclip queue, so you can correctly dispatch your worker. Configure resque and your workers exactly as you would otherwise.

DJ

Just make sure that you have DJ up and running.

Sidekiq

Make sure that Sidekiq is running and listening to the paperclip queue, either by adding it to your sidekiq.yml config file under - queues: or by passing the command line argument -q paperclip to Sidekiq.

Displaying images during processing

In the default setup, when you upload an image for the first time and try to display it before the job has been completed, Paperclip will be none the wiser and output the url of the image which is yet to be processed, which will result in a broken image link being displayed on the page.

To have the missing image url be outputted by paperclip while the image is being processed, all you need to do is add a #{attachment_name}_processing column to the specific model you want to enable this feature for. This feature gracefully degrades and will not affect models which do not have the column added to them.


  class AddAvatarProessingToUser < ActiveRecord::Migration
    def self.up
      add_column :users, :avatar_processing, :boolean
    end

    def self.down
      remove_column :users, :avatar_processing
    end
  end

  @user = User.new(:avatar => File.new(...))
  @user.save
  @user.avatar.url #=> "/images/original/missing.png"
  Delayed::Worker.new.work_off

  @user.reload
  @user.avatar.url #=> "/system/images/3/original/IMG_2772.JPG?1267562148"

Custom image for processing

This is useful if you have a difference between missing images and images currently
being processed.



  class User < ActiveRecord::Base
    has_attached_file :avatar

    process_in_background :avatar, :processing_image_url => "/images/original/processing.jpg"
  end

  @user = User.new(:avatar => File.new(...))
  @user.save
  @user.avatar.url #=> "/images/original/processing.png"
  Delayed::Worker.new.work_off

  @user.reload
  @user.avatar.url #=> "/system/images/3/original/IMG_2772.JPG?1267562148"

Only process certain styles

This is useful if you don’t want the background job to reprocess all styles.


  class User < ActiveRecord::Base
    has_attached_file :avatar, :styles => { :small => "25x25#", :medium => "50x50x" }

    process_in_background :avatar, :only_process => [:small]
  end

Split processing

You can process some styles in the foreground and some in the background by setting only_process on both has_attached_file and process_in_background.


  class User < ActiveRecord::Base
    has_attached_file :avatar, :styles => { :small => "25x25#", :medium => "50x50x" }, :only_process => [:small]

    process_in_background :avatar, :only_process => [:medium]
  end

Reprocess Without Delay

This is useful if you don’t want the background job. It accepts individual styles to. Take note, normal `reprocess!` does not accept
styles as arguments anymore. It will delegate to DelayedPaperclip and reprocess all styles.


  class User < ActiveRecord::Base
    has_attached_file :avatar, :styles => { :small => "25x25#", :medium => "50x50x" }

    process_in_background :avatar
  end

  @user.avatar.url #=> "/system/images/3/original/IMG_2772.JPG?1267562148"
  @user.avatar.reprocess_without_delay!(:medium)

What if I’m not using images?

This library works no matter what kind of post-processing you are doing with Paperclip.

Does it work with s3?

Yes.

Contributing

Checkout out CONTRIBUTING for more info.

About

Process your Paperclip attachments in the background with delayed_job or Resque.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%