Coder Social home page Coder Social logo

proactivehealth / delayed_paperclip Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jrgifford/delayed_paperclip

0.0 5.0 0.0 363 KB

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

Home Page: http://www.jstorimer.com/ruby/2010/01/30/delayed-paperclip.html

License: MIT License

delayed_paperclip's Introduction

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 AddAvatarProcessingToUser < 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"

You can also define a custom logic for processing_image_url, for example to display the original
picture while specific formats are being processed.


  class Item < ActiveRecord::Base
    has_attached_file :photo

    process_in_background :photo, :processing_image_url => :processing_image_fallback

    def processing_image_fallback
      options = photo.options
      options[:interpolator].interpolate(options[:url], photo, :original)
    end
  end

Have processing? status available, but construct image URLs as if delayed_paperclip wasn’t present

If you define the #{attachment_name}_processing column, but set the 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.

Note especially the method .processing? which passes through the value of the boolean created via migration.



  class User < ActiveRecord::Base
    has_attached_file :avatar

    process_in_background :avatar, :url_with_processing => false
  end

  @user = User.new(:avatar => File.new(...))
  @user.save
  @user.avatar.url #=> "/system/images/3/original/IMG_2772.JPG?1267562148"
  @user.avatar.processing? #=> true
  Delayed::Worker.new.work_off

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

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

Like paperclip, you could also supply a lambda function to define only_process dynamically.


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

    process_in_background :avatar, :only_process => lambda { |a| a.instance.small_supported? ? [:small, :large] : [:large] }
  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)

Defaults

Global defaults for all delayed_paperclip instances in your app can be 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.

If you’re using Rails you can define a Hash with default options in 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:


  module YourApp
    class Application < Rails::Application
      # Other code...

      config.delayed_paperclip_defaults = {
          :url_with_processing => true,
          :processing_image_url => 'custom_processing.png'
      }
    end
  end

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. In short, you’ll need a redis server running for testing. Run all tests with

rake

delayed_paperclip's People

Contributors

scotterc avatar bertg avatar jrgifford avatar jstorimer avatar joshk avatar bigfleet avatar kevinmtrowbridge avatar tommeier avatar neilco avatar riffraff avatar mrnugget avatar aguilarsoto avatar csaura avatar vdaubry avatar timbo avatar dtaniwaki avatar sbkok avatar maintux avatar jgrubbs avatar jrallison avatar grempe-thrillcall avatar endoscient avatar d--j avatar cpjolicoeur avatar nthalk avatar onomojo avatar eagleas avatar

Watchers

 avatar  avatar James Cloos avatar  avatar Semaphore CI User avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.