Search code examples
ruby-on-railsrubygemsgemfilesqueel

Composing one Gemfile from multiple others


I'm splitting a large application into multiple smaller ones. When doing this I realized that by creating a Gemfile.global and including it in both my main and sub applications I could clean up all of my Gemfiles. For example, all of my deployment gems go in the main Gemfile, and my rails goes in the Gemfile.global.

It works for pretty much all of my gems except one: Squeel.

My Gemfile in the root app starts off with:

gemfiles = [
  File.join('Gemfile.global'),
]
Dir.glob(File.join(File.dirname(__FILE__), gemfiles)) do |gemfile|
  eval(IO.read(gemfile), binding)
end

# gem 'squeel' # Let's try putting this in Gemfile.global

My Gemfile.global looks like:

source 'https://rubygems.org'
# rails and dependencies
gem 'squeel'

bundle install works great, as well as rails s and pretty much everything, the way you'd expect, with Squeel in the main Gemfile. Putting it in Gemfile.global, however, screws up the initialization process when using the default Squeel initializer:

Squeel.configure do |config|
end

rails s throws a uninitialized constant Squeel (NameError) even though bundle install reports Using squeel (1.0.13). Why does this method of composing my Gemfile mess up the rails runtime constants?


Solution

  • You can also use eval_gemfile:

    gemfiles = [ 'Gemfile.global' ]
    gemfiles.each do |gemfile|
      eval_gemfile gemfile
    end