Skip to Main Content
Professional Ruby on Rails™
book

Professional Ruby on Rails™

by Noel Rappin
February 2008
Intermediate to advanced content levelIntermediate to advanced
479 pages
14h
English
Wrox
Content preview from Professional Ruby on Rails™

15.2. Creating a Plugin

Downloading a plugin is such a simple and quick way to add functionality to your program that it's easy to overlook the value of writing your own plugins. And I don't mean that just in a "there's value in giving back to the Rails community" kind of way — although releasing plugins is a great way get your code out there in the community (disclaimer: as of this writing, I have exactly zero publicly available plugins). Writing your program extensions as plugins is extremely simple, and it's the best way to package snippets of Rails functionality that you might reuse from application to application.

To demonstrate how to write your own plugin, I'll walk you through the process of creating the acts_as_reviewable plugin referenced in the previous chapter. The goal of this plugin is to allow a class to automatically reference a separate table of reviews, aggregate the values, and manage the reviews. This plugin includes a migration and integration with ActiveRecord. Many plugins won't need all of the features discussed here. However, this example should be a good guide toward designing your plugins.

As with so many things in Rails, plugin layout is governed by convention. In this case, the convention is enforced by the following generator:

$ script/generate plugin acts_as_reviewable --with-generator create vendor/plugins/acts_as_reviewable/lib create vendor/plugins/acts_as_reviewable/tasks create vendor/plugins/acts_as_reviewable/test create vendor/plugins/acts_as_reviewable/README ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby on Rails® Bible

Ruby on Rails® Bible

Timothy Fisher
Rails 4 in Action

Rails 4 in Action

Yehuda Katz, Rebecca Skinner, Stephen Klabnik, Ryan Bigg

Publisher Resources

ISBN: 9780470223888Purchase book