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

后端 未结 7 1058
Happy的楠姐
Happy的楠姐 2020-11-30 01:23

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,

7条回答
  •  独厮守ぢ
    2020-11-30 02:28

    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)

提交回复
热议问题