Gem dependency issue

22 views
Skip to first unread message

Colin Law

unread,
Sep 28, 2015, 5:01:22 PM9/28/15
to Ruby on Rails: Talk
I am developing a rails gem. It uses the gon gem so in the gemspec
file I have included

spec.add_dependency 'gon'

Then in the Gemfile for the application using my gem I have put

gem 'my_gem', :path => '/path/to/dir/containing/my_gem'

When I run bundle install I see that it includes my gem and also the
gon gem as expected.
From a view helper in the app I am calling helper method in a module
in my gem's lib folder, and in there I have code such as
gon.variable = ...
and that code throws an error
ActionView::Template::Error (undefined method `gon' for
#<PlotValuesController:0x000000067bbc30>)
where PlotValuesController is the controller in the app. If I
explicitly include gon in the Gemfile for the app then all works as
correctly.

Any suggestions as to why I have to explicitly include gon in the
application's Gemfile, when it is included anyway via the dependency
in my gem?

Gemfile.lock with and without gon included explicitly can be seen at
http://pastebin.com/vzvNgg3K

Colin

Marco Antonio Almeida

unread,
Sep 28, 2015, 9:59:16 PM9/28/15
to rubyonra...@googlegroups.com
Hi Colin,

On Mon, Sep 28, 2015 at 11:00 PM Colin Law <cla...@gmail.com> wrote:
I am developing a rails gem.  It uses the gon gem so in the gemspec
file I have included

spec.add_dependency 'gon'

Then in the Gemfile for the application using my gem I have put

gem 'my_gem', :path => '/path/to/dir/containing/my_gem'

When I run bundle install I see that it includes my gem and also the
gon gem as expected.
From a view helper in the app I am calling helper method in a module
in my gem's lib folder, and in there I have code such as
gon.variable = ...
and that code throws an error
ActionView::Template::Error (undefined method `gon' for
#<PlotValuesController:0x000000067bbc30>)
where PlotValuesController is the controller in the app.  If I
explicitly include gon in the Gemfile for the app then all works as
correctly.

Any suggestions as to why I have to explicitly include gon in the
application's Gemfile, when it is included anyway via the dependency
in my gem?

If I recall correctly, your gem is a Rails engine. It's a bit late and I might be wrong, but a gem doesn't auto require other gems.

So to fix it, you need to require 'gon' in your gem (probably in your lib/your_gem_name.rb file).

A good example is Devise: https://github.com/plataformatec/devise/blob/master/lib/devise.rb. They do also a lot of stuff in between, but you can see the that ir requires the ORM, and Warden and other dependencies.

Hope it works,
// Marco


Gemfile.lock with and without gon included explicitly can be seen at
http://pastebin.com/vzvNgg3K

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuh7_ni44oG%3DTu5yVpAd4B16tzFg%2BEY6ewXcKJ2XPWQXQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Eito Katagiri

unread,
Sep 29, 2015, 1:58:33 AM9/29/15
to rubyonra...@googlegroups.com
Hi,

Before using `gon.variable`, do you call `include_gon` somewhere in your view?

HTH,

:eito

Frederick Cheung

unread,
Sep 29, 2015, 2:02:53 AM9/29/15
to Ruby on Rails: Talk
On Monday, September 28, 2015 at 10:01:22 PM UTC+1, Colin Law wrote:
>
> Any suggestions as to why I have to explicitly include gon in the
> application's Gemfile, when it is included anyway via the dependency
> in my gem?
>

Bundler's autorequire feature only requires gems listed in the Gemfile, not their dependencies. You could either require gon from within your gem, when it is required (this may or may not be appropriate ) or require it from your app.

Fred

Colin Law

unread,
Sep 29, 2015, 4:13:26 AM9/29/15
to Ruby on Rails: Talk
On 29 September 2015 at 07:02, Frederick Cheung
<frederic...@gmail.com> wrote:
> On Monday, September 28, 2015 at 10:01:22 PM UTC+1, Colin Law wrote:
>>
>> Any suggestions as to why I have to explicitly include gon in the
>> application's Gemfile, when it is included anyway via the dependency
>> in my gem?
>>
>
> Bundler's autorequire feature only requires gems listed in the Gemfile, not their dependencies. You could either require gon from within your gem, when it is required (this may or may not be appropriate ) or require it from your app.

Thanks all, that was the point I had missed, that bundler ensures all
the gems are available but only those explicitly included in Gemfile
are automatically loaded. I required gon in the gem and now all is
well.

Thanks

Colin
Reply all
Reply to author
Forward
0 new messages