Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Engine - Gems dependencies, how to load them into the application?

I'm doing an engine here, it works alright in stand alone.

When I transform it into a gem, and load it inside another application, I get a lot of undefined errors, coming from my engine gem's dependecies.

Here is the gemspec:

s.add_dependency('paperclip') s.add_dependency('jquery-rails') s.add_dependency('rails3-jquery-autocomplete') s.add_dependency('remotipart') s.add_dependency('cancan') 

In the application, when I do a bundle install, it lists all these dependencies, but as i run the application I receive a lot of undefined methods errors (has_attachment from paperclip for example). It seems that the application doesn't load the engines dependencies. Is this the default behavior? Can I change it? Same thing happened with a plugin inside the engine.

If I insert by hand those gems, in the application Gemfile, all works...

like image 519
Tiago Avatar asked Mar 01 '11 19:03

Tiago


2 Answers

Include them in your gemfile and run bundle install. Then require them in your lib/<your_engine>/engine.rb file. Don't forget to require rubygems

  require 'rubygems'   require 'paperclip'   require 'jquery-rails'   require 'rails3-jquery-autocomplete'   require 'remotipart'   require 'cancan' 

Then in your host app (The app where you included your gem) run bundle install/ bundle update (bundle update did the trick for me) and then everything should work perfectly. You can also test this by starting the console in your host app and just type the module name e.g.

Loading development environment (Rails 3.0.3) irb(main):001:0> Paperclip => Paperclip 

Hope this helps

like image 142
Daniël Zwijnenburg Avatar answered Oct 14 '22 16:10

Daniël Zwijnenburg


You can require them manually like Daniel posted, and you can also require them automatically. You need to add dependencies in 3 files:

  • yourengine.gemspec

    s.add_dependency "rails", '4.1.0' s.add_dependency "sqlite3" 
  • Gemfile

    # Imports dependencies from yourengine.gemspec gemspec 
  • lib/yourengine.rb

    # requires all dependencies Gem.loaded_specs['yourengine'].dependencies.each do |d|  require d.name end  require 'yourengine/engine'  module Yourengine end 

Update: It's a simplistic demonstration of how to require the dependencies. You should test it and filter unwanted items, for example: require d.name unless d.type == :development (thx @imsinu9)

like image 22
carlosvini Avatar answered Oct 14 '22 15:10

carlosvini