Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gem dependencies in Rails Engines

I have followed the Getting Started with Engines in Rails documentation and I have set up an api engine in engines directory. According to the paragraph 6.6 Other Gem Dependencies one is supposed to define the gem dependencies in engines/my_api/my_api.gemspec file and that is pretty much what I did:

s.add_dependency "responders", "2.0"

After adding

`gem 'my_api', path: "engines/my_api"`

to the applications Gemfile and running bundler, everything looks as expected:

 bundle install | grep responders
 Installing responders 2.0.0

In the next step I set up a root path with a corresponding controller etc. and go to engines/my_api/app/controllers/my_api/application_controller.rb and add following content:

module MyApi
  class ApplicationController < ActionController::Base
    respond_to :json
  end
end

I start the rails server, go the root url and guess what? I get following message:

The controller-level respond_to' feature has been extracted to the responders gem. Add it to your Gemfile to continue using this feature: gem 'responders', '~> 2.0' Consult the Rails upgrade guide for details.

As suggested in the error message, I've added the gem to the applications Gemfile, run bundle install and everything works as expected.

As far as I understood, engines are supposed to be self contained rails apps. From a self contained app I would at least expect to correctly resolve its dependencies. I assume that I am just doing something wrong and I hope someone will be able to help me to tackle down the problem, why do I have to explicitly specify the gem in the applications Gemfile?

EDIT:

Forgot to mention the versions:

$ gem list | grep rails
coffee-rails (4.1.0)
jquery-rails (4.0.3)
rails (4.2.1)
rails-deprecated_sanitizer (1.0.3)
rails-dom-testing (1.0.6)
rails-html-sanitizer (1.0.2)
sass-rails (5.0.3)
sprockets-rails (2.2.4)
like image 587
the_disco Avatar asked Sep 29 '22 04:09

the_disco


1 Answers

As you observe, the gem is included in your bundle whether it is in your application's Gemfile or not. The difference is that when Bundler.require is called during application initialisation it only auto requires the gem's in your application's Gemfile - not indirect dependencies.

If your gem requires the responders gem gem to be loaded then it should require it explicitly - for example at the top of my_api.rb

like image 112
Frederick Cheung Avatar answered Oct 03 '22 03:10

Frederick Cheung