The Basics of Creating Rails Plugins

A Rails plugin is either an extension or a modification of the core framework. Plugins provide:

  • a way for developers to share bleeding-edge ideas without hurting the stable code base

  • a segmented architecture so that units of code can be fixed or updated on their own release schedule

  • an outlet for the core developers so that they don’t have to include every cool new feature under the sun

After reading this guide you should be familiar with:

  • Creating a plugin from scratch

  • Writing and running tests for the plugin

  • Storing models, views, controllers, helpers and even other plugins in your plugins

  • Writing generators

  • Writing custom Rake tasks in your plugin

  • Generating RDoc documentation for your plugin

  • Avoiding common pitfalls with init.rb

This guide describes how to build a test-driven plugin that will:

  • Extend core ruby classes like Hash and String

  • Add methods to ActiveRecord::Base in the tradition of the acts_as plugins

  • Add a view helper that can be used in erb templates

  • Add a new generator that will generate a migration

  • Add a custom generator command

  • A custom route method that can be used in routes.rb

For the purpose of this guide pretend for a moment that you are an avid bird watcher. Your favorite bird is the Yaffle, and you want to create a plugin that allows other developers to share in the Yaffle goodness. First, you need to get setup for development.

1. Preparation

1.1. Create the basic app

The examples in this guide require that you have a working rails application. To create a simple rails app execute:

gem install rails
rails yaffle_guide
cd yaffle_guide
script/generate scaffold bird name:string
rake db:migrate
script/server

Then navigate to http://localhost:3000/birds. Make sure you have a functioning rails app before continuing.

Note
Editor's note:
The aforementioned instructions will work for sqlite3. For more detailed instructions on how to create a rails app for other databases see the API docs.

1.2. Generate the plugin skeleton

Rails ships with a plugin generator which creates a basic plugin skeleton. Pass the plugin name, either CamelCased or under_scored, as an argument. Pass --with-generator to add an example generator also.

This creates a plugin in vendor/plugins including an init.rb and README as well as standard lib, task, and test directories.

Examples:

./script/generate plugin yaffle
./script/generate plugin yaffle --with-generator

To get more detailed help on the plugin generator, type ./script/generate plugin.

Later on this guide will describe how to work with generators, so go ahead and generate your plugin with the --with-generator option now:

./script/generate plugin yaffle --with-generator

You should see the following output:

create  vendor/plugins/yaffle/lib
create  vendor/plugins/yaffle/tasks
create  vendor/plugins/yaffle/test
create  vendor/plugins/yaffle/README
create  vendor/plugins/yaffle/MIT-LICENSE
create  vendor/plugins/yaffle/Rakefile
create  vendor/plugins/yaffle/init.rb
create  vendor/plugins/yaffle/install.rb
create  vendor/plugins/yaffle/uninstall.rb
create  vendor/plugins/yaffle/lib/yaffle.rb
create  vendor/plugins/yaffle/tasks/yaffle_tasks.rake
create  vendor/plugins/yaffle/test/core_ext_test.rb
create  vendor/plugins/yaffle/generators
create  vendor/plugins/yaffle/generators/yaffle
create  vendor/plugins/yaffle/generators/yaffle/templates
create  vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb
create  vendor/plugins/yaffle/generators/yaffle/USAGE

1.3. Setup the plugin for testing

In this guide you will learn how to test your plugin against multiple different adapters using Active Record. This guide will not cover how to use fixtures in plugin tests.

To setup your plugin to allow for easy testing you'll need to add 3 files:

  • A database.yml file with all of your connection strings.

  • A schema.rb file with your table definitions.

  • A test helper that sets up the database before your tests.

vendor/plugins/yaffle/test/database.yml:

sqlite:
  :adapter: sqlite
  :dbfile: yaffle_plugin.sqlite.db

sqlite3:
  :adapter: sqlite3
  :dbfile: yaffle_plugin.sqlite3.db

postgresql:
  :adapter: postgresql
  :username: postgres
  :password: postgres
  :database: yaffle_plugin_test
  :min_messages: ERROR

mysql:
  :adapter: mysql
  :host: localhost
  :username: rails
  :password:
  :database: yaffle_plugin_test

For this guide you'll need 2 tables/models, Hickwalls and Wickwalls, so add the following:

vendor/plugins/yaffle/test/schema.rb:

