0

The name of a gem is not the same as the module. Right now I need to require it and include in various types of files, such as controllers and models. This is amounting to many requires which I don't have to do for other gems. It's a gem I can update. Is there a way the gem needs to be configured so it is "attached" to Rails, and if so, where may I find instructions to do this?

UPDATE: using required: "name-of-module" in Gemfile removes need for require everywhere. Still wondering, if gem could be configured to not require this in Gemfile?

foamroll
  • 752
  • 7
  • 23

2 Answers2

1

In the Gemfile, you can do these things:

# Require a different file than the gem's name
gem 'foo', require: 'bar'

# Install but not require anything.
# You need to manually require the gem somewhere.
gem 'foo', require: false

You can still add version and platform specification if you want.

Real-world examples are ActiveSupport and rspec:

gem 'activesupport', '~> 5.2', require: 'active_support/all'

gem 'rspec', '~> 3.1', group: :test, require: false
Aetherus
  • 8,720
  • 1
  • 22
  • 36
-1

You need to specify which files you want Ruby to load for you, so you either need specify the gem in your Gemfile (and run bundle exec ...) or put require in the right place(s) in your code. There's no way around that.

If it's a gem that you work on at the same time as you use it in another project, then you can specify a path to the gem. Like

# Gemfile
source "https://rubygems.org"
ruby '2.6.1'

gem 'my_gem', path: '/home/user/Development/my_gem'

This way, you can change your gem and use it directly without having to build and install it.

sammygadd
  • 299
  • 2
  • 6