ModelAttachment
ModelAttachment is intended as an simple file attachment library for ActiveRecord. This is experimental software, the interface is subject to change and we are still adding tests. Use with caution only in development.
See the documentation for has_attachement
in ModelAttachment::ClassMethods for slightly more detailed options.
Quick Start
In your model:
class Document < ActiveRecord::Base
:path => ":domain/:folder/:document/:version/",
:types => {
:small => { :command => 'convert -geometry 100x100' },
:large => { :command => 'convert -geometry 500x500' }
},
:aws => :default,
:logging => true
def domain
# returns string representing the domain
end
def folder
# returns string representing the folder
end
def document
# returns string representing document
end
def version
# returns document version number
end
end
In your migrations:
class AddAttachmentColumnsToDocument < ActiveRecord::Migration
def self.up
add_column :documents, :bucket, :string # if you are using aws
add_column :documents, :file_name, :string
add_column :documents, :content_type, :string
add_column :documents, :file_size, :integer
add_column :documents, :version, :integer
add_column :documents, :updated_at, :datetime
end
def self.down
remove_column :documents, :bucket
remove_column :documents, :file_name
remove_column :documents, :content_type
remove_column :documents, :file_size
remove_column :documents, :version
remove_column :documents, :updated_at
end
end
In your edit and new views:
<% form_for :document, @document, :url => document_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :file_name %>
<% end %>
In your controller:
def create
@document = Document.create( params[:user] )
end
def send
# check permissions
@document = Document.find(params[:id])
# use x_send_file w/ apache for better results - http://github.com/simmerz/x_send_file
x_send_file(@document.full_filename(params[:type]), :type => @document.content_type, :disposition => 'inline')
end
In your show view:
<%= link_to "Download", @document.url(:large) %>
Usage
The basics of model_attachment are quite simple: Declare that your model has an attachment with the has_attachment method, and give it a name. ModelAttachment will wrap up up to four attributes and give the a friendly front end. The attributes are file_name, file_size, content_type, and updated_at.
Attachments can be validated with ModelAttachment’s validation methods, validates_attachment_presence and validates_attachment_size.
Attachments can be moved to amazon with @document.move_to_amazon and from amazon with @document.move_to_filesystem.
You can use @document.url(:type) to get the url to the file.
Storage
The files that are assigned as attachments are, by default, placed in the directory specified by the :path option to has_attachment. By default, this location is “:rails_root/system/:document/:basename.:extention”.
Options currently accepted and evaluated: :domain :folder :document :version
Post Processing
ModelAttachment supports post processing by sending the types a command, this will be run on any images.
Credit
This is a blatant strip down of the Paperclip module by Jon Yurek and thoughtbot, inc.