At 37signals we usually use five or six plugins in each app. — David Heinemeier Hansson at Canada on Rails
Plugins are self-contained libraries made specially for Rails. They are a great way to reuse someone else’s code or to package your own code for reuse.
This is the first of a three part tutorial on writing plugins for Ruby on Rails.
Unlike gems, plugins are installed directly into a specific Rails app. This makes it easier to deploy them remotely along with an entire application.
I mentioned previously that the following places are good sources for plugins:
Once you’ve found a plugin that does what you want, install it into your existing Rails app like this:
# Install a plugin from a known source ./script/plugin install calendar_helper # Similar on Windows ruby script/plugin install calendar_helper # Install from a specific url ./script/plugin install http://topfunky.net/svn/plugins/calendar_helper # Install by linking it directly within Subversion ./script/plugin install -x calendar_helper
If the script doesn’t automatically find the plugin, run the discovery action which looks for all the sources on the Rails wiki:
# Import new plugin sources ./script/plugin discover
There are also a few other arguments to the
plugin script. Run
./script/plugin with no arguments to see all the options.
svn propdel svn:externals vendor/plugins/
Just like riding a bike or learning to swim, being able to write a plugin is a great skill. If you’ve written any Ruby code at all, you’re halfway there.
There are a few things that can only be done with a plugin (like writing your own generator). For the rest, you’ll appreciate the ability to share common code between your applications or with other developers.
Plugins can do almost anything that a Rails app can, plus a little more. Here’s a short summary of the kinds of things a plugin can do:
libfolder and you’ll be ready to go.
mixin). Also quite easy.
app/controllersdirectory. Intermediate difficulty.
.rakefile into the
tasksfolder and you can reuse your tasks!
publicdirectory. Intermediate difficulty.
You can see the basic layout of a plugin by running the standard plugin generator:
# Use the generator to make a transmogrifier plugin ./script/generate plugin transmogrifier create vendor/plugins/transmogrifier/lib create vendor/plugins/transmogrifier/tasks create vendor/plugins/transmogrifier/test create vendor/plugins/transmogrifier/README create vendor/plugins/transmogrifier/Rakefile create vendor/plugins/transmogrifier/init.rb create vendor/plugins/transmogrifier/install.rb create vendor/plugins/transmogrifier/lib/transmogrifier.rb create vendor/plugins/transmogrifier/tasks/transmogrifier_tasks.rake create vendor/plugins/transmogrifier/test/transmogrifier_test.rb
Here’s the basic folder structure:
transmogrifier |-- init.rb |-- install.rb |-- Rakefile |-- README |-- lib/ | |-- transmogrifier.rb |-- tasks/ | |-- transmogrifier_tasks.rake |-- test | |-- transmogrifier_test.rb
None of these files are necessary, but they each do different things. You might write a plugin that only has a
tasks folder, or another that only has a
lib folder. Here is what each piece does:
.rakefile here as you would in
lib/tasks. It’s automatic…no other action is needed!
Next week I’ll step through the basics of making a plugin from scratch!