Kingsley Simon
Kingsley Simon

Reputation: 2210

rails engine gemfile.lock not being included rails engine

I am trying to build a rails engine and I have some gems that I want to isolate and bundle only in the rails engine Gemfile. I put these gems in my Gemfile:

source 'https://rubygems.org'
gemspec
gem "pg", "0.15"

In my insurance.gemspec:

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

# Maintain your gem's version:
require "insurance/version"

# Describe your gem and declare its dependencies:
Gem::Specification.new do |spec|
  spec.name        = "insurance"
  spec.version     = Insurance::VERSION
  spec.authors     = ["Engine Sample"]
  spec.email       = ["[email protected]"]
  spec.homepage    = "https://github.com/engine/engine_plugin"
  spec.summary     = "Engine Sample."
  spec.description = "Engine Sample."
  spec.license     = "MIT"

  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
  # to allow pushing to a single host or delete this section to allow pushing to any host.
  if spec.respond_to?(:metadata)
    spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
  else
    raise "RubyGems 2.0 or newer is required to protect against " \
      "public gem pushes."
  end

  spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]

  spec.add_dependency "rails", "~> 5.0.1"
  spec.add_development_dependency "route_translator"
end

Then I go to my engine on terminal and run bundle install which generates a Gemfile.lock in my engine folder.

I then go to main application and add in main application Gemfile this:

gem 'insurance', path: '/home/Documents/Projects/insurance'

I also created some routes in my engine folder config/routes.rb

but when I run my application, I get this error

Gem::LoadError - pg is not part of the bundle. Add it to your Gemfile.:

Upvotes: 1

Views: 843

Answers (1)

Vasfed
Vasfed

Reputation: 18464

Rails engines are actually gems, they are supposed to rely on parent app's Gemfile.lock and only specify dependencies in their gemspec.

If you want some gem to be a dependency of the engine - then add it to gemspec, and if you want to lock exact version - do so in dependency declaration (but in fact, it's better to specify a more loose version):

spec.add_dependency "pg", "0.15" # better use something like "~>0.15" here

Upvotes: 1

Related Questions