Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does config.gem (in environment.rb) do?

I've been told that doing:

config.gem 'tzinfo'

doesn't obviate the need to require 'tzinfo'. Is this true of all gems? If yes, what exactly does adding config.gem WHATEVER do?

like image 252
Tom Lehman Avatar asked Aug 07 '09 19:08

Tom Lehman


2 Answers

config.gem should cause the gem to be automatically required. You should not need to do a manual 'require' call.

config.gem

  1. Tells Rails to load this gem automatically
  2. Tells Rails that this gem is needed for the application, so that rake gems:install will install it
  3. The :source option can tell rails to get it from a nonstandard repository
  4. The :lib option can tell rails to load a non-standard file from the gem (i.e. something not named after the gem itself)
like image 115
edebill Avatar answered Oct 21 '22 10:10

edebill


If i'm correct, during the environment initialization 'config.gem' allows your app to setup and require GEM dependencies from within the app, without the need to have to install them manually. (As we did before) By calling "config.gem tzinfo" as you did above, it automagically requires the gem across the app. This helps when you deploy to an external server and need to prepare the app along with necessary gems, etc. You can then run RAKE GEMS:INSTALL and rails will pull in all your gems and require them.

A thing to note though is that if you DO NOT want a gem to be required across your app. Then add ":lib => false" after config.gem i.e (config.gem 'tzinfo' :lib => false).

In some cases, (I followed your link) if you're getting an uninitialized gem, and you've manually installed it. Make sure that the config.gem ":lib" directory matches with the correct :lib directory of the gem. I.E a gem may be packaged and installed as "nlewis-supergem", however I may need to point the lib at "supergem". i.e "config.gem "nlewis-supergem" :lib=>"supergem". It all depends on how some people package their gem and the corresponding libraries.

A quick tip is instead of installing manually always install the gem via "config.gem" and then rake GEMS:INSTALL to catch any wierd errors before deployment.

Hope this helps.

like image 43
Nick L Avatar answered Oct 21 '22 10:10

Nick L