百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

attachinary

> 编程语言
开源

用于 Rails 的附件处理器,它使用 Cloudinary 进行存储。

294 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

用于 Rails 的附件处理器,它使用 Cloudinary 进行存储。

Attachinary

Note: v1 is not backward compatible. Refer to wiki on how to upgrade.

Need lightweight attachment (photos and raw files) handler for any of your model, in either has_one or has_many relation, without altering your models' schema and with zero effort? Attachinary is the tool for you!

Why is Attachinary different:

  • Supports both ActiveRecord and Mongoid ORMs!
  • No need to alter your model schema every time you introduce new kind of attachment.
  • Handles both has_one and has_many use cases.
  • No need for ImageMagick (or similar) - your thumbnails are generated on the fly by Cloudinary.
  • Fully customizable, custom jQuery plugin for async file uploads with previews.
  • Files are uploaded directly to Cloudinary completely bypassing your app (without affecting its performance).
  • Very easy to use. Once set up, 1 line is enough to add attachment support to your model. No migrations, no Uploaders.
  • Lightweight form submission. Attachinary handles file upload asynchronously and the only thing that is passed to your server is metadata. That makes form postbacks fast and reliable.
  • Benefits of jQuery File Upload (drag'n'drop, selecting multiple files, progress indicators.. etc)
  • All the benefits of Cloudinary (resizing, cropping, rotating, rounding corners, face detection...).

Attachinary uses Cloudinary service. Gem is structured as mountable rails engine.

Installation

First, make sure that you have cloudinary gem installed and properly configured.

Add following line to your Gemfile:

gem 'attachinary'

Specify which ORM you wish to use by adding following line to your application.rb file (or custom initializer):

require "attachinary/orm/YOUR_ORM" # active_record or mongoid

If you're using ActiveRecord ORM, then run following lines to generate required table:

rake attachinary:install:migrations
rake db:migrate

Next, add following line in your routes.rb file:

mount Attachinary::Engine => "/attachinary"

It will generate '/attachinary/cors' which will be used for iframe file transfers (for unsupported browsers).

Finally, make sure that you have following line in head section of your application layout file:

Usage

Lets say that we want all of our users to have single avatar and many photos in their gallery. We also want avatar to be required. We also want to limit the number of photos user can upload to 10. We can declare it like this:

ruby
class User 

If you're using SimpleForm, you can even shorten this to:

erb

Finally, you have to include necessary javascript files. In your application.js, add following lines:

javascript
//= require jquery.ui.widget
//= require jquery.iframe-transport
//= require jquery.fileupload
//= require cloudinary/jquery.cloudinary
//= require attachinary

If you don't have the jQuery File Upload files, you can use following rake task to fetch (or update) them:

rake attachinary:fetch_fileupload

And, add this code on document ready:

javascript
$('.attachinary-input').attachinary()

Attachinary jquery plugin is based upon jQuery File Upload plugin but without any fancy UI (it leaves it up to you to decorate it).

Plugin is fully customizable. It uses John Resig's micro templating in the background, but you can override it with whatever you like. Check out the source code for more configuration options you can set.

Displaying avatar and photos

Here comes the good part. There is no need to transform images on your server. Instead, you can request image transformations directly from Cloudinary. First time you request image, it is created and cached on the Cloudinary server for later use. Here is sample code that you can use in your _user.html.erb partial:

erb

	

	

Avatar will be automatically cropped to 50x50px to show only user face. You read it right: face detection :) All other user photos are just cropped to fit within 125x125.

Whenever you feel like changing image sizes, you don't need to set rake task that will take forever to re-process thousands of photos. You just change the dimension in your partial and thats it.

Additional methods

…

No-JS usage

If you don't want fancy JS features, all you have to do is just switch to :input file field:

erb

Preventing n+1 queries when loading attachinary associations

You can eager load attachinary associations.

For example, I have a user model that has photo and avatar as attachments.

rb
# user.rb
  has_attachment :avatar
  has_attachments :photos

# users_controller.rb
  User.includes(:avatar_files, :photo_files).all

Conventions

  • always use singular identifier after has_attachment (e.g. has_attachment :photo)
  • always use plural identifier after has_attachments (e.g. has_attachments :images)
  • do not use colliding identifiers (e.g. has_attachment :photo and has_attachments :photos) on same model.

Requirements and Compatibility

  • Cloudinary
  • Ruby 1.9
  • Rails 3.2+
  • jQuery

Browser Compatibility

Attachinary jquery plugin uses JSON2 to generate JSON data. This works for all major browsers, but if you wish to support older ones (e.g. IE7-), include json2.js.

Credits and License

Developed by Milovan Zogovic.

This software is released under the MIT License.

Issues· 92 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言