=== NOTE: From version 0.1.3 and above RailsUploads::Engine renamed Rails::Uploads::Engine
---
{}[https://codeclimate.com/github/mattways/rails_uploads] {}[https://travis-ci.org/mattways/rails_uploads] {}[https://gemnasium.com/mattways/rails_uploads]
= Rails Uploads
Minimalistic toolkit to handle file and images uploads using ActiveRecord.
= Install
Put this line in your Gemfile:
gem 'rails_uploads'
Then bundle:
$ bundle
= Usage
Mount the engine at the end of you routes.rb:
mount Rails::Uploads::Engine => '/' # Will be use to generate on the fly missing image presets
Add the column to your table (just a string):
create_table :models do |t|
t.string :prop
end
If you need a file:
class Model < ActiveRecord::Base
attr_accessible :prop
attached_file :prop, :default => 'file.txt' # It's optional set a default file, the path it's relative to the public folder (used to generate the url)
end
If you need a image:
class Model < ActiveRecord::Base
attr_accessible :prop
attached_image :prop, :presets => [:small, :big], :default => 'assets/image.jpg' # It's optional set a default image, the path it's relative to the publica folder (used to generate the url)
end
Define presets in your application.rb
config.uploads.presets = {
:big => { :method => :fit, :width => 1024, :height => 768 }, # Fit will scale the image until it fit in the space without cropping
:small => { :method => :fill, :width => 120, :height => :120 }, # Fill will scale the image to fill all the space and then crop
:custom => proc { |image| image.convert :resize => '100x100' } # ImageMagick wrapper to do whatever you want
}
config.uploads.default_presets = [:small] # Define the default presets for all models with attached images
The validation works very similar to paperclip:
class Model < ActiveRecord::Base
attr_accessible :prop
attached_file :prop
validates :prop, :attachment_presence => true, :attachment_size => { :in => 0..4.megabytes }, :attachment_content_type => { :in => ['txt'] }
end
If you want to translate the errores the keys are:
errors.messages.attachment_presence
errors.messages.attachment_size_in # :less_than and :greater_than
errors.messages.attachment_size_less_than # :less_than
errors.messages.attachment_size_greater_than # :greater_than
errors.messages.attachment_content_type # :types
In your views:
a{ :href => record.file.path } # To get the file
a{ :href => record.image.path } # To get the original image
a{ :href => record.image.path(:big) } # To get the thumb
a{ :href => record.image.url(:big) } # If you want to use a base url
In your forms:
= f.file_field :prop
= FAQ
== How can I use a cdn with this plugin?
Just define a base url in your application.rb and use url method instead of path:
config.uploads.base_url = 'http://example.com'
== How can I clean a preset?
Just remove the corresponding folder in uploads/images manually or with rake task:
rake uploads:preset:clean[preset]
== How to migrate from versions before 0.1.0?
To migrate from versions before 0.1.0 you need to reorganize uploads with this task after define your presets in your application.rb:
rake uploads:migrate