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

后端 未结 7 1056
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:04

    At the time being (Rails 3.1 and above I think), you shouldn't have do declare any gems in the test/dummy/Gemfile anymore:

    Quote from test/dummy/Gemfile (generated using rails plugin new my_engine --full):

    Declare your gem's dependencies in simple_view_helpers.gemspec. Bundler will treat runtime dependencies like base dependencies, and development dependencies will be added by default to the :development group.

    Declare any dependencies that are still in development here instead of in your gemspec. These might include edge Rails or gems from your path or Git. Remember to move these dependencies to your gemspec before releasing your gem to rubygems.org.

    0 讨论(0)
  • 2020-11-30 02:07

    You must add the gem file to both the .gemspec file, and your engine.rb file. In the .gemspec file it would be like: s.add_dependency "kaminari", "0.16.1"

    In the engine.rb file at the top add: require "kaminari"

    I think you also need to add the gem to the rails engine Gemfile and bundle install, but I'm not certain if you need it there.

    0 讨论(0)
  • 2020-11-30 02:07

    You can include all gems for the environment with a simple bundler command:

    Bundler.require(*Rails.groups)
    

    You could add this to an config/initializer.

    0 讨论(0)
  • 2020-11-30 02:12

    You really shouldn't need them on the Gemsec, and they should be loaded. When you say "here is the gemspec", you are surrounding it with Gem::Specification.new do |s| or something to that effect, right?

    0 讨论(0)
  • 2020-11-30 02:22

    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

    0 讨论(0)
  • 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)

    0 讨论(0)
提交回复
热议问题