Rails Plugin Tutorial

There are other Rails Plugin tutorials out there but I found holes in the details they provided so I felt I need to write this Ruby on Rails plugin tutorial. This tutorial is written with the experience of developing the acts_as_commentable plugin.

To get started you will need to create your plugin directory under the vendor/plugins for your Rails application. You will need a init.rb file and a lib directory. Here are

To get started run the generate plugin script to create the necessary directory structure in your Rails application.

script/generate plugin acts_as_commentable

This command should have created the following files and directories:

create  vendor/plugins/acts_as_commentable/lib
create  vendor/plugins/acts_as_commentable/tasks
create  vendor/plugins/acts_as_commentable/test
create  vendor/plugins/acts_as_commentable/README
create  vendor/plugins/acts_as_commentable/Rakefile
create  vendor/plugins/acts_as_commentable/init.rb
create  vendor/plugins/acts_as_commentable/install.rb
create  vendor/plugins/acts_as_commentable/lib/acts_as_commentable.rb
create  vendor/plugins/acts_as_commentable/tasks/acts_as_commentable_tasks.rake
create  vendor/plugins/acts_as_commentable/test/acts_as_commentable_test.rb

In this tutorial we are just going to edit init.rb and acts_as_commentable.rb. We are also going to add a comment.rb file for our ActiveRecord model.

For the Rails environment to find you plugin correctly you need to edit the init.rb. For the acts_as_commentable plugin I had to add the following lines:

require 'acts_as_commentable'
ActiveRecord::Base.send(:include, Juixe::Acts::Commentable)

These two lines of code will enhance the ActiveRecord class to include the methods defined in the Juixe::Acts:Commentable module. The Juixe::Acts::Commentable module contains most of the implementation for this plugin.

Now let me define the basic structure of the acts_as_commentable plugin.

module Juixe
  module Acts #:nodoc:
    module Commentable #:nodoc:

      def self.included(base)
        base.extend ClassMethods
      end

      module ClassMethods
        def acts_as_commentable
          include Juixe::Acts::Commentable::InstanceMethods
          extend Juixe::Acts::Commentable::SingletonMethods
        end
      end

      module SingletonMethods
        # Add class methods here
      end

      module InstanceMethods
        # Add instance methods here
      end
    end
  end
end

Even though I still haven’t defined any methods there is a lot going on in this stub. The include and extend functions add functionality to a class or module from the named module. I have have written extensively about mixin modules with classes with include and extend in Ruby Mixin Tutorial.

Just to flush out the stub for this plugin let add and edit the Comment ActiveRecord class in comment.rb file.

class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true
end

At this point any model can act as commentable by just adding one line in you ActiveRecord class. Lets say you are writing a blog application you maybe define a Post mode as such:

class Post < ActiveRecord::Base
  acts_as_commentable
end

At this point you have a working plugin. You can add static and instance methods in the SingletonMethods and InstanceMethods, respectively, that will be available to any model that acts as commentable. Of course you can also add methods to the Comment class. For a complete listing of the code for the Acts As Commentable plugin see the project's site.

One final note, when editing a plugin I found it necessary to restart the server.

Technorati Tags: , , , , ,