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 ...
Get Professional Ruby on Rails™ 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.