class AsyncPaperclipUploader::Temporary

Public Class Methods

base_path() click to toggle source
# File lib/async_paperclip_uploader/temporary.rb, line 35
def self.base_path
  ENV["FILE_UPLOAD_STAGING_DIR"]
end
new(object, attribute, uploaded_file) click to toggle source
# File lib/async_paperclip_uploader/temporary.rb, line 3
def initialize(object, attribute, uploaded_file)
  @object = object
  @attribute = attribute
  @uploaded_file = uploaded_file
end

Public Instance Methods

call() click to toggle source
# File lib/async_paperclip_uploader/temporary.rb, line 9
def call
  if valid?
    upload
    run_permanent_upload_job
  end
end
valid?() click to toggle source
# File lib/async_paperclip_uploader/temporary.rb, line 16
def valid?
  valid_class_attr? && valid_attachment? 
end
valid_attachment?() click to toggle source
# File lib/async_paperclip_uploader/temporary.rb, line 24
def valid_attachment?
  attachment = {
    :filename => @uploaded_file[:filename],
    :content_type => @uploaded_file[:type],
    :headers => @uploaded_file[:head],
    :tempfile => @uploaded_file[:tempfile]
  }
  @object.send("#{@attribute}=", ActionDispatch::Http::UploadedFile.new(attachment))
  @object.valid?
end
valid_class_attr?() click to toggle source
# File lib/async_paperclip_uploader/temporary.rb, line 20
def valid_class_attr?
  @object.respond_to?(@attribute.to_sym)
end

Private Instance Methods

base_job_class_name() click to toggle source
# File lib/async_paperclip_uploader/temporary.rb, line 63
def base_job_class_name
  'UploadJob'
end
job_class() click to toggle source
# File lib/async_paperclip_uploader/temporary.rb, line 58
def job_class
  name = "#{@object.class.name}#{base_job_class_name}"
  Object::const_get(name)
end
path() click to toggle source
# File lib/async_paperclip_uploader/temporary.rb, line 44
def path
  extname = File.extname(@uploaded_file[:filename])
  name = "#{@attribute}-#{@object.id}#{extname}"
  File.join(self.class.base_path, name)
end
run_permanent_upload_job() click to toggle source
# File lib/async_paperclip_uploader/temporary.rb, line 54
def run_permanent_upload_job
  job_class.perform_async(@object.id, @attribute, path)
end
tempfile() click to toggle source
# File lib/async_paperclip_uploader/temporary.rb, line 50
def tempfile
  @uploaded_file[:tempfile]
end
upload() click to toggle source
# File lib/async_paperclip_uploader/temporary.rb, line 40
def upload
  File.open(path, "wb") { |f| f.write(tempfile.read) }
end