[ACCEPTED]-Rails Engine - Gems dependencies, how to load them into the application?-rails-engines

Accepted answer
Score: 78

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

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

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

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

Hope 1 this helps

Score: 25

You can require them manually like Daniel 7 posted, and you can also require them automatically. You 6 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 5 a simplistic demonstration of how to require 4 the dependencies. You should test it and 3 filter unwanted items, for example: require 2 d.name unless d.type == :development (thx 1 @imsinu9)

Score: 2

from paperclip's README :

For Non-Rails usage:

class ModuleName < ActiveRecord::Base
  include Paperclip::Glue
  ...
end

I 2 had the same issue and that fixed it for 1 me.

Score: 2

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

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

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

Score: 0

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

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

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

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

Score: 0

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

Score: 0

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

Bundler.require(*Rails.groups)

You could 1 add this to an config/initializer.

More Related questions