[special]This article is an excerpt from "Rails: Novice To Ninja" by Glenn Goodrich and Patrick Lenz. You can purchase the full version of this book here.[/special]
Rails Plugins
While this book is unable to cover all of the built-in functionality that ships with Rails—and there's plenty of functionality for you to discover and experiment with once you're beyond the last chapter— the plugins architecture of Rails warrants our attention.
What is a plugin?
A plugin is a component that you can add to your application to extend its functionality. While you can certainly write your own plugins, we'll limit our discussion here to using existing plugins. Plugins have been developed for various parts of the Rails framework, adding functionality such as:
- extensions to ActiveRecord functionality
- helper methods
- new template engines (for coding a view using an alternate templating language)
The number of existing Rails plugins is enormous and grows every day. Programmers in the Ruby and Rails communities are excellent about sharing code and creating useful plugins based on extensions they need. A good resource of existing Rails plugins can be found by searching for "Rails" on the Rubygems site or on the Ruby Toolbox site.
Plugins are distributed as gems, which we covered in Chapter 2. Plugins can be pulled into an existing Rails application by adding them to the Gemfile
and running bundle install
. You probably remember our discussion about Bundler from Chapter 4, where its job is to manage application dependencies. Bundler makes including existing plugins into our app a breeze.
Finding a plugin that does what you require is usually just a Google or RubyGems search away. As seen in Figure 10-1, searching for "rails tagging" brings up a few gems that have been created, including one called acts-as-taggable-on
.
[caption id="attachment_143334" align="alignnone" width="806"] Figure 10-1. Searching for a plugin on "rails tagging"[/caption]
The overwhelming majority of gems keep their source on GitHub, including acts-as-taggable-on
from the first link in our search above. Following that link leads to the source on GitHub, as shown in Figure 10-2.
[caption id="attachment_143336" align="alignnone" width="1024"] 10-2. The GitHub repository for 'acts-as-taggable-on' [/caption]
Most GitHub source repositories have a README
or README.md
file that explains what the gem does, how to install and use it, and so on. acts-as-taggable-on
follows this convention, which can be seen in Figure 10-3. It explains the object of the gem, the supported versions of Rails, as well as how to install and configure the gem.
[caption id="attachment_143337" align="alignnone" width="1024"] 10-3. A standard README file.[/caption]
After reading through the README.md
, we now know how to pull the gem into our application and use its functionality. You may feel that walking through the topic of "how to find and learn about gems" is a bit tedious, but you will find yourself spending loads of time doing just that–so I figured it was tedium well spent.
No Time for Name-calling
There are many ways to extend Rails; for example, by using a "plugin", "engine", and "railtie", to name a few. While there are technical differences between these items, they are often (incorrectly) used interchangeably. Defining these terms and their differences is beyond the scope of this book, so I'm going to stick to the word "plugin" for now. As you grow in your Rails-fu, you'll no doubt want to do some research around Rails extensibility. Boom–I just turned this note into an EXTRA CREDIT!
Okay, enough theory! Let's go ahead and install our first plugin.
Continue reading %From Novice to Ninja: How to Master Using Plugins in Rails%
by Glenn Goodrich via SitePoint
No comments:
Post a Comment