ActiveRecord::Schema.define(:version => 0) do
  create_table :hickwalls, :force => true do |t|
    t.string :name
    t.string :last_squawk
    t.datetime :last_squawked_at
  end
  create_table :wickwalls, :force => true do |t|
    t.string :name
    t.string :last_tweet
    t.datetime :last_tweeted_at
  end
end

vendor/plugins/yaffle/test/test_helper.rb:

ENV['RAILS_ENV'] = 'test'
ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'

require 'test/unit'
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))

config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")

db_adapter = ENV['DB']

db_adapter ||=
  begin
    require 'rubygems'
    require 'sqlite'
    'sqlite'
  rescue MissingSourceFile
    begin
      require 'sqlite3'
      'sqlite3'
    rescue MissingSourceFile
    end
  end

if db_adapter.nil?
  raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
end

ActiveRecord::Base.establish_connection(config[db_adapter])

load(File.dirname(__FILE__) + "/schema.rb")

require File.dirname(__FILE__) + '/../init.rb'

class Hickwall < ActiveRecord::Base
end

class Wickwall < ActiveRecord::Base
end

1.4. Run the plugin tests

Once you have these files in place, you can write your first test to ensure that your plugin-testing setup is correct. By default rails generates a file in vendor/plugins/yaffle/test/yaffle_test.rb with a sample test. Replace the contents of that file with:

vendor/plugins/yaffle/test/yaffle_test.rb:

require File.dirname(__FILE__) + '/test_helper.rb'

class YaffleTest < Test::Unit::TestCase

  def test_active_record_classes_from_test_helper
    assert_kind_of Hickwall, Hickwall.new
    assert_kind_of Wickwall, Wickwall.new
  end

end

To run this, go to the plugin directory and run rake:

cd vendor/plugins/yaffle
rake

You should see output like:

/opt/local/bin/ruby -Ilib:lib "/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/yaffle_test.rb"
-- create_table(:hickwalls, {:force=>true})
   -> 0.0220s
-- create_table(:wickwalls, {:force=>true})
   -> 0.0077s
-- initialize_schema_migrations_table()
   -> 0.0007s
-- assume_migrated_upto_version(0)
   -> 0.0007s
Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
Started
.
Finished in 0.002236 seconds.

1 test, 1 assertion, 0 failures, 0 errors

By default the setup above runs your tests with sqlite or sqlite3. To run tests with one of the other connection strings specified in database.yml, pass the DB environment variable to rake:

rake DB=sqlite
rake DB=sqlite3
rake DB=mysql
rake DB=postgresql

Now you are ready to test-drive your plugin!

2. Extending core classes

This section will explain how to add a method to String that will be available anywhere in your rails app by:

  • Writing tests for the desired behavior

  • Creating and requiring the correct files

2.1. Working with init.rb

When rails loads plugins it looks for the file named init.rb. However, the plugin initializer script init.rb is invoked via eval (not require) so it has slightly different behavior.

Under certain circumstances if you reopen classes or modules in init.rb itself, you may inadvertently create a new class, rather than reopening an existing class. A better alternative is to reopen the class in a different file, and require that file from init.rb.

If you must reopen a class in init.rb you can use module_eval or class_eval:

vendor/plugins/yaffle/init.rb

Hash.class_eval do
  def is_a_special_hash?
    true
  end
end

Another way is to explicitly define the top-level module space for all modules and classes, like ::Hash:

vendor/plugins/yaffle/init.rb

class ::Hash
  def is_a_special_hash?
    true
  end
end

2.2. Creating the test

In this example you will add a method to String named to_squawk. To begin, create a new test file with a few assertions:

vendor/plugins/yaffle/test/core_ext_test.rb

require File.dirname(__FILE__) + '/test_helper.rb'

class CoreExtTest < Test::Unit::TestCase
  def test_to_squawk_prepends_the_word_squawk
    assert_equal "squawk! Hello World", "Hello World".to_squawk
  end
end

Navigate to your plugin directory and run rake test:

cd vendor/plugins/yaffle
rake test

The test above should fail with the message:

 1) Error:
test_to_squawk_prepends_the_word_squawk(CoreExtTest):
NoMethodError: undefined method `to_squawk' for "Hello World":String
    ./test/core_ext_test.rb:5:in `test_to_squawk_prepends_the_word_squawk'

Great - now you are ready to start development.

