Where are required gems defined? - ruby-on-rails

In my rails application I once used authlogic-oid and ruby-openid. Now I want to get rid of them and I removed both gems and also their config.gem lines from my environment.rb.
Although my application works, I can't do any database migrations because I get a "Missing these required gems" error. Also if I run rake gems:install these gems are re-installed.
Where are the references to the gems stored?

The standard way to define a gem dependency is in the environment configuration. It usually takes place in the environment.rb file for any environment, but some gems might be specified also per-environment. Check the environment files in config/environments.
Also make sure some file doesn't include the gem with the classic RubyGems gem command.
Finally, check these gems are not required by other gems or plugins used by your application.

Related

Bundler is deprecating bundle console in favor of bin/console. Can anyone provide more clarity as to how bin/console should work?

I have a custom ruby gem that relies heavily on bundle console. Nothing special or fancy, just an interactive console with the set of gems defined by the Gemfile included. We use the console a lot during development.
Currently when I run the command, I receive the following deprecation message:
[DEPRECATED] bundle console will be replaced by bin/console generated by bundle gem <name>
Digging around in the bundler docs I found the following explanation:
The bundle console will be removed and replaced with bin/console.
Over time we found bundle console hard to maintain because every
user would want to add her own specific tweaks to it. In order to
ease maintenance and reduce bikeshedding discussions, we're removing
the bundle console command in favor of a bin/console script
created by bundle gem on gem generation that users can tweak to
their needs.
Can anyone with knowledge provide a more detailed explanation? This gem currently does not have a bin directory. I'm happy to make one, I'm just not sure what should be in the file. Running bundle gem as described in the note above raises an error (as expected).
This is the file that is generated at bin/console:
#!/usr/bin/env ruby
require "bundler/setup"
require "(your gem name here)"
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start
require "irb"
IRB.start(__FILE__)
You can see the template in the rubygems GitHub repo.

Rails does not load gems from vendor/gems

I have strange old buggy project on Rails 2.
It have gem's dependencies in config/environment.rb like
config.gem "andand"
config.gem "json"
config.gem "chronic"
config.gem "mini_fb"
all those gems are located in vendor/gems/
andand-1.3.3/
chronic-0.6.7/
json-1.7.3/
mini_fb-1.1.7/
rbet-1.0.3/
redis-3.0.1/
responsys_client-0.0.1/
but when i start unicorn server with this app it always complain that it can't find this gems. Why?
UPDATE
After building and installing gem from vendor/gems rails still complain about it.
I have tweake mini_fb gem into custom mini_fb_custom gem. Changed all references in gemspec and other files from mini_fb to mini_fb_my, installed it and it is shown in gem list as mini_fb_my. But it fails to load from config/environment.rb and complains that
Missing these required gems:
mini_fb_my >= 0
maybe i should rename lib/mini_fb.rb to lib/mini_fb_my.rb
i'll check it.
UPDATE 2
Yes, renaming files rocks!
You still need to install them from those folders, or unicorn will not know where to look for them.
Just install the gems from that directory and unicorn should pick them up.
UPDATE
You can install your gems locally with this command
gem install --local vendor/gems/gem/gem-name.gem
On more recent versions of rails you just specify path on the Gemfile
gem "gem-name", path: "path/to/gem"
My advice: replace the obsolete gem configuration with bundler (it works fine with rails 2, there should be a tutorial for rails 2 available on their website).
Configuration through gem command, freezing gems, etc. is just pain in the a** and it seemed kinda buggy to me when I'd used it (long time ago).

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?
config.gem should cause the gem to be automatically required. You should not need to do a manual 'require' call.
config.gem
Tells Rails to load this gem automatically
Tells Rails that this gem is needed for the application, so that rake gems:install will install it
The :source option can tell rails to get it from a nonstandard repository
The :lib option can tell rails to load a non-standard file from the gem (i.e. something not named after the gem itself)
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.

How to freeze a gem that doesn't want to freeze? (rack)

When I try to deploy my rails app to my shared hosting (dreamhost) I get this error:
can't activate rack (~> 1.0.1, runtime) for [], already activated rack-1.0.0 for []
So I want to freeze the rack gem in my dev environment, and add it to the project, but even though I have this in my config/environment.rb:
config.gem 'rack'
Doing a rake gems:unpack:dependencies doesn't freeze the gem.
ajmbp:trunk ajl$ rake gems:unpack:dependencies
(in /Users/ajl/dev/site/trunk)
ajmbp:trunk ajl$ ll vendor/gems/
.svn/ haml-2.2.14/ net-ssh-2.0.16/
I tried adding another gem to the config.gem just to test (RedCloth) and that does work as expected, but no luck with rack.
Any ideas?
Thanks!
Have you checked your environment variables (RUBY_GEMS I think it is, or something like that)?
Also, your environment.rb and (if you use fastcgi) your .fcgi.
Make sure the paths are ordered from highest priority to lowest.
See this article.
Any gem that is loaded in your Rakefile (e.g. metric_fu, vlad, etc) is considered to be a ‘framework gem’ by Rails, and such gems are not unpacked. Given that the vendor/gems directory is not yet in the load path when the Rakefile is loading, this is probably a good idea.
In other words, if you have a library that provides Rake tasks, or is otherwise necessary for your .rake files to be valid, don’t expect “config.gem” and friends to handle it for you.
For Dreamhost see their own documentation on installing your own gems. Or see this excellent article on how to load your own packages and gems (be warned it's not for the faint of heart).
But all my reading is telling me that you may still run into trouble because of the way Passenger may be using the Rack version installed by Dreamhost.
Good Luck.

config.gem in environment.rb

Let's say in a Rails app you have some gems that you use in your app (we'll call them "primary gems") and you have vendored them for portability.
Let's say that those "primary gems" also require gems of their own - we'll call these "secondary gems".
When you are setting up your environment.rb, you have to say:
config.gem 'primary-gem'
for any of the gems you are using directly.
But, do you also need to say . . .
config.gem 'secondary-gem'
even if you are not using that gem explicitly in your app?
Or is it simply enough to include the gem in your vendor/gems directory for it to get picked up by your app?
At deploy time rails knows about your dependencies, so if you want to freeze your gems then you can run
rake gems:unpack:dependencies
to freeze them into the vendor directory.
At runtime however it's the gems job to load it's dependencies, and usually the gems do this, so a config.gem 'primary' should work.
No, you don't or at least you shouldn't. Each GEM specification should include it's own list of dependencies. When primary gem is installed, RubyGems automatically will install each gem dependency on cascade.
In other words, if A requires B that requires C+D, you only need to write
config.gem 'A'
When the command
gem install A
is run, RubyGems will resolve all the dependencies and install them.
You can view all A dependencies running (from a Rails project)
rake gems
Sometimes, a GEM author may forget to include some GEM dependencies in the specification. In this case you should specify them in your environment.rb to force the application to install them. Off course, it's also a good idea to contact the GEM maintainer so that it can fix the problem.

Resources