Rails has_file Plugin

Posted by John Wulff Wed, 09 May 2007 00:48:00 GMT

has_file is an easy-to-use Rails plugin that provides file manipulation methods to an ActiveRecord model.

SVN , Google Code Project

Basic Example
$ cd MyRailsApp
$ script/plugin install http://rails-has-file.googlecode.com/svn/trunk
class Node < ActiveRecord::Base
  has_file :donkey
end
$ script/console
Loading development environment.
>> n = Node.new
  => #nil}, @new_record=true>
>> n.donkey = "secret donkey message" 
  => "secret donkey message" 
>> n.donkey_file_path
  => "/tmp/node-20123394-donkey" 
>> n.donkey
  => "secret donkey message" 
>> n.save!
  => true
>> n.donkey_file_path
  => "/Users/jwulff/Development/rails/resources/node-4-donkey" 
>> n.donkey
  => "secret donkey message" 
>> n.destroy
  => #"2007-05-08 15:07:47"}>
>> File.exists? "/Users/jwulff/Development/rails/resources/node-4-donkey" 
  => false

Some Notes:

  • Temporary files are employed for unsaved records. Meaning you can use the set/get methods before saving the record.
  • By default files are saved to RAILS_ROOT/resources. You can change this by passing a path in along with the declaration like so:
    has_file :donkey, '/var/stuff'
  • Upon destruction of the record
    record.destroy
    associated files will be deleted.
Trackbacks

Use the following link to trackback from your own site:
http://www.johnwulff.com/trackbacks?article_id=13

Leave a comment

Comments