Plug-ins and Gems

When you create a reasonably complicated web application, you will probably need all kinds of functionality. Examples:

  • You might need to attach files to a User model in order for your users to have photos as part of their profile.
  • Users of your application will need to log in and out, and your system will need to be able to track who is logged in and who is not.
  • When you write automated tests for your application, you will need a way to quickly and easily create tests objects without having to specify every field each time.

This is an absurdly small list of the kinds of functionality that virtually every well-written application will need. You can write each piece of this yourself, from scratch, or you can save yourself a lot of grief and time, and use software that is already written, tested, documented, and available for free from the generous open-source community. Smart developers opt for the latter, with some caveats.++

There are two broad categories for this type of code - Ruby Gems and Rails Plug-ins. Both types make methods available to you for use in your application with minimal integration overhead. Both are (hopefully) already well-tested, so you wouldn't need to write additional automated tests to use them.

Plug-ins

Plug-ins are Ruby mini-apps that you add directly to your application. The source code for a plug-in is stored in your application tree, in /vendor/plugins. You install plug-ins from the command line, at your application's root directory, as with:

script/plugin install git://github.com/thoughtbot/paperclip.git

This will create a directory called paperclip in your application's /vendor/plugins directory, with the class libraries and supporting tests for the popular file-attachment plug-in Paperclip. That's all you need to do - code in the plugins directory is automatically available to use within your models, views, and controllers.

Gems

Gems are similar to plug-ins except that they are not stored within your application; instead, they are stored directly on your development computer (and so, must be installed on the server when you push to your staging and production environments). In order to make the code from a gem available to your application, you have to require it somewhere in your application. You can require a gem in individual models:

class User < ActiveRecord::Base
  require 'paperclip'
end

or in a controller:

class UsersController < ApplicationController
  require 'paperclip'
end

but the simplest way is probably to just add it to your /config/environment.rb file:

Rails::Initializer.run do |config|
  config.gem 'paperclip', :version => '>= 2.3'
  ..
end

In this way, you not only specify which gems you want to use, you specify the version as well. Adding the config line to your environment.rb file means that the code from the gem is available to your entire application.

Which is better?

There are pros and cons to both approaches. Which you should use depends on your particular situation.

Advantages of Using Gems

  • Not including the libraries in your actual application makes it leaner and therefore quicker to check in and out of a code repository.
  • Smaller applications use less storage space.
  • Allows you to use the library in multiple applications while only installing it once.

Advantages of Using Plug-ins

  • You are likely to have fewer problems working with a distributed team that might have incompatible versions of the same software installed.
  • You don't have to remember to include the software - it is automatically available to use.
  • Limits the potential for naming-collision problems (when more than one application have a same-named method).

Furthermore, some code libraries are not available in both formats. In that case, you will have to use the format offered or not use it at all.

++ Any time you use code written by someone else, you are at their mercy in terms of code quality, thoroughness of testing, and security vulnerabilities. You should always do a thorough review of any software you install for use with a web application, especially for one that will process sensitive personal information.

Additional Reading:

Also available in: HTML TXT