Writing Your Own Plugins

To create a plugin, type this from your application’s root directory:

./script/generate plugin {plugin-name}

The code generator creates a directory under vendor/plugins with the name you specified in the command. You should see the output shown in Example 2-1. The minimum set of files you need to modify is shown in bold.

Example 2-1. Output from generating stub files

ChakBookPro:example chak$ ./script/generate plugin my_plugin
      create  vendor/plugins/my_plugin/lib
      create  vendor/plugins/my_plugin/tasks
      create  vendor/plugins/my_plugin/test
      create  vendor/plugins/my_plugin/README
      create  vendor/plugins/my_plugin/MIT-LICENSE
      create  vendor/plugins/my_plugin/Rakefile
      create  vendor/plugins/my_plugin/init.rb
      create  vendor/plugins/my_plugin/install.rb
      create  vendor/plugins/my_plugin/uninstall.rb
      create  vendor/plugins/my_plugin/lib/my_plugin.rb
      create  vendor/plugins/my_plugin/tasks/my_plugin_tasks.rakecreate  vendor/plugins/my_plugin/test/my_plugin_test.rb

The most important files for 99% of the plugins you will write are in bold. Unfortunately, the two most important files, init.rb and the plugin code itself, in lib/my_plugin.rb (substitute your own plugin’s name here), are empty. There is no guidance whatsoever on which to base plugins, the most important part of your architectural strategy.

In init.rb, the code generator gave us the following:

# Include hook code here

In lib/my_plugin.rb, the start we get is similarly useless:

# MyPlugin

In fact, all of the files generated ...

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