Daniel Dyba

Self-directed learning happens here

Using 'Bundle Install' in a Rails 3 Generator

| Comments

Problem

I came across Rails issue #3153 on Github which involved someone thinking bundle install didn’t work with Rails generators.

When working with Rails generators it’s important to remember that generators use Yehuda Katz’s Thor gem. The issue arose because the user tried to run the command bundle install from inside the directory in which the generator file had been created.

1
2
3
4
5
6
7
8
class SomegenGenerator < Rails::Generators::Base
 source_root File.expand_path('../templates', __FILE__)

 def install
   gem "unicorn"
   run "bundle install"
 end
end

Solution

Again, the issue is that the command bundle install is being run from inside the directory Issue3153/lib/generators/something/. I’ll let you take a quick look at the Thor source code at lib/thor/actions.rb. As you have now seen, we can use the inside method to run the bundle install command from the root directory of our app Issue3153.

1
2
3
4
5
6
7
8
9
10
11
# Issue3153/lib/generators/something/something_generator.rb
class SomethingGenerator < Rails::Generators::Base
  source_root File.expand_path('../templates', __FILE__)

  def install
    gem "unicorn"
    inside Rails.root do
      run "bundle install"
    end
  end
end

This now works!

Take Aways

  • Rails generators use Thor Actions
  • The bundle install command must be run in the directory where the Gemfile of interest resides

Comments