Cloudinary Blog

Attachinary - a modern attachments solution for Ruby on Rails

by Nadav Soferman
When developing a website you are required for a somewhat tedious work of handling dynamically uploaded content. The constantly added content includes images uploaded by your users and content administrator, user documents and other files.
 
As a developer, you'll be responsible for adding integration of attachments to your application's model and taking care of the upload, normalization, storage, transformation and delivery of such assets.
 
Over time, we've run into a lot of attachment management libraries for many of the web development frameworks available. For Ruby on Rails alone there are CarrierWave, Paperclip, Dragonfly, attachment_fu and quite a few others. 
 
While Cloudinary streamlines all your image management needs and takes care of uploads, storage, transformations, manipulations and delivery, you still need to integrate it with your application's model. To simplify the integration, Cloudinary offers many client libraries for all major web dev platforms and programming languages. For example, Cloudinary's Ruby GEM includes a plugin that seamlessly integrate with CarrierWave for managing uploads and image transformations in the cloud. We've covered this in a previous blog post.
 
Today, we wanted to tell you about a new attachment management library for Ruby on Rails - Attachinary. Attachinary was developed by Milovan Zogovic and it does an amazing job in simplifying attachment management in your application's model. 
 
We've tried Attachinary, really liked it and wanted to share our experience working with it. 
 

How is Attachinary different? 

There are so many other attachment management libraries, why do we need another one? 
Well, here are a few reasons:
 
  • Attachinary offers non-intrusive integration with ActiveRecord and Mongoid model. You no longer need to maintain DB columns in your model entities for referencing uploaded images.
  • With attachinary it's very easy to switch between a single image and multiple images for a model entity.
  • Attachinary allows for simple integration through Rails 3.2's modern Asset Pipeline.
  • Attachinary uses Cloudinary for uploading files to the cloud, transforming images and delivering files through a fast CDN. No need to install any image manipulation software.
  • Attachinary has built-in integration with Cloudinary's jQuery-based direct upload from the browser. Progress indication and image preview included.
  • Attachinary is extremely simple to use and requires minimal changes to your existing code base.
 

How is Attachinary used?

After a very quick Cloudinary & Attachinary GEM installation and setup, you can add any attachment attribute to your model class. The following code shows a User model entity:
class User < ActiveRecord::Base
  attr_accessible :name
  
  has_attachment  :avatar
  has_attachments :photos, :maximum => 3
end
The 'has_attachment' and 'has_attachments' methods were used to define a User that can have a single 'avatar' and up to 3 'photo' attachments. That's it, no additional migrations or code changes are required.
 
Uploading assets to Cloudinary is also very simple with Attachinary. The following HAML view code shows how to create an upload form. The 'attachinary_file_field_tag' view helper method is responsible for all the magic. When this form is submitted, all images will be automatically uploaded to the cloud directly from your website visitor's browser. Multiple photos can be uploaded from this same form and the identifiers of the uploaded images will be automatically stored in your model.
= form_for(@user) do |user_form|
    = user_form.text_field(:name)
    = attachinary_file_field_tag 'user[avatar]', @user, :avatar
    = attachinary_file_field_tag 'user[photos]', @user, :photos
    
    = user_form.submit("Save")
Model management in your controller is exactly the same as always:
def create
  @user = User.new(params[:user])
  @user.save!
end
You can display uploaded images, generate thumbnails and apply image transformations by using Cloudinary's cl_image_tag. Here's an example of a view code that displays a 80x100 face detection based thumbnail of the user's avatar and a 70x50 rounded corner version of all uploaded photos.
- if user.avatar.present?
  = cl_image_tag(user.avatar.path, :width => 80, :height => 100,
                 :crop => :thumb, :gravity => :face)        
  - user.photos.each do |photo|
    = cl_image_tag(photo.path, :size => '70x50', :crop => :fill, :radius => 20)
Using attachinary allows you to dynamically apply any image transformation supported by Cloudinary. All transformed images are generated on-the-fly in the cloud and are then cached and delivered through a fast CDN.
 
Last but not least - Attachinary also supports non-image raw files.
 

Summary 

We hope that with this quick introduction, we've managed to pique your interest about Attachinary. Together with Cloudinary, Attachinary is really easy to use and also very powerful. Attaching images to your Rails model has never been easier. 
 
For more details, setup instructions and usage examples, check out Attachinary's page at GitHub.
 
If you are a Rails developer, you should definitely give it a try.
 
blog comments powered by Disqus

Recent Blog Posts

Control the zoom level with automatic image cropping

Many websites now offer their users the ability to upload images and profile pictures, making it a challenge for web designers to maintain a certain graphic design and style when subsequently displaying these images. The profile pictures may need to be smartly cropped to focus on the faces, with some sites that prefer close-ups of faces and others that prefer including more background when displaying images of people.

Read more
Introducing cloud based service for video management

They say that a picture is worth a thousand words. For modern websites, a video surely takes the visual impact to a whole new level.

Nowadays, people enjoy the amazing capability of shooting videos with smartphone cameras that easily fit in their back pockets and are accessible nearly everywhere. Modern web applications have an opportunity to dramatically increase their visual impact by showcasing these videos online. Between news reports, user shared video snippets, explainer videos and ad campaigns, we see more and more videos appearing daily in our visited websites.

Read more
Automatic and accurate red eye removal with Cloudinary

Red eye often happens due to the use of flash in low light conditions as the light hits the eye very quickly and into the retina. It then bounces off of the back of the eye and emits a red color due to the blood vessels there. Although more professional modern cameras and flashes generally prevent this from happening, red eye may still occur with simpler, smaller cameras (including smartphones). There are various software solutions for red eye removal available on mobile devices and desktops, some of which require manual processing to get good results.

Read more
How to detect and prevent malware infected user uploads

Social networking sites allow users to upload images or other types of files that are immediately available to other users via news feeds or notifications. In some cases, attackers can directly spread infected files, but more commonly, they leverage the viral effect and the fact that users are simply unaware that their files are infected through sharing and collaborating with others. As a site owner or application developer, it is your responsibility to protect users and prevent these situations from occurring. Fortunately, Cloudinary makes this easier with its Metascan add-on.

Read more