Created
May 1, 2013 07:30
-
-
Save basgys/5494162 to your computer and use it in GitHub Desktop.
Upload images in background. This is a draft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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