ruby_object
ruby_object

Reputation: 1266

How to optionally exclude gem pg from the Gemfile?

In our team some people do not have pg gem installed on their machines. At the moment we use two seprate database configs that are being copied to database.yml. We had problems with that approach because we had to keep commenting out gem pg in our Gemfile. So I tried following in our Gemfile:

unless ['host1, 'host2'].include? `hostname`.strip!
  gem 'pg'
end

It seemed to work, but the boss wants a better solution, so that he can test the app on his laptop without having to install Postgres and without having his hostname in the Gemfile.

Gem::Specification.all_names

doesn't show pg being installed although 'gem list pg --local' shows it is installed.

Trying to use gem list pg --local in the Gemfile doesn't work because the system seems to go into infinite loop if you don't have pg installed.

Is there something similar to 'Gem::Specification.all_names' that correctly shows list of installed gems that could be used in optional excluding of gems in a Gemfile. Or is there a better way to use gifferent Gems on different machines for the above mentioned scenario?

note

if File.open('./config/database.yml').read.each_line.first.index('Postgre').is_a?(Integer)
 gem 'pg'
end

seems to work but now I get this when I run bundle install:

Your bundle is complete!
Gems in the group postgres were not installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

any idea where it comes form?

note 2

'Gems in the group postgres were not installed' was fixed after running: rm -r ./.bundle

Upvotes: 1

Views: 1025

Answers (1)

max
max

Reputation: 102222

One possible solutions would be to use a custom environment and bundler group.

You might have noticed this line in config/application.rb:

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

Which means running rails s -e bossmode would require the gems in:

group :bossmode do
  # no pg in here...
end

An even better solution would be to convince your boss to KISS and use PG. The performance cost even on a lowly macbook air is tiny.

Upvotes: 1

Related Questions