Chapter 14. Extending Rails with Plug-ins

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. ...

Get Rails Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.