Rails Plugins: attachment_fu
I’ve used Rick Olson’s excellent acts_as_attachment on a number of projects and it’s saved me a huge amount of time that would have been spent worrying about how best to resize images, how to make sure uploaded files are properly written to storage, and other such boring details. So I’ve been noticing with interest increasing references to his new attachment_fu plugin, which is a complete rewrite of acts_as_attachment.
That interest was piqued as I skimmed this nice how-to on file attachment from Mike Clark (if you’re just looking for how to get started, go read that). It seems attachment_fu is almost entirely backwards compatible with acts_as_attachment (you just need to change your models to rename the ‘acts_as_attachment’ declaration ‘has_attachment’), but with a number of enhancements.
First up is the addition of pluggable ‘processors.’ Where acts_as_attachment relied on RMagick to manipulate images (useful things like creating thumbnails), attachment_fu supports ImageScience and MiniMagick, both of which are much lighter on resources than RMagick. And because each processor is implemented as its own module, you can easily add in any image processing library you can write a wrapper for.
So say you wanted to use a custom processor contained in the module Technoweenie::AttachmentFu::Processors::MyCustomProcessor, your model might look like:
class Avatar < ActiveRecord::Base has_attachment :content_type => :image, :processor => :my_custom, :max_size => 500.kilobytes, :resize_to => '240x240>', :thumbnails => { :thumb => '100x100>' } validates_as_attachment end
Similarly, the storage code is also now pluggable. As Mike Clark explains in his tutorial, the first fruits of that is an S3 storage option, allowing all your uploaded files to be stored in your Amazon S3 account. But there’s no reason to stop there. To add your own storage option you’d just need a compatible module and to declare it just as you would a custom processor. For Technoweenie::AttachmentFu::Backends::MyCustomBackend the model would look like:
class Avatar < ActiveRecord::Base has_attachment :content_type => :image, :storage => :my_custom, :max_size => 500.kilobytes, :resize_to => '240x240>', :thumbnails => { :thumb => '100x100>' } validates_as_attachment end
attachment_fu is only compatible with Rails 1.2+ so for those legacy projects still on older versions of Rails, it’s not an option. But for new projects, the ability to use other processors looks like a definite win, and the pluggable architecture is a nice bonus.
More notes from a Rails 3.0pre upgrade
about 2 months ago - No comments
This is a follow-on from my piece on how I got the (development version of) Catapult Magazine up and running with Rails 3.0pre. If you haven’t already done so, I’d recommend you read that first.
Catapult makes use of the permalink_fu plugin which fails in Rails 3. It fails because of a reliance on the evaluate_attribute_method More >
Upgrading an app to Rails 3.0pre
about 2 months ago - 1 comment
I used to be a strong adherent to tracking edge rails. Up until the release of rails 2.3 I let most of my frequently updated projects track edge with a vendored copy of rails, and it rarely caused me any trouble. When 2.3 hit I rethought all that.
With Rails 3 development ramping up I More >
Rails 2.3 final and theme_support updates
about 11 months ago - Comments Off
Somewhere between Ruby on Rails versions 2.3.0 (RC1) and 2.3.2 (final) a change was made to the arguments required for one of the methods the theme_support plugin requires. I must confess I hadn’t spotted it, but github user knapo kindly sent me a message with a patch. That patch is now applied in the main More >
Rails 2.3 and theme_support part 3: Layouts
about 1 year ago - 11 comments
In my ongoing efforts to bring my fork of theme_support in line with Rails 2.3 I’ve covered the core views and email, but when I left off earlier today layouts still weren’t working.
The key problem with overriding layouts is that the process of identifying them relies on some class methods on ActionController::Base (provided in the More >
Rails 2.3 and theme_support part 2: ActionMailer
about 1 year ago - 6 comments
Stage 2 of fixing up theme_support for Rails 2.3 was making sure that ActionMailer picked up themed templates (for stage 1 information see here). That’s something I’d not quite cracked in the 2.2 version, so starting afresh with 2.3 forced me to spend the time to look through the full render path and figure out More >
Rails 2.3 and theme_support
about 1 year ago - 1 comment
A couple of months back, I realised that two of my projects (Generous and Catapult) could do with the help of the theme_support Rails plugin. Discovering that it didn’t play nicely with Rails 2.1, I created a fork on github and hacked at the _pick_template method to get it to do what I wanted. It More >
Rails 2.3 and Grand Rapids WiFi
about 1 year ago - Comments Off
I like to always have at least one project on the go that works as a testbed for a variety of new Rails techniques, tools and releases. For a long time that project was Grand Rapids WiFi, a site that lapsed into some neglect after I moved (a long way) away from the town it More >
Migrating from attachment_fu to paperclip
about 1 year ago - 6 comments
Thoughtbot have released a suite of plugins over the past few months that are enlivening the fields of Ruby on Rails file attachments (paperclip) and authentication (clearance), long dominated by Rick Olson’s attachment_fu and restful_authentication. You can see some previous posts about attachment_fu here and here.
I’ve been playing with paperclip on a couple of projects, More >
New home for Rails ‘geo plugin comparison’
about 1 year ago - 2 comments
About eighteen months ago I compiled a series of reviews of Ruby on Rails plugins concerned with geography. I put together a comparison chart and posted it on this blog. It subsequently found a new home on a wiki, but lately that wiki has rarely been accessible so I decided it was time to move More >
acts_as_amazon_product
about 1 year ago - 1 comment
A couple of years ago I wrote and released a Ruby on Rails plugin called loads_from_amazon. It made it relatively simple to populate a model with data based on an amazon search, and was very helpful in the project I was then working on.
That project ended and I’ve not had time to maintain the plugin More >
Comments are closed.
about 2 years ago
Have you written a custom processor (something not related to image processing). Would be great to have a look at the code if you did.
about 2 years ago
I’m afraid I haven’t. The default processors have served me fine so far. It should be fairly straightforward to follow the code in the defaults and modify from there.
about 2 years ago
@Warren – I just discovered one writeup that you may find helpful. It describes writing an Apple Core Image adapter for attachment_fu
http://redartisan.com/2007/12/12/attachment-fu-with-core-image