=== NOTE: From version 0.1.5 and above defaults must be in public folder
=== NOTE: From version 0.2.0 and above the use of path is deprecated
---
{}[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
ImageMagick must be install, you can install it with homebrew:
brew install imagemagick
= Usage
Mount the engine at the end of you routes.rb:
mount RailsUploads::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
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
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
config.uploads.storage = :local # The default it's local, you can use :s3 as well
If you want to use S3 create a s3.yml file in your config directory like this:
development:
bucket: development-bucket
access_key_id: development_access_key_id
secret_access_key: development_secret_access_key
test:
bucket: test-bucket
access_key_id: test_access_key_id
secret_access_key: test_secret_access_key
production:
bucket: production-bucket
access_key_id: production_access_key_id
secret_access_key: production_secret_access_key
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.url } # To get the file url
a{ :href => record.image.url } # To get the original image
a{ :href => record.image.url(:big) } # To get the a thumb
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:
config.uploads.base_url = 'http://cdn.example.com'
== Can I automatically create buckets?
Yes, use this rake task after create the s3.yml file:
rake uploads:s3:buckets:create
== How can I clean a preset?
Just remove the corresponding folder in uploads/images manually or with this rake task:
rake uploads:preset:clean NAME=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