Tuesday, December 23, 2008

How To Freeze Gems In Rails

Here's how to freeze any gem into your Rails app. This is a simplified version of the larger discussion Vendor Everything at err.the_blog.

  1. Copy the gems you want into the directory vendor/gems.
    $ mkdir vendor/gems
    $ cd vendor/gems
    $ gem unpack some_gem_name # repeat for each gem
    
  2. Edit config/environment.rb and add the following inside the Rails::Initializer.run block:
    Rails::Initializer.run do |config|
      # add the next three lines
      config.load_paths += Dir["#{RAILS_ROOT}/vendor/gems/**"].map do |dir|
        File.directory?(lib = "#{dir}/lib") ? lib : dir
     end
    end
    

Now you can require those gems in your code, even if the system you are running on doesn't have that gem installed.

1 comment:

Fawad said...

Thanks mate! This is really useful