Acts As Uploaded plugin

Plugin details

This plugin simplifies handling of file uploads in Ruby on Rails applications. It is partly inspired by Rick Olson's attachment_fu plugin, but is mostly an abstraction of my own handling of file uploads. It also differs from and improves upon some areas of that plugin that I'm not so keen on. It is a simpler plugin, but does exactly what I need. YMMV.

Repositorygit://github.com/jcoglan/acts_as_uploaded.git Author James Coglan Tags Upload, acts LicenseMIT

Documentation

Install the plugin:
ruby script/plugin install git://github.com/jcoglan/acts_as_uploaded.git

First: files go in the file system, never in the database. My current thinking is that the database is the wrong place to be putting files, especially binary files. I know, databases have binary column types, but what are you going to do? Run a fulltext index on your binary data? I think not. If you want to index your files through the DB, that's your business, and you should write +after_save+ hooks for that.

Second: I should be able to call my columns whatever I like. Some of us have legacy schemas to deal with and we should be allowed to specify column names. This plugin assumes +filename+ as a default for storing filenames, and that's it. You can change that name if you want, and speicfy columns for storing content type and filesize too.

Third: I want to write clean-as-possible code. Oftentimes I'll write a form that lets you upload several files at once, then it loops through:

  params[:files].each do |file|
    # deal with file...
  end


I don't want to have to care about the fact that +file+ will be an array in the above code, I just want to hand it to my model and be done with it. This plugin lets you write:

  params[:files].each do |file|
    record = MyFancyUpload.new(:uploaded_file => file)
    record.save
  end


and it deals with fishing the file out of that array for you.


Usage
=========

To make this all work, you'll need to call +acts_as_uploaded+ in your model - see the docs for that method for options. Having done that, you can upload files like they were regular old attributes (see the example above). To upload one file from a form:

Controller:

  def create
    @record = MyFancyUpload.new
    if request.post?
      @record = MyFancyUpload.new(params[:record])
      @record.save
    end
  end


View:

  <% form_for(:record, @record, :url => {:action => 'create'}, :html => {:method => 'post'}) do %>
    
    <%= form.file_field(:uploaded_file) %>
  <% end %>


That +uploaded_file+ attribute is how you need to pass the file to the model for it to be processed. Unless you specify them yourself, the uploaded file will set the filename, content type and filesize attributes of the new record for you.

Validation
===============

As detailed under the +acts_as_attachment+ method notes, you can have your model check the content type and filesize of uploads as part of the validation process. You don't need to specify any +validate_+ macros in your model, just specify valid types and sizes in the +acts_as_attachment+ options hash.

In addition to this, the plugin will not let you save a record if doing so will overwrite an existing file. If you need to overwrite, just use update_attribute(:uploaded_file => file) and the new file will be saved (+update_attribute+ skips the validation process).

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: hardway, 5 months ago