module UploadProgress
Public Instance Methods
Returns the upload_id
from the query parameters or if it cannot be found in the query parameters, then return the last_upload_id
# File lib/upload_progress/lib/upload_progress.rb, line 289 def current_upload_id params[:upload_id] or last_upload_id end
Overwrites the body rendered if the upload comes from a form that tracks the progress of the upload. After clearing the body and any redirects, this method then renders the helper finish_upload_status
This method only needs to be called if you wish to pass a javascript parameter to your finish event handler that you optionally define in form_with_upload_progress
Parameter:¶ ↑
- client_js_argument
-
a string containing a Javascript expression that will be evaluated and passed to your
finish
handler ofform_tag_with_upload_progress
.
You can pass a String, Number or Boolean.
Strings¶ ↑
Strings contain Javascript code that will be evaluated on the client. If you wish to pass a string to the client finish callback, you will need to include quotes in the client_js_argument
you pass to this method.
Example¶ ↑
finish_upload_status("\"Finished\"") finish_upload_status("'Finished #{@document.title}'") finish_upload_status("{success: true, message: 'Done!'}") finish_upload_status("function() { alert('Uploaded!'); }")
Numbers / Booleans¶ ↑
Numbers and Booleans can either be passed as Number objects or string versions of number objects as they are evaluated by Javascript the same way as in Ruby.
Example¶ ↑
finish_upload_status(0) finish_upload_status(@document.file.size) finish_upload_status("10")
Nil¶ ↑
To pass nil
to the finish callback, use a string “undefined”
Example¶ ↑
finish_upload_status(@message || "undefined")
Redirection¶ ↑
If you action performs a redirection then finish_upload_status
will recognize the redirection and properly create the Javascript to perform the redirection in the proper location.
It is possible to redirect and pass a parameter to the finish callback.
Example¶ ↑
redirect_to :action => 'show', :id => @document.id finish_upload_status("'Redirecting you to your new file'")
# File lib/upload_progress/lib/upload_progress.rb, line 255 def finish_upload_status(client_js_argument='') if not @rendered_finish_upload_status and params[:upload_id] @rendered_finish_upload_status = true # erase_render_results # location = erase_redirect_results || '' ## TODO determine if #inspect is the appropriate way to marshall values ## in inline templates template = "<%= finish_upload_status({" template << ":client_js_argument => #{client_js_argument.inspect}, " template << ":redirect_to => #{location.to_s.inspect}, " template << "}) %>" render({ :inline => template, :layout => false }) end end
Either returns the last saved upload_id
or looks in the session for the last used upload_id
and saves it as the intance variable @upload_id
# File lib/upload_progress/lib/upload_progress.rb, line 283 def last_upload_id @upload_id ||= ((session[:uploads] || {}).keys.map{|k| k.to_i}.sort.last || 0).to_s end
Returns and saves the next unique upload_id
in the instance variable @upload_id
# File lib/upload_progress/lib/upload_progress.rb, line 276 def next_upload_id @upload_id = last_upload_id.succ end
Get the UploadProgress::Progress
object for the supplied upload_id
from the session. If no upload_id
is given, then use the current_upload_id
If an UploadProgress::Progress
object cannot be found, a new instance will be returned with total_bytes == 0
, started? == false
, and finished? == true
.
# File lib/upload_progress/lib/upload_progress.rb, line 299 def upload_progress(upload_id = nil) upload_id ||= current_upload_id session[:uploads] && session[:uploads][upload_id] || UploadProgress::Progress.new(0) end