module Ramaze::Helper::Upload::ClassMethods

Helper class methods. Methods in this module will be available in your controller class (not your controller instance).

Public Instance Methods

handle_uploads_for(*args) click to toggle source

This method will activate automatic handling of uploaded files for specified actions in the controller.

@example

class MyController < Ramaze::Controller

  # Use upload helper
  helper :upload

  # Handle all uploads for the foo and bar actions
  handle_uploads_for :foo, :bar

  # Handle all uploads for the baz action and uploads beginning with
  # 'up' for the qux action
  handle_uploads_for :baz, [:qux, /^up.*/]
end

@param [Array] args An arbitrary long list of arguments with action

names (and optionally patterns) that should handle file uploads
automatically. Each argument can either be a symbol or a two-element
array consisting of a symbol and a reqexp.

@see handle_all_uploads @see Ramaze::Helper::Upload#get_uploaded_files

# File lib/ramaze/helper/upload.rb, line 255
def handle_uploads_for(*args)
  args.each do |arg|
    if arg.respond_to?(:first) and arg.respond_to?(:last)
      before(arg.first.to_sym) do
        get_uploaded_files(arg.last)
      end
    else
      before(arg.to_sym) do
        get_uploaded_files
      end
    end
  end
end
upload_options(options) click to toggle source

Sets options for file uploads in the controller.

@example

# This controller will handle all file uploads automatically.
# All uploaded files are saved automatically in '/uploads/myapp'
# and old files are overwritten.
#
class MyController < Ramaze::Controller

  # Use upload helper
  helper :upload

  handle_all_uploads
  upload_options :allow_overwrite => true,
                 :autosave => true,
                 :default_upload_dir => '/uploads/myapp',
                 :unlink_tempfile => true
end

# This controller will handle all file uploads automatically.
# All uploaded files are saved automatically, but the exact location
# is depending on a session variable. Old files are overwritten.
#
class MyController2 < Ramaze::Controller

  # Use upload helper
  helper :upload

  # Proc to use for save directory calculation
  calculate_dir = lambda { File.join('/uploads', session['user']) }

  handle_all_uploads
  upload_options :allow_overwrite => true,
                 :autosave => true,
                 :default_upload_dir => calculate_dir,
                 :unlink_tempfile => true
end

@param [Hash] options Options controlling how file uploads

are handled.

@option options [Boolean] :allow_overwrite If set to true, uploaded

files are allowed to overwrite existing ones. This option is set to
*false* by default.

@option options [Boolean] :autosave If set to true,

Ramaze::Helper::Upload::UploadedFile#save will be called on all
matched file uploads
automatically. You can use this option to automatically save files
at a preset location, but please note that you will need to set the
:default_upload_dir (and possibly :allow_overwrite) options as well
in order for this to work correctly. This option is set to *false*
by default.

@option options [String|Proc] :default_upload_dir If set to a string

(representing a path in the file system) this option will allow you
to save uploaded files without specifying a path. If you intend to
call Ramaze::Helper::Upload::UploadedFile#save with a path you don't
need to set this option at all. If you need to delay the calculation
of the directory, you can also set this option to a proc. The proc
should accept zero arguments and return a string. This comes in handy
when you want to use different directory paths for different users
etc.  This option is set to *nil* by default.

@option options [Boolean] :unlink_tempfile If set to true, this

option will automatically unlink the temporary file created by Rack
immediately after Ramaze::Helper::Upload::UploadedFile#save is done
saving the uploaded file. This is probably not needed in most cases,
but if you don't want to expose your uploaded files in a shared
tempdir longer than necessary this option might be for you. This
option is set to *false* by default.

@see Ramaze::Helper::Upload::UploadedFile#initialize @see Ramaze::Helper::Upload::UploadedFile#save

# File lib/ramaze/helper/upload.rb, line 339
def upload_options(options)
  trait(
    :upload_options => Ramaze::Helper::Upload::ClassMethods.trait[
      :default_upload_options
    ].merge(options)
  )
end