14.0. Introduction

Eventually, you'll want to extend Rails by installing third-party software to accomplish tasks that the Rails framework is not designed to handle. There are several ways to do this. The most common facilities for extending Rails are RubyGems and Rails plug-ins.

The RubyGems package management system is not Rails-specific, but rather a standardized system for managing and distributing Ruby packages, or gems. Many gems are designed specifically for use with Rails. To use a gem in a Rails application, you have to add an include or require directive somewhere in the application. Typically, gems are included with require statements in environment.rb or application.rb such as:

require 'localization'

As of Rails 0.14, the Rails framework has had its own software distribution facility, know as plug-ins.

A plug-in consists of as series of files and directories that each perform a role in the administration or usage of the plug-in. Perhaps the most important file of the plug-in architecture is init.rb, which is read when your Rails application starts up. This file is often used to include other code required by the plug-in. Most plug-ins also have a lib directory, which is automatically added to the application's $LOAD_PATH.

Installing a plug-in is as simple as placing it the vendor/plugins directory and restarting your application. When a Rails application is first loaded, a file named init.rb is run for each plug-in in the plug-ins directory.

Creating a plug-in requires knowledge of the inner workings of the Rails framework and how Ruby allows classes to be redefined at runtime. A generator helps with the initialization of the files required to build a basic plug-in. For example, the generator for a plug-in called acts_as_dictionary lays the following files and directories:

$ ./script/generate plugin acts_as_dictionary
create vendor/plugins/acts_as_dictionary/lib create vendor/plugins/acts_as_dictionary/tasks create vendor/plugins/acts_as_dictionary/test create vendor/plugins/acts_as_dictionary/README create vendor/plugins/acts_as_dictionary/Rakefile create vendor/plugins/acts_as_dictionary/init.rb create vendor/plugins/acts_as_dictionary/install.rb create vendor/plugins/acts_as_dictionary/lib/acts_as_dictionary.rb create vendor/plugins/acts_as_dictionary/tasks/acts_as_dictionary_tasks.rake create vendor/plugins/acts_as_dictionary/test/acts_as_dictionary_test.rb

Another mechanism for extending Rails are Engines. Rails Engines are best described as vertical slices of a Rails framework that are mixed into an existing application. Rails engines have really fallen out of favor but are still used occasionally and are distributed in the form of plug-ins.