ranked-model icon indicating copy to clipboard operation
ranked-model copied to clipboard

Gem doesn't load in Rails 4 engine.gemspec

Open bbugh opened this issue 10 years ago • 4 comments

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!

bbugh avatar Mar 27 '14 00:03 bbugh

@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.

mixonic avatar Mar 30 '14 22:03 mixonic

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.

bbugh avatar Mar 31 '14 22:03 bbugh

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)

svilenkov avatar Oct 29 '14 10:10 svilenkov

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.

mat2maa avatar May 04 '15 15:05 mat2maa

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

brendon avatar Jun 04 '24 05:06 brendon