Attached
Attached is a Ruby on Rails file attachment tool that lets users upload to the cloud. The gem supports AWS, Google and Rackspace for storage networks by default. It is influenced (and copied) from Paperclip and uses the incredibly awesome Fog library in the back.
Requirements
The gem is tested with Ruby 1.9.2 and Rails 3.0.5 but may well work with other versions of Ruby and Rails.
Installation
gem install attached
Examples
Migration:
class CreateVideo < ActiveRecord::Migration
def self.up
create_table :videos do |t|
t.string :encoding_identifier
t.string :encoding_extension
t.integer :encoding_size
t.
end
end
def self.down
drop_table :videos
end
end
Model:
has_attached :encoding, :styles => {
:mp4_720p => { :extension => '.mp4' },
:mp4_480p => { :extension => '.mp4' },
:ogg_720p => { :extension => '.ogv' },
:ogg_480p => { :extension => '.ogv' },
}
after_save do
remote.encode(self.encoding.url)
end
Form:
<%= form_for @video, :html => { :multipart => true } do |form| %>
<%= form.file_field :encoding %>
<% end %>
View:
<video>
<source src="<%= @video.encoding.url(:mp4_480p) %>" />
<source src="<%= @video.encoding.url(:ogg_480p) %>" />
</video>
Advanced
Validations
# app/models/person.rb
validates_attached_presence :avatar
validates_attached_size :avatar, :in => 2.kilobytes..2.megabytes
Storage
# app/models/person.rb
has_attached :avatar, :medium => :aws, :credentials => "#{Rails.root}/config/aws.yml"
# app/models/person.rb
has_attached :avatar, :medium => :google, :credentials => "#{Rails.root}/config/google.yml"
# app/models/person.rb
has_attached :avatar, :medium => :rackspace, :credentials => "#{Rails.root}/config/rackspace.yml"
# config/initializers/attached.rb
Attached::Attachment.[:medium] = :aws
Attached::Attachment.[:credentials] = "#{Rails.root}/config/aws.yml"
# config/initializers/attached.rb
Attached::Attachment.[:medium] = :google
Attached::Attachment.[:credentials] = "#{Rails.root}/config/google.yml"
# config/initializers/attached.rb
Attached::Attachment.[:medium] = :rackspace
Attached::Attachment.[:credentials] = "#{Rails.root}/config/rackspace.yml"
Processor
# app/models/person.rb
has_attached :avatar, :processor => :image, :styles => {
:small => { :size => '200x200<', :extension => 'png' },
:large => { :size => '400x400>', :extension => 'png' },
:default => { :size => '300x300#' },
}
Aliases
# app/initializer/attached.rb
Attached::Attachment.[:alias] = http://c0378198.cdn2.cloudfiles.rackspacecloud.com/
# app/initializer/attached.rb
Attached::Attachment.[:aliases] = %w(
http://d1kf5um3mxa8kv.cloudfront.net/
http://d3d5wf186mp1rv.cloudfront.net/
http://d2wmfkw6rydl1g.cloudfront.net/
http://d1gqpdc40l7s16.cloudfront.net/
)
Copyright
Copyright © 2010 Kevin Sylvestre. See LICENSE for details.