Ruby on Rails feels so good because is it fast work with, and you are able to create substantial results in no time. The flexibility and pragmatism of Ruby and the extensive framework and conventions of Rails of course has a huge part in this, but all the great plugins that clever and kind developers are willing to share also plays a vital role in the “Rails feeling”.

I find myself using the same plugins over and over through different projects, and I thought I’d just write a bit about three of the ones I use the most, so others might also realize that these plugins can fill a common need in their Rails applications. All three plugins are MIT licensed and production ready.

Exception Notifier
Jamis Buck’s Exception Notifier plugin is a classic amongst plugins and is part of the “almost-made-it-into-core” plugin package in the official Rails repository. It simply enables your Rails application to compile an email packed with all kind of information and send to one or more recipients each time an unhandled error occurs on your site. For larger apps and teams some kind of error database is probably more appropriate, but in any case it is of vital sense to be aware of errors and bugs in your applications, and to have as much information about them as possible.

Now, there is one catch with the Exception Notifier plugin. It doesn’t play nice with Radiant CMS, so if you use that and want to get notified of errors (which of course you do!), you’ll have to put in a minor workaround. The problem is that Radiant alters where views are found to support extensions, and Exception Notifier also expects a helper to be included automatically, which it isn’t.

First we need to override the Radiant method that locates mailer views so it can find the one of Exception Notifier. Second we need to let the ExceptionNotifier class explicitly include the helper it need. Create a file in your lib-directory with the contents below, and include it in environment.rb:

module Radiant
  module MailerViewPathsExtension

    # Need to override this method for Exception Notifer and other plugins that sends emails to work properly
    def full_template_path(template)
      view_paths.each do |path|
        full_path = File.join(path, template)
        return full_path unless Dir["#{full_path}.*"].empty?
      end
      # If none of the view paths decided by Radiant fitted, try the default for the mailer
      return File.join(template_root, template) if self.respond_to?(:template_root)
      throw Exception.new "Unable to locate path for the ActionMailer template #{template}"
    end

  end
end

module ExceptionNotifierExt

  def self.included(base)
    base.class_eval {
      include InstanceMethods
      alias_method_chain :initialize_template_class, :includes
    }
  end

  module InstanceMethods
    def initialize_template_class_with_includes(assigns)
      template = initialize_template_class_without_includes(assigns)
      class << template
        include ExceptionNotifierHelper
      end
      template
    end
  end

end

ExceptionNotifier.send :include, ExceptionNotifierExt if defined?(ExceptionNotifier)

Simple HTTP auth
You often find yourself creating Rails applications where the contents are for everyone to see, but only for a few select to modify. It’s really not that hard to add users, roles, registration- and login forms, forgot password and user administration pages, and so on, but it takes time. Sometimes it is sufficient to just secure certain actions with a fixed username and password, and this is where the Simple HTTP auth plugin comes in. It lets you protect select controllers and actions with basic HTTP authentication, which is the box that pops up and requires a username and password to continue. I am aware that another plugin exist in the “almost-made-it-into-core” plugin package I mentioned earlier that does exactly the same thing, however, this plugin requires you to run Edge Rails and I usually don’t. (In fact, if look at the code in that other plugin, you’ll see that it just forwards to methods that are built into Rails 2.0.)

While this plugin works straight out the box, and even allows you to lookup usernames and password from a database if you want to do that, it makes it a bit harder to test the controllers and actions it is protecting. Once you start applying http authentication to your controllers, you’ll naturally see your functional tests starting to fail, as they get 401 Not Authorized responses generated by the plugin. What I usually do is to add a login method to my test_helper.rb file, and call this method in my tests; either in the setup method or before I do a get or post to a protected action.

# Logs in using basic authentication
def login
  @request.env['HTTP_AUTHORIZATION'] = "Basic #{Base64.encode64('my_username:my_password')}"
end

Localization Simplified
This is a perhaps less well-known plugin created by one of my fellow geeks in the Copenhagen Ruby Brigade, and it is used for localizing application that are in one language only - a language that is not English. Localization Simplified allows you to set the language of your application once and for all in environment.rb, and then makes sure that all ActiveRecord error messages, dates and times and currency localized according to that language. It also makes sure everything runs UTF-8.

Currently 14 languages are supported, amongst them all the Scandinavian languages (Danish, Swedish, Norwegian and Finnish) and all the major European languages (English, German, French, Spanish and Italian). If you are creating an application in a single language only, I can highly recommended this plugin over more heavy plugins such as Globalize or Gibberish.