Attachie
Declarative and flexible attachments.
Installation
Add this line to your application's Gemfile:
gem 'attachie'
And then execute:
$ bundle
Or install it yourself as:
$ gem install attachie
Usage
First, include Attachie and specify an attachment:
class User
include Attachie
: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 attachie gem ships with the following drivers:
Attachie::FileDriver: To store files on the local file systemAttachie::FakeDriver: To store files in memory (for testing)Attachie::S3Driver: To store files on S3Attachie::SwiftDriver: To store files on an Openstack Swift provider
You can eg use the file system driver:
require "attachie/file_driver"
Attachie.[:driver] = Attachie::FileDriver.new("/path/to/attachments")
class User
include Attachie
:avatar, host: "www.example.com", versions: {
# ...
}
end
Contributing
- Fork it ( https://github.com/mrkamel/attachie/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
