Plugins and Gems

Introduction

Where to Find Plugins

Creating Plugins

A Few Recommended Plugins

The Plugin Module Inclusion Pattern


module MyPlugin
  def self.included(base)
    base.extend ClassMethods
    base.send :include, InstanceMethods
  end
  module InstanceMethods
    ...
  end
  module ClassMethods
    ...
  end
end

class ActiveRecord::Base
  include MyPlugin
end

A Word of Caution

RubyGems

Declaring Gem Dependencies


# In config/environment.rb:
config.gem "aws-s3", :lib => 'aws/s3', :version => "0.5.1" 
config.gem "money", :version => "1.7.1" 
config.gem "fastercsv", :version => "1.4.0" 
config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net" 

Installing and Freezing Gems


# Install declared gem dependencies on the system (sudo gem install <gem-name>)
rake gems:install

# To avoid external libarary dependencies:
# 1. Copy declared gems to under RAILS_ROOT/vendor/gems
rake gems:unpack
# 2. Copy Rails itself to RAILS_ROOT/vendor/rails
rake rails:freeze:gems
# The Rails version is given by RAILS_GEM_VERSION in config/environment.rb.