2.3. Organize your files

A common pattern in rails plugins is to set up the file structure something like this:

|-- init.rb
|-- lib
|   |-- yaffle
|   |   `-- core_ext.rb
|   `-- yaffle.rb

The first thing we need to to is to require our lib/yaffle.rb file from init.rb:

vendor/plugins/yaffle/init.rb

require 'yaffle'

Then in lib/yaffle.rb require lib/core_ext.rb:

vendor/plugins/yaffle/lib/yaffle.rb

require "yaffle/core_ext"

Finally, create the core_ext.rb file and add the to_squawk method:

vendor/plugins/yaffle/lib/yaffle/core_ext.rb

String.class_eval do
  def to_squawk
    "squawk! #{self}".strip
  end
end

To test that your method does what it says it does, run the unit tests with rake from your plugin directory. To see this in action, fire up a console and start squawking:

$ ./script/console
>> "Hello World".to_squawk
=> "squawk! Hello World"

3. Add an acts_as_yaffle method to ActiveRecord

A common pattern in plugins is to add a method called acts_as_something to models. In this case, you want to write a method called acts_as_yaffle that adds a squawk method to your models.

To keep things clean, create a new test file called acts_as_yaffle_test.rb in your plugin's test directory and require your test helper.

vendor/plugins/yaffle/test/acts_as_yaffle_test.rb

require File.dirname(__FILE__) + '/test_helper.rb'

class Hickwall < ActiveRecord::Base
  acts_as_yaffle
end

class ActsAsYaffleTest < Test::Unit::TestCase
end

vendor/plugins/lib/acts_as_yaffle.rb

module Yaffle
end

One of the most common plugin patterns for acts_as_yaffle plugins is to structure your file like so:

vendor/plugins/lib/acts_as_yaffle.rb

module Yaffle
  def self.included(base)
    base.send :extend, ClassMethods
  end

  module ClassMethods
    # any method placed here will apply to classes, like Hickwall
    def acts_as_something
      send :include, InstanceMethods
    end
  end

  module InstanceMethods
    # any method placed here will apply to instaces, like @hickwall
  end
end

With structure you can easily separate the methods that will be used for the class (like Hickwall.some_method) and the instance (like @hickwell.some_method).

Let's add class method named acts_as_yaffle - testing it out first. You already defined the ActiveRecord models in your test helper, so if you run tests now they will fail.

Back in your acts_as_yaffle file, update ClassMethods like so:

module ClassMethods
  def acts_as_yaffle(options = {})
    send :include, InstanceMethods
  end
end

Now that test should pass. Since your plugin is going to work with field names, you need to allow people to define the field names, in case there is a naming conflict. You can write a few simple tests for this:

vendor/plugins/yaffle/test/acts_as_yaffle_test.rb

require File.dirname(__FILE__) + '/test_helper.rb'

class ActsAsYaffleTest < Test::Unit::TestCase
  def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
    assert_equal "last_squawk", Hickwall.yaffle_text_field
  end

  def test_a_hickwalls_yaffle_date_field_should_be_last_squawked_at
    assert_equal "last_squawked_at", Hickwall.yaffle_date_field
  end

  def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
    assert_equal "last_tweet", Wickwall.yaffle_text_field
  end

  def test_a_wickwalls_yaffle_date_field_should_be_last_tweeted_at
    assert_equal "last_tweeted_at", Wickwall.yaffle_date_field
  end
end

To make these tests pass, you could modify your acts_as_yaffle file like so:

vendor/plugins/yaffle/lib/acts_as_yaffle.rb

module Yaffle
  def self.included(base)
    base.send :extend, ClassMethods
  end

  module ClassMethods
    def acts_as_yaffle(options = {})
      cattr_accessor :yaffle_text_field, :yaffle_date_field
      self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s
      self.yaffle_date_field = (options[:yaffle_date_field] || :last_squawked_at).to_s
      send :include, InstanceMethods
    end
  end

  module InstanceMethods
  end
end

Now you can add tests for the instance methods, and the instance method itself:

vendor/plugins/yaffle/test/acts_as_yaffle_test.rb

require File.dirname(__FILE__) + '/test_helper.rb'

