JZ.
JZ.

Reputation: 21877

Can you walk me through how constants are initialized in a Gem loaded in Rails?

I am wondering how the initialization process works with Gems. Basically, I ran into an issue today when I tried to call a constant, that lives in my Rails application from a Gem that I have included in my rails project.

The constant is Air::Fly

The constant loads fine from Rails, but the Gem can not load this constant. My goal was to extend the Rails Application class from the context of the Gem, in order to make my code more modular.

How can I load a rails application class from a Gem?

Upvotes: 0

Views: 478

Answers (1)

d11wtq
d11wtq

Reputation: 35308

Bundler resolves the dependencies of gems by checking the gemspec file for runtime dependencies. If a gem X's gemspec has add_runtime_dependency "Y", then gem Y will be loaded before gem X.

So, assuming Air is a gem, in your case, you need to add to your gemspec:

s.add_runtime_dependency "air"

This will ensure it's available for use inside your own gem ;)

Upvotes: 2

Related Questions