ranked-model
ranked-model copied to clipboard
Gem doesn't load in Rails 4 engine.gemspec
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 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.
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 thetest_plugin.gemspec
-
bundle
Make a ranked-model model
-
rails g model Duck row_order:integer
- edit the model and add
include RankedModel
andranks :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
.
- It will report the error
- Now take out the
add_dependency
line fromtest_plugin.gemspec
and putgem 'ranked-model'
in theGemfile
and try again.- It will work correctly.
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)
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
.
Open to fixing this if it's still an issue. Let me know and I'll reopen.