class ActsAsYaffleTest < Test::Unit::TestCase

  def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
    assert_equal "last_squawk", Hickwall.yaffle_text_field
  end
  def test_a_hickwalls_yaffle_date_field_should_be_last_squawked_at
    assert_equal "last_squawked_at", Hickwall.yaffle_date_field
  end

  def test_a_wickwalls_yaffle_text_field_should_be_last_squawk
    assert_equal "last_tweet", Wickwall.yaffle_text_field
  end
  def test_a_wickwalls_yaffle_date_field_should_be_last_squawked_at
    assert_equal "last_tweeted_at", Wickwall.yaffle_date_field
  end

  def test_hickwalls_squawk_should_populate_last_squawk
    hickwall = Hickwall.new
    hickwall.squawk("Hello World")
    assert_equal "squawk! Hello World", hickwall.last_squawk
  end
  def test_hickwalls_squawk_should_populate_last_squawked_at
    hickwall = Hickwall.new
    hickwall.squawk("Hello World")
    assert_equal Date.today, hickwall.last_squawked_at
  end

  def test_wickwalls_squawk_should_populate_last_tweet
    wickwall = Wickwall.new
    wickwall.squawk("Hello World")
    assert_equal "squawk! Hello World", wickwall.last_tweet
  end
  def test_wickwalls_squawk_should_populate_last_tweeted_at
    wickwall = Wickwall.new
    wickwall.squawk("Hello World")
    assert_equal Date.today, wickwall.last_tweeted_at
  end
end

vendor/plugins/yaffle/lib/acts_as_yaffle.rb

module Yaffle
  def self.included(base)
    base.send :extend, ClassMethods
  end

  module ClassMethods
    def acts_as_yaffle(options = {})
      cattr_accessor :yaffle_text_field, :yaffle_date_field
      self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s
      self.yaffle_date_field = (options[:yaffle_date_field] || :last_squawked_at).to_s
      send :include, InstanceMethods
    end
  end

  module InstanceMethods
    def squawk(string)
      write_attribute(self.class.yaffle_text_field, string.to_squawk)
      write_attribute(self.class.yaffle_date_field, Date.today)
    end
  end
end
Note
Editor's note:
The use of write_attribute to write to the field in model is just one example of how a plugin can interact with the model, and will not always be the right method to use. For example, you could also use send("#{self.class.yaffle_text_field}=", string.to_squawk).

4. Create a squawk_info_for view helper

Creating a view helper is a 3-step process:

  • Add an appropriately named file to the lib directory.

  • Require the file and hooks in init.rb.

  • Write the tests.

First, create the test to define the functionality you want:

vendor/plugins/yaffle/test/view_helpers_test.rb

require File.dirname(__FILE__) + '/test_helper.rb'
include YaffleViewHelper

class ViewHelpersTest < Test::Unit::TestCase
  def test_squawk_info_for_should_return_the_text_and_date
    time = Time.now
    hickwall = Hickwall.new
    hickwall.last_squawk = "Hello World"
    hickwall.last_squawked_at = time
    assert_equal "Hello World, #{time.to_s}", squawk_info_for(hickwall)
  end
end

Then add the following statements to init.rb:

vendor/plugins/yaffle/init.rb

require "view_helpers"
ActionView::Base.send :include, YaffleViewHelper

Then add the view helpers file and

vendor/plugins/yaffle/lib/view_helpers.rb

module YaffleViewHelper
  def squawk_info_for(yaffle)
    returning "" do |result|
      result << yaffle.read_attribute(yaffle.class.yaffle_text_field)
      result << ", "
      result << yaffle.read_attribute(yaffle.class.yaffle_date_field).to_s
    end
  end
end

You can also test this in script/console by using the helper method:

$ ./script/console
>> helper.squawk_info_for(@some_yaffle_instance)

5. Create a migration generator

When you created the plugin above, you specified the —with-generator option, so you already have the generator stubs in your plugin.

We'll be relying on the built-in rails generate template for this tutorial. Going into the details of generators is beyond the scope of this tutorial.

Type:

script/generate

You should see the line:

Plugins (vendor/plugins): yaffle

When you run script/generate yaffle you should see the contents of your USAGE file. For this plugin, the USAGE file looks like this:

Description:
    Creates a migration that adds yaffle squawk fields to the given model

Example:
    ./script/generate yaffle hickwall

    This will create:
        db/migrate/TIMESTAMP_add_yaffle_fields_to_hickwall

Now you can add code to your generator:

vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb

