Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gem doesn't load in Rails 4 engine.gemspec #72

Closed
bbugh opened this issue Mar 27, 2014 · 5 comments
Closed

Gem doesn't load in Rails 4 engine.gemspec #72

bbugh opened this issue Mar 27, 2014 · 5 comments

Comments

@bbugh
Copy link

bbugh commented Mar 27, 2014

Hi, since a Rails 4 --mountable engine is a .gemspec, and since the recommended place to put gems is in the .gemspec, this is more bug-ish than a bug. I've seen it in other libraries too.

In the engine_name.gemspec file, this loads but is inaccessible by Rails.
s.add_dependency 'ranked-model'

The only way to fix it is to put it into the Gemfile. Kind of a bummer when all of the other gems are in the gemspec!

@mixonic
Copy link
Contributor

mixonic commented Mar 30, 2014

@bbugh a little confused here- You are saying that the only way to use ranked-model in an engine is to add it as a dependency in the engine's Gemfile? I have not used engines much.

@bbugh
Copy link
Author

bbugh commented Mar 31, 2014

Correct. Engines have both a Gemfile and an engine_name.gemspec file. The officially recommended way to load gems is to use the .gemspec, as we would with any gem. We have a really big gem list and I haven't seen any of the others do this.

You can read more about why this is important from the Rails guide: http://guides.rubyonrails.org/engines.html#other-gem-dependencies

Problem
Without a reference in the gemspec, the gem will not be installed when added as a gem to another project, like gem 'test_plugin'. It will install correctly with Bundler either way. The only solution for this issue is to put it in both the Gemfile and the .gemspec file, which is not a big deal but is annoying to keep track of especially for specific versioning.

Reproduction
Make a mountable engine

  • rails plugin new test_plugin --mountable
  • Add s.add_dependency 'ranked-model' to the test_plugin.gemspec
  • bundle

Make a ranked-model model

  • rails g model Duck row_order:integer
  • edit the model and add include RankedModel and ranks :row_order
  • rake db:migrate app:db:test:prepare
  • Edit test/models/test_plugin/duck_test.rb and add a simple Duck test, test "rank" do; Duck.new.valid?; end

Test it

  • run rake
    • It will report the error NameError: uninitialized constant TestPlugin::Duck::RankedModel.
  • Now take out the add_dependency line from test_plugin.gemspec and put gem 'ranked-model' in the Gemfile and try again.
    • It will work correctly.

@svilenkov
Copy link

Adding it to both the engine's Gemfile and .gemspec didn't solve it. Still it gives the NameError: uninitialized constant. Using (ruby 2.0.0p576 & Rails 4.0.5)

@mat2maa
Copy link

mat2maa commented May 4, 2015

Just ran across the same issue. The following is inspired by this and using Ruby 1.9.3 and Rails 3.2.13 - not sure therefore if it is of any help to a Rails 4 issue.

I got it to work without error by first adding s.add_dependency 'ranked-model' to test_plugin.gemspec, following the example set out by @bbugh, and then adding:

require 'ranked-model'

to the top of lib/test_plugin/engine.rb.

@brendon
Copy link
Owner

brendon commented Jun 4, 2024

Open to fixing this if it's still an issue. Let me know and I'll reopen.

@brendon brendon closed this as completed Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants