codahale.com٭blog

This is my old blog. My current writing is here: codahale.com

Rails Environments: a plugin for, well, Rails

This one’s simple. Sometimes I need to know what environment Rails is running in (for example, so I can display additional debugging information, or not link to the HTTPS version of an action), and typing out ENV['RAILS_ENV'] is a hassle and looks ugly. Hence, this plugin.

Installation

Are you down with SVN? Yeah, you know me!


./script/plugin install -x http://svn.codahale.com/rails_environments/trunk

Think source control is for the weak?


./script/plugin install http://svn.codahale.com/rails_environments/trunk

Usage

Rails Environments drops five hot new methods in your lap:


if Rails.production?
  'Production'
elsif Rails.development?
  'Development'
elsif Rails.test?
  'Test'
elsif Rails.none?
  'Oh, You Gotta Be Kidding Me'
else
  "Dude, what the hell: #{Rails.environment}"
end

Not complicated, sure, but it makes my code pretty. Ponies!

4 Responses to “Rails Environments: a plugin for, well, Rails”

  1. Paolo Dona Says:

    Simple and effective. Thanks a lot man. It’s weird such a function is not bundled with rails itself, as it really should.

  2. Jon Maddox Says:

    excellent. I do this all the time. This is handy.

  3. nixxbox writes » Blog Archive » MySQL, Rails, Unit Tests, and Foreign Keys Says:

    [...] See, it’s that easy. Note, that the code above requires the Rails environments plugin. It’s not absolutely necessary, but makes the code look nicier. Without this plugin one would have to test for environment like this: [...]

  4. Panasonic Youth – Staging Environments in Rails Says:

    [...] to have less constants in my code, as I feel all caps detracts from the beauty of Ruby. You can use Coda’s plugin or write your own in probably 5 minutes. Since you have a level of indirection around your [...]