Module: UploadProgress
- Defined in:
- lib/upload_progress/lib/progress.rb,
lib/upload_progress/lib/upload_progress.rb,
lib/upload_progress/lib/upload_progress_helper.rb
Overview
:nodoc:
Defined Under Namespace
Modules: ClassMethods, UploadProgressHelper Classes: Progress
Class Method Summary collapse
-
.append_features(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#current_upload_id ⇒ Object
Returns the
upload_id
from the query parameters or if it cannot be found in the query parameters, then return thelast_upload_id
. -
#finish_upload_status(client_js_argument = '') ⇒ Object
Overwrites the body rendered if the upload comes from a form that tracks the progress of the upload.
-
#last_upload_id ⇒ Object
Either returns the last saved
upload_id
or looks in the session for the last usedupload_id
and saves it as the intance variable@upload_id
. -
#next_upload_id ⇒ Object
Returns and saves the next unique
upload_id
in the instance variable@upload_id
. -
#upload_progress(upload_id = nil) ⇒ Object
Get the UploadProgress::Progress object for the supplied
upload_id
from the session.
Class Method Details
.append_features(base) ⇒ Object
:nodoc:
2 3 4 5 6 |
# File 'lib/upload_progress/lib/upload_progress.rb', line 2 def self.append_features(base) #:nodoc: super base.extend(ClassMethods) base.helper_method :upload_progress, :next_upload_id, :last_upload_id, :current_upload_id end |
Instance Method Details
#current_upload_id ⇒ Object
Returns the upload_id
from the query parameters or if it cannot be found in the query parameters, then return the last_upload_id
289 290 291 |
# File 'lib/upload_progress/lib/upload_progress.rb', line 289 def current_upload_id params[:upload_id] or last_upload_id end |
#finish_upload_status(client_js_argument = '') ⇒ Object
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'")
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# 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 |
#last_upload_id ⇒ Object
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
283 284 285 |
# 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 |
#next_upload_id ⇒ Object
Returns and saves the next unique upload_id
in the instance variable @upload_id
276 277 278 |
# File 'lib/upload_progress/lib/upload_progress.rb', line 276 def next_upload_id @upload_id = last_upload_id.succ end |
#upload_progress(upload_id = nil) ⇒ Object
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
.
299 300 301 302 |
# 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 |