Rails 3.1 mountable engine gem dependencies

1.7k views Asked by At

I'm woking on an api for my app. The api is a mountable engine.

My problem is that if gems are not specified in parent apps gemfile, they are not loaded in api engine.

My current set-up:

parentapp/Gemfile

gem 'api', :path => 'path/to/api'

parentapp/routes.rb

mount Api::Engine => '/api'

api.gemspec

$:.push File.expand_path("../lib", __FILE__)

require "api/version"

Gem::Specification.new do |s|
  s.name        = "api"
  s.version     = Api::VERSION
  s.authors     = ["Author"]
  s.email       = ["email"]
  s.homepage    = "http://example.com"
  s.summary     = "Summary"
  s.description = "Description"

  s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
  s.test_files = Dir["test/**/*"]

  # Is not loading.
  s.add_dependency 'rails', '~> 3.1.3'
  s.add_dependency 'rabl'
  s.add_dependency 'yajl-ruby'
end

api/test/dummy/config/routes.rb

Rails.application.routes.draw do

  mount Api::Engine => "/api"
end

Weird thing is that it is getting "loaded", because if I set

s.add_dependency 'rails', '~> 3.2'

It raises an error.

Stacktrace if i remove gem 'rabl' from parent apps gemfile.

NameError: uninitialized constant Rabl
~/Sites/api/config/initializers/rabl_init.rb:1:in `<top (required)>'
~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/engine.rb:556:in `block (2 levels) in <class:Engine>'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/engine.rb:555:in `each'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/engine.rb:555:in `block in <class:Engine>'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:30:in `instance_exec'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:30:in `run'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:55:in `block in run_initializers'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:54:in `each'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:54:in `run_initializers'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/application.rb:96:in `initialize!'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
    ~/Sites/parentapp/config/environment.rb:5:in `<top (required)>'
    <internal:lib/rubygems/custom_require>:29:in `require'
    <internal:lib/rubygems/custom_require>:29:in `require'
    ~/Sites/parentapp/config.ru:3:in `block in <main>'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/builder.rb:4:in `instance_eval'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/builder.rb:4:in `initialize'
    ~/Sites/parentapp/config.ru:1:in `new'
    ~/Sites/parentapp/config.ru:1:in `<main>'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/server.rb:50:in `eval'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/server.rb:50:in `load_config'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/server.rb:43:in `initialize'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/server.rb:13:in `new'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/server.rb:13:in `run'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/bin/nack_worker:4:in `<main>'

This is my rabl_init.rb file located in config/initializers (in engine)

Rabl.configure do |config|
 config.include_json_root = false
end

If i remove this file i get Template is missing

Missing template api/v1/myresource/show, api/application/show with {:handlers=>[:erb, :builder, :coffee, :haml], :formats=>[:json]

The rabl handler is not included, ergo, rabl is not loaded.

1

There are 1 answers

2
Tim Brunsmo On BEST ANSWER

I was not requiring the gems as I should. Adding require "rabl" in lib/api/engine.rb solved it.