Skip to content

Instantly share code, notes, and snippets.

@basgys
Created May 1, 2013 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basgys/5494162 to your computer and use it in GitHub Desktop.
Save basgys/5494162 to your computer and use it in GitHub Desktop.
Upload images in background. This is a draft
# has_attached_file :image, configuration
# background_upload_for :image
module PaperclipBackgroundUpload
extend ActiveSupport::Concern
included do
def background
@background || false
end
def upload_stopped
@upload_stopped || false
end
def stop_foreground_upload
self.upload_stopped = true
false unless background
end
def request_upload
if upload_stopped
Resque.enqueue(
ImageUploadWorker,
self.class.paperclip_background_model,
self.class.paperclip_background_field,
self.id
)
self.upload_stopped = false
end
end
end
module ClassMethods
def background_upload_for(field)
self.paperclip_background_model = self.to_s.split("::").first
self.paperclip_background_field = field
after_commit :request_upload
attr_accessor :background, :upload_stopped
public_send "before_#{field}_post_process", :stop_foreground_upload # Paperclip callback
end
attr_accessor :paperclip_background_model, :paperclip_background_field
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment