Require gems based on platform - ruby-on-rails

I typically use Red Hat Linux during development; There I need to use gems like therubyracer and libv8. However, there are times I would like to have the ability to develop on Windows and not have these gems.
Is it possible to limit gems in the Gemfile based on operating system?

You can use groups
group :unix do
gem 'therubyracer'
gem 'libv8'
end
# when bundling on windows:
bundle install --without unix

Related

Pushing Ruby gems to own server so that it is publicly accessible

Is there any way to push Ruby gems to own sever? I want to make it publicly accessible (by an outside application). I have tried using geminabox but I have read that geminabox set up a sever to host gem within my organisation. Please suggest.
Thanks in advance.
USING GEMS FROM YOUR SERVER
Use the gem sources command to add the gem server to your system-wide gem sources.
gem sources --add http://localhost:9292
Then install gems as usual:
gem install secretgem
Successfully installed secretgem-0.0.1 1 gem installed
If you’re using Bundler then you can specify this server as a gem source in your Gemfile:
cat Gemfile
source "http://localhost:9292"
gem "secretgem"
bundle
Using secretgem (0.0.1)
Using bundler (1.0.13)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
Extracted from RubyGems Guides
You could use geminabox or simpler gem server, just create proxy ( use nginx )
and you could serve your gems world wide, Your customers should set source "your server " in their Gemfile.

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
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.

Ruby/rails/bundler: bundling only *some* of the required gems

I need to deploy a Rails app packaged up as a RHEL RPM. I want to bundle some of the gems it requires, but let the rest be satisfied from the production machine's system gems.
As an end result (for instance), I want the app to find some gems (like versionomy, for instance) in the app's vendor tree, but find the Rails activerecord, actionpack, and other gems in the host's system-wide gem library.
So far I've only found ways to bundle all-or-nothing. Can anyone point me to documentation explaining how to use Bundler and yet have $: be a search path, listing the app's bundled gems first and then the system's gems? Or if it's even possible?
Thanks!
specify path for gems in vendor directory
in Gemfile:
gem 'versionomy', :path => 'vendor/extensions'
Update
Yes it will work in deployment group.
You can specify wich gems are used in development, production and test
group :development, :test do
gem 'sqlite3'
gem 'thin'
end
group :production do
gem 'pg'
end
system path for gems in my case
/usr/local/lib/ruby/gems/1.9.1/gems/
check your install path by whereis gem
and if you are using rbenv
/home/username/.rbenv/versions/1.9.3-p362/lib/ruby/gems/1.9.1/gems/
You may have to install the requirements you don't want to provide as their own RPMs on the building machine. Then the RPM build process will require them but not provide them.

how to include all project gems into the gemfile?

I work as a developer supporting several apps built in Rails, often I need to install gems that are not included into the gem file.
Last day I build a project and when I try to run it on another computer I experienced some issues with missing gems, a lot actually and I didn't know where to get a list of all the missing gems or how to install them.
The question is, is there a way to include all the gems that the project needs into the gem file so next time someone try to run it on another computer it will be enough to use the comand bundle install.
You need to include the Gems in your Gemfile, and then run bundle install on ANY new machine in order to install those Gems and their dependencies. For example:
source 'https://rubygems.org'
gem 'rails', '3.2.6'
gem 'jquery-rails'
gem 'mongoid'
gem 'devise'
gem 'cancan'
With this example, all dependencies of rails such as Active Record, Action Pack and so on will be installed when bundler installs rails. The same for the remaining gems and their dependencies.
If you are planning, and it appears that you are, to spend much time with rails, you should really read up on Bundler.

How to set Gemfile for the need of different computer?

I have a rails app that need to be install on two computer (mac and linux)
somehow the mac can run the rails app with ruby-mysql gem and the linux can run with mysql gem
How do I set the bundle Gemfile?
You want to use the mysql2 gem in this case, otherwise there are tricks to install different gems based on the environment, however you will encounter annoyances when pulling from the repository since you will have to run bundle install (or update) everytime if Gemfile.lock is in your repo (it should)
gem "mysql2", "~> 0.2.7"
Platform example:
if RUBY_PLATFORM.downcase.include?("darwin")
gem "rb-fsevent"
gem "growl"
end
MacOS and Linux work fine with gem 'mysql' or 'mysql2'. it's just written on C++ and cross-platform

Resources