class YaffleGenerator < Rails::Generator::NamedBase
  def manifest
    record do |m|
      m.migration_template 'migration:migration.rb', "db/migrate", {:assigns => yaffle_local_assigns,
        :migration_file_name => "add_yaffle_fields_to_#{custom_file_name}"
       }
    end
  end

  private
    def custom_file_name
      custom_name = class_name.underscore.downcase
      custom_name = custom_name.pluralize if ActiveRecord::Base.pluralize_table_names
    end

    def yaffle_local_assigns
      returning(assigns = {}) do
        assigns[:migration_action] = "add"
        assigns[:class_name] = "add_yaffle_fields_to_#{custom_file_name}"
        assigns[:table_name] = custom_file_name
        assigns[:attributes] = [Rails::Generator::GeneratedAttribute.new("last_squawk", "string")]
        assigns[:attributes] << Rails::Generator::GeneratedAttribute.new("last_squawked_at", "datetime")
      end
    end
end

Note that you need to be aware of whether or not table names are pluralized.

This does a few things:

  • Reuses the built in rails migration_template method.

  • Reuses the built-in rails migration template.

When you run the generator like

script/generate yaffle bird

You will see a new file:

db/migrate/20080529225649_add_yaffle_fields_to_birds.rb

class AddYaffleFieldsToBirds < ActiveRecord::Migration
  def self.up
    add_column :birds, :last_squawk, :string
    add_column :birds, :last_squawked_at, :datetime
  end

  def self.down
    remove_column :birds, :last_squawked_at
    remove_column :birds, :last_squawk
  end
end

6. Add a custom generator command

You may have noticed above that you can used one of the built-in rails migration commands m.migration_template. You can create your own commands for these, using the following steps:

  1. Add the require and hook statements to init.rb.

  2. Create the commands - creating 3 sets, Create, Destroy, List.

  3. Add the method to your generator.

Working with the internals of generators is beyond the scope of this tutorial, but here is a basic example:

vendor/plugins/yaffle/init.rb

require "commands"
Rails::Generator::Commands::Create.send   :include,  Yaffle::Generator::Commands::Create
Rails::Generator::Commands::Destroy.send  :include,  Yaffle::Generator::Commands::Destroy
Rails::Generator::Commands::List.send     :include,  Yaffle::Generator::Commands::List

vendor/plugins/yaffle/lib/commands.rb

require 'rails_generator'
require 'rails_generator/commands'

module Yaffle #:nodoc:
  module Generator #:nodoc:
    module Commands #:nodoc:
      module Create
        def yaffle_definition
          file("definition.txt", "definition.txt")
        end
      end

      module Destroy
        def yaffle_definition
          file("definition.txt", "definition.txt")
        end
      end

      module List
        def yaffle_definition
          file("definition.txt", "definition.txt")
        end
      end
    end
  end
end

vendor/plugins/yaffle/generators/yaffle/templates/definition.txt

Yaffle is a bird

vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb

class YaffleGenerator < Rails::Generator::NamedBase
  def manifest
    m.yaffle_definition
  end
end

This example just uses the built-in "file" method, but you could do anything that Ruby allows.

7. Add a Custom Route

Testing routes in plugins can be complex, especially if the controllers are also in the plugin itself. Jamis Buck showed a great example of this in http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2.

vendor/plugins/yaffle/test/routing_test.rb

require "#{File.dirname(__FILE__)}/test_helper"

class RoutingTest < Test::Unit::TestCase

  def setup
    ActionController::Routing::Routes.draw do |map|
      map.yaffles
    end
  end

  def test_yaffles_route
    assert_recognition :get, "/yaffles", :controller => "yaffles_controller", :action => "index"
  end

  private

    # yes, I know about assert_recognizes, but it has proven problematic to
    # use in these tests, since it uses RouteSet#recognize (which actually
    # tries to instantiate the controller) and because it uses an awkward
    # parameter order.
    def assert_recognition(method, path, options)
      result = ActionController::Routing::Routes.recognize_path(path, :method => method)
      assert_equal options, result
    end
end

vendor/plugins/yaffle/init.rb

require "routing"
ActionController::Routing::RouteSet::Mapper.send :include, Yaffle::Routing::MapperExtensions

vendor/plugins/yaffle/lib/routing.rb

