Setting up bundler with Rails 2.3

Requirements
  • Ruby 1.8.6
  • Rubygems >= 1.3.5
  • Rails >= 2.3.4
  • Bundler 0.7.0
  • A Rails application.
Using git?

If you're using git we advise you to put the following lines in your .gitignore

vendor/bundler_gems/doc
vendor/bundler_gems/environment.rb
vendor/bundler_gems/gems
vendor/bundler_gems/specifications

Setup bundler to provide all necessary gems

$ vi Gemfile

Add the following lines:

bundle_path "vendor/bundler_gems"

gem "rails"

disable_system_gems
including extra Gemfiles?

To make sure bundler handles all the required gems including the ones from plugins use the following code:

my_gemfile = File.join(File.dirname(__FILE__), 'vendor', 'plugins', 'my_plugin', 'Gemfile')
instance_eval(File.read(my_gemfile), my_gemfile)

Create initializer for Bundler

$ vi config/preinitializer.rb

Add the following line:

require "#{File.dirname(__FILE__)}/../vendor/bundler_gems/environment"

Initialize Bundler before Rails boots

Add the following lines to config/boot.rb (before the Rails.boot!)

# for bundler
class Rails::Boot
  def run
    load_initializer
    extend_environment
    Rails::Initializer.run(:set_load_path)
  end
  def extend_environment
    Rails::Initializer.class_eval do
      old_load = instance_method(:load_environment)
      define_method(:load_environment) do
        Bundler.require_env RAILS_ENV
        old_load.bind(self).call
      end
    end
  end
end

Create your bundle of joy

$ gem bundle