Creating a minimal Rails plugin
$ rails myapp
$ cd myapp
$ script/generate plugin myplugin
To load the plugin automatically add this line in vendor/plugins/myplugin/init.rb
require 'myplugin'
Now most action happens in vendor/plugins/myplugin/lib/myplugin.rb. Suppose we’d like to add a class method to ActiveRecord:
Now in the model definition we can use the plugin this way:
A useful technique we might need is backing up the class methods if we’re redefining them. The alias_method method is quite useful: