Managing Gems in Rails can be infuriating at times especially when Bundler is not present. That is what happens when you use Rails version lower than 3 as it does not come bundled with Bundler.
A quick fresh up though as why we use Bundler -
Bundler maintains a consistent environment for ruby applications. It tracks an application's code and the rubygems it needs to run, so that an application will always have the exact gems (and versions) that it needs to run.
With Bundler, it can be easy to share your code between development, staging and production machines.
So here is a workaround by which you can use Bundler in Rails 2.3.x apps.
Disclaimer - Rails version lower than 2.3 have not been tested.
Navigate to the config folder of your Rails app.
config/directory contains files related to the application configuration.
In it there is a file named boot.rb
Add the following code at the bottom of config/boot.rb, above the line Rails.boot!
class Rails::Boot
def run
load_initializer
Rails::Initializer.class_eval do
def load_gems
@bundler_loaded ||= Bundler.require :default, Rails.env
end
end
Rails::Initializer.run(:set_load_path)
end
end
Note - Any changes in the configuration file would require you to restart Rails server for the changes to take effect.
Next create a new file named preinitializer.rb inside the config folder and add the following bit of code to it.
begin
require 'rubygems'
require 'bundler'
rescue LoadError
raise "Could not load the bundler gem. Install it with `gem install bundler`."
end
if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.24")
raise RuntimeError, "Your bundler version is too old for Rails 2.3.\n" +
"Run `gem install bundler` to upgrade."
end
begin
# Set up load paths for all bundled gems
ENV["BUNDLE_GEMFILE"] = File.expand_path("../../Gemfile", __FILE__)
Bundler.setup
rescue Bundler::GemNotFound
raise RuntimeError, "Bundler couldn't find some gems.\n" +
"Did you run `bundle install`?"
end
Create a new file inside your app folder by the name of Gemfile and remember, this file does not contain any extension so save the file type as All files.
Note - The app folder is your application folder i.e suppose the name of your app is example then you have to create the file inside example folder and not inside example/app folder.
Remove all your gem requirement list from config/environment.rb file and add it to your Gemfile.
Below is a sample Gemfile. Edit according to your requirements.
# include at least one source and the rails gem
source :gemcutter
gem 'rails', '~> 2.3.5', :require => nil
gem 'sqlite3-ruby', :require => 'sqlite3'
# Devise 1.0.2 is not a valid gem plugin for Rails, so use git until 1.0.3
# gem 'devise', :git => 'git://github.com/plataformatec/devise.git', :ref => 'v1.0'
group :development do
# bundler requires these gems in development
gem 'rails-footnotes'
end
group :test do
# bundler requires these gems while running tests
gem 'rspec'
end
If you have gems that should only be loaded in certain environments, like development-only or test-only , you can put those in the development and test groups.
Once this is done just fire the console and execute bundle install.
This command instructs bundler to go through the gems listed in Gemfile one by one, fetch them, install them locally & include them in the app.
In future if you want to include any gem just add it to your Gemfile and execute bundle install again.
HAPPY BUNDLING GUYS
0 Comment(s)