Skip to content

Instantly share code, notes, and snippets.

@d--j
Forked from indirect/Gemfile
Created February 16, 2010 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save d--j/305262 to your computer and use it in GitHub Desktop.
Save d--j/305262 to your computer and use it in GitHub Desktop.
# add this to the bottom of config/boot.rb, before Rails.boot!
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 :default, Rails.env
old_load.bind(self).call
end
end
end
end
# and add this to the very end, after Rails.boot! to load all Gem Plugins
class Rails::Plugin::GemLocator
# find the original that we patch in rails/lib/rails/plugin/locator.rb:80
def plugins
specs = Bundler.load.specs_for(:default, Rails.env)
specs += Gem.loaded_specs.values.select do |spec|
spec.loaded_from && # prune stubs
# File.exist?(File.join(spec.full_gem_path, "rails", "init.rb"))
(File.exist?(File.join(spec.full_gem_path, "rails", "init.rb")) || File.exist?(File.join(spec.full_gem_path, "init.rb")))
end
specs.compact!
require "rubygems/dependency_list"
deps = Gem::DependencyList.new
deps.add(*specs) unless specs.empty?
deps.dependency_order.collect do |spec|
Rails::GemPlugin.new(spec, nil)
end
end
end
# include at least one source and the rails gem
source :gemcutter
gem 'rails', '~> 2.3.5', :require => nil
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
# this code goes in config/preinitializer.rb, which you should create if it doesn't exist
begin
# Require the preresolved locked set of gems.
require File.expand_path('../../.bundle/environment', __FILE__)
rescue LoadError
# Fallback on doing the resolve at runtime.
require "rubygems"
require "bundler"
Bundler.setup
end
@wallace
Copy link

wallace commented Jul 18, 2011

I get an error trying this with rails 2.3.12, bundler 1.0.10, and devise 1.0.11

undefined method specs_for' for #Bundler::Runtime:0x10105d2f8 (NoMethodError)`

Any suggestions?

@wallace
Copy link

wallace commented Jul 18, 2011

My issue was occurring because I was attempting to use devise in a custom rails engine in my project.

I updated the code above so that errors weren't thrown and but ultimately decided to remove the rails engine and move associated code back into the main project.

class Rails::Plugin::GemLocator
  # find the original that we patch in rails/lib/rails/plugin/locator.rb:80
  def plugins
    specs     = Bundler.definition.specs_for([]).to_a
    gem_specs = Gem.loaded_specs.values.select do |spec|
      spec.loaded_from && # prune stubs
        # File.exist?(File.join(spec.full_gem_path, "rails", "init.rb"))
        (File.exist?(File.join(spec.full_gem_path, "rails", "init.rb")) || File.exist?(File.join(spec.full_gem_path, "init.rb")))
    end
    specs += gem_specs

    require "rubygems/dependency_list"

    deps = Gem::DependencyList.new
    deps.add(*specs) unless specs.empty?

    deps.dependency_order.collect do |spec|
      Rails::GemPlugin.new(spec, nil)
    end
  end
end

@d--j
Copy link
Author

d--j commented Jul 18, 2011

Glad that you found the solution yourself. The code above is for a quite ancient bundler version (0.9.5 I belive) so I'm not too suprised that it does not work with the current version anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment