Configuring gems for your rails app

Until I found this (recently), I have been guilty of not configuring my gems for my rails apps.  Apparently these features have been around for a while and can be really helpful when deploying or doing team development with rails and gems.

What it does

  • Requires gems automatically
  • Documents the gems you used
  • Allows you to specify gems by environment.   This is key so you don’t have to require testing gems for a production environment.
  • Provides rake tasks for freezing,  installing and checking the status of the required gems.
  • helps to ensure that functionality you depend on gems for will be working.

Rake Tasks

Here is a listing of gem related rake tasks.

 rake gems                             # List the gems that this rails application depends on
 rake gems:build                       # Build any native extensions for unpacked gems
 rake gems:install                     # Installs all required gems.
 rake gems:refresh_specs               # Regenerate gem specifications in correct format.
 rake gems:unpack                      # Unpacks all required gems into vendor/gems.
 rake gems:unpack:dependencies         # Unpacks all required gems and their dependencies into vendor/gems.
 rake rails:freeze:gems                # Lock this application to the current gems (by unpacking them into vendor/rails)
 rake rails:unfreeze                   # Unlock this application from freeze of gems or edge and return to a fluid use of system gems
  • rake gems:unpack variants are helpful to make sure that you deploy the same gems from your development environment to your production environment, even if it’s on a shared host.
  • rake rails:freeze:gems does the same for the rails gems.
  • A simple rake gems will give you an overview of your gems like this:
rubyyot@atlas:$ rake gems
(in /home/rubyyot/working/examples)
 - [F] ruby-openid
 - [F] paperclip
 - [I] RedCloth
 - [F] sqlite3-ruby
 - [F] ruby-openid >= 2.0.4
I = Installed
F = Frozen
R = Framework (loaded before rails starts)

Example

Here are my configured gems from a project I’m currently working on.

config/enviroment.rb
  config.gem "ruby-openid", :lib => 'openid'
  config.gem "paperclip"
  config.gem "RedCloth", :lib => 'redcloth'
config/environments/development.rb
  config.gem "sqlite3-ruby", :lib => "sqlite3"
config/environments/test.rb
 config.gem "notahat-machinist", :lib > 'machinist', :version => '0.3.1'
 config.gem "faker", :version => '0.3.1'
 config.gem "sqlite3-ruby", :lib => "sqlite3"
config/environments/production.rb
config.gem "mysql", :lib => "mysql"

More

If you are interested, there is a Railscast episode on this topic.

Tags: , , ,

2 Responses to “Configuring gems for your rails app”

  1. [...] fact, something I failed to cover is configuring your gems. Something you might want to take a minute to [...]

  2. [...] Configuring gems for Rails – Manage dependencies by documenting the gems you are using and requiring certain versions. You can also freeze a version of each to your app. [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">