Attachments
Declarative and flexible attachments.
Installation
Add this line to your application's Gemfile:
gem 'attachments'
And then execute:
$ bundle
Or install it yourself as:
$ gem install attachments
Usage
First, include Attachments
and specify an attachment:
class User
include Attachments
attachment :avatar, versions: {
icon: { path: "users/:id/avatar/icon.jpg" },
thumbnail: { path: "users/:id/avatar/thumbnail.jpg" },
original: { path: "users/:id/avatar/original.jpg" }
}
end
Second, store blobs for your version:
user.avatar(:icon).store("blob")
user.avatar(:thumbnail).store("blob")
user.avatar(:original).store("blob")
or via multipart upload
user.avatar(:icon).store_multipart do |upload|
upload.upload_part "chunk1"
upload.upload_part "chunk2"
# ...
end
Third, add the images url to your views:
image_tag user.avatar(:thumbnail).url
More methods to manipulate the blobs:
user.avatar(:icon).delete
user.avatar(:icon).exists?
user.avatar(:icon).value
user.avatar(:icon).temp_url(expires_in: 2.days) # Must be supported by the driver
Drivers
The attachments
gem ships with the following drivers:
Attachments::FileDriver
: To store files on the local file systemAttachments::FakeDriver
: To store files in memory (for testing)Attachments::S3Driver
: To store files on S3Attachments::SwiftDriver
: To store files on an Openstack Swift provider
You can eg use the file system driver:
require "attachments/file_driver"
Attachments.default_options[:driver] = Attachment::FileDriver.new("/path/to/attachments")
class User
include Attachments
attachment :avatar, host: "www.example.com", versions: {
# ...
}
end
Contributing
- Fork it ( https://github.com/mrkamel/attachments/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request