module Yaffle #:nodoc:
  module Routing #:nodoc:
    module MapperExtensions
      def yaffles
        @set.add_route("/yaffles", {:controller => "yaffles_controller", :action => "index"})
      end
    end
  end
end

config/routes.rb

ActionController::Routing::Routes.draw do |map|
  ...
  map.yaffles
end

You can also see if your routes work by running rake routes from your app directory.

8. Odds and ends

8.1. Generate RDoc Documentation

Once your plugin is stable, the tests pass on all database and you are ready to deploy do everyone else a favor and document it! Luckily, writing documentation for your plugin is easy.

The first step is to update the README file with detailed information about how to use your plugin. A few key things to include are:

  • Your name.

  • How to install.

  • How to add the functionality to the app (several examples of common use cases).

  • Warning, gotchas or tips that might help save users time.

Once your README is solid, go through and add rdoc comments to all of the methods that developers will use.

Before you generate your documentation, be sure to go through and add nodoc comments to those modules and methods that are not important to your users.

Once your comments are good to go, navigate to your plugin directory and run:

rake rdoc

8.2. Store models, views, helpers, and controllers in your plugins

You can easily store models, views, helpers and controllers in plugins. Just create a folder for each in the lib folder, add them to the load path and remove them from the load once path:

# File: vendor/plugins/yaffle/init.rb

%w{ models controllers helpers }.each do |dir|
  path = File.join(directory, 'lib', dir)
  $LOAD_PATH << path
  Dependencies.load_paths << path
  Dependencies.load_once_paths.delete(path)
end

Adding directories to the load path makes them appear just like files in the the main app directory - except that they are only loaded once, so you have to restart the web server to see the changes in the browser.

Adding directories to the load once paths allow those changes to picked up as soon as you save the file - without having to restart the web server.

8.3. Write custom Rake tasks in your plugin

When you created the plugin with the built-in rails generator, it generated a rake file for you in vendor/plugins/yaffle/tasks/yaffle.rake. Any rake task you add here will be available to the app.

Many plugin authors put all of their rake tasks into a common namespace that is the same as the plugin, like so:

vendor/plugins/yaffle/tasks/yaffle.rake

namespace :yaffle do
  desc "Prints out the word 'Yaffle'"
  task :squawk => :environment do
    puts "squawk!"
  end
end

When you run rake -T from your plugin you will see:

yaffle:squawk             # Prints out the word 'Yaffle'

You can add as many files as you want in the tasks directory, and if they end in .rake Rails will pick them up.

8.4. Store plugins in alternate locations

You can store plugins wherever you want - you just have to add those plugins to the plugins path in environment.rb.

Since the plugin is only loaded after the plugin paths are defined, you can't redefine this in your plugins - but it may be good to now.

You can even store plugins inside of other plugins for complete plugin madness!

config.plugin_paths << File.join(RAILS_ROOT,"vendor","plugins","yaffle","lib","plugins")

8.5. Create your own Plugin Loaders and Plugin Locators

If the built-in plugin behavior is inadequate, you can change almost every aspect of the location and loading process. You can write your own plugin locators and plugin loaders, but that's beyond the scope of this tutorial.

8.6. Use Custom Plugin Generators

If you are an RSpec fan, you can install the rspec_plugin_generator gem, which will generate the spec folder and database for you. See http://github.com/pat-maddox/rspec-plugin-generator/tree/master.

9. Appendix

9.1. References

9.2. Final plugin directory structure

The final plugin should have a directory structure that looks something like this:

  |-- MIT-LICENSE
  |-- README
  |-- Rakefile
  |-- generators
  |   `-- yaffle
  |       |-- USAGE
  |       |-- templates
  |       |   `-- definition.txt
  |       `-- yaffle_generator.rb
  |-- init.rb
  |-- install.rb
  |-- lib
  |   |-- acts_as_yaffle.rb
  |   |-- commands.rb
  |   |-- core_ext.rb
  |   |-- routing.rb
  |   `-- view_helpers.rb
  |-- tasks
  |   `-- yaffle_tasks.rake
  |-- test
  |   |-- acts_as_yaffle_test.rb
  |   |-- core_ext_test.rb
  |   |-- database.yml
  |   |-- debug.log
  |   |-- routing_test.rb
  |   |-- schema.rb
  |   |-- test_helper.rb
  |   `-- view_helpers_test.rb
  |-- uninstall.rb
  `-- yaffle_plugin.sqlite3.db