Module: Ramaze::Helper::Upload::ClassMethods

Includes:
Traited
Defined in:
lib/ramaze/helper/upload.rb

Overview

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

Since:

Instance Method Summary (collapse)

Instance Method Details

- (Object) handle_uploads_for(*args)

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

Examples:

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

Parameters:

  • args (Array)

    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 Also:

Since:

  • 04-08-2011



255
256
257
258
259
260
261
262
263
264
265
266
267
# 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

- (Object) upload_options(options)

Sets options for file uploads in the controller.

Examples:

# 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

Parameters:

  • options (Hash)

    Options controlling how file uploads are handled.

Options Hash (options):

  • :allow_overwrite (Boolean)

    If set to true, uploaded files are allowed to overwrite existing ones. This option is set to false by default.

  • :autosave (Boolean)

    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.

  • :default_upload_dir (String|Proc)

    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.

  • :unlink_tempfile (Boolean)

    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 Also:

Since:

  • 04-08-2011



339
340
341
342
343
344
345
# 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