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.
 

Recent Blog Posts

Automatic visual image enhancement for web apps

Various factors can have an effect on the visual quality of photos captured by a wide variety of digital cameras. Technical limitations of cameras, coupled with changing conditions in which users take photos, results in a wide range of visual quality. Camera-related limitations arise from a combination of poor optics, noisy sensors, and the modest capabilities of mobile camera phones that are used to take photos in conditions that range from bright daylight to indoor scenes with incandescent light or even dark night scenes.

Read more
Automatically create images for Responsive design

Responsive web design is a method of designing websites to provide an optimal viewing experience to users, irrespective of the device, window size, orientation, or resolution used to view the website. A site designed responsively adapts its layout to the viewing environment, resizing and moving elements dynamically and based on the properties of the browser or device the site is being displayed on.

Read more
10 Startups managing images in the cloud, Part 6
Cloudinary’s customers can range in size, be found in a variety of verticals and boast multiple use cases. Large and small, our customers are our backbone and we like to support them by highlighting their stories on our blog.
 
Check out the ten featured startups below who are Cloudinary’s customers and utilizing the service as their image management system of choice.
Read more

How Answers.com manage millions of images

by Orly Bogler
How Answers.com utilizes Cloudinary to manage millions of images

When was the last time you've asked Google about your favorite band, movie star, or personal hobby? I can only assume that one of the first results that came up was from Answers.com. Nearly everyone knows this website, which is on the Quantcast Top 10 most visited sites in the world.

Read more
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