Rails 3 Gem Developers: Make your Rails-dependent gems as library agnostic as Rails itself

I ran into an issue last night when trying to set up a new Rails 3 project with DataMapper and HAML. Once I required the haml & haml-rails gems, I noticed that bundler was requiring ActiveRecord and every other Rails 3 gem, even though I was trying to use only DataMapper. The culprit turned out to be an overly broad gem dependency specified in haml-rails. From haml-rails:

s.add_dependency "rails", "~>3.0.0

By requiring Rails in its entirety, haml-rails was dependent upon every gem that Rails itself required, even those it did not actually need:

s.add_dependency('activesupport',  version)
s.add_dependency('actionpack',     version)
s.add_dependency('activerecord',   version)
s.add_dependency('activeresource', version)
s.add_dependency('actionmailer',   version)
s.add_dependency('railties',       version)

I’ve submitted the following patch to haml-rails to correct this situation:

  Gem::Specification.new do |s|
   s.rubyforge_project         = "haml-rails"
   s.required_rubygems_version = ">= 1.3.6"

-  s.add_dependency "rails", "~>3.0.0"
+  s.add_dependency "activesupport", "~> 3.0.0"
+  s.add_dependency "actionpack", "~> 3.0.0"
+  s.add_dependency "railties", "~> 3.0.0"
   s.add_dependency "haml", "~>3.0.18"
+  s.add_development_dependency "rails", "~> 3.0.0"
   s.add_development_dependency "bundler", "~> 1.0.0"

   s.files        = `git ls-files`.split("\n")
   s.executables  = `git ls-files`.split("\n").select{|f| f =~ /^bin/}

Anybody who is developing a ruby gem specifically for Rails 3/Railties integration, please remember to only require the libraries you actually need. Be a good citizen in the wonderfully agnostic Rails 3 world!


After working through this patch with @indirect (Andre Arko), we discovered that it is important to make sure that you require "rails" within your gem. The Rails library in this case is provided by the railties gem, not the rails gem.