litany against fear
The Rails Module (in Rails 3)
So, you may have noticed this in the Rails 3 Changelog…
Railties now deprecates:
RAILS_ROOTin favour ofRails.root,
RAILS_ENVin favour ofRails.env, and
RAILS_DEFAULT_LOGGERin favour ofRails.logger.
Great…but why? Better alternatives have existed for a while in Rails core (some since 2.1.0), and it’s about damn time you start using them properly. There’s also some other helpful methods on the Rails module we’ll explore in this post.
Rails.root
This is a big one. Every Rails developer has done File.join(RAILS_ROOT, "path", "to", "something") before. Stop that. And don’t just replace RAILS_ROOT with Rails.root either. Rails.root is a Pathname, which means you can do cool stuff like this:
$ rails console
>> Rails.root
=> #<Pathname:/Users/qrush/Dev/ruby/new_app>
>> Rails.root.join("config", "database.yml")
=> #<Pathname:/Users/qrush/Dev/ruby/new_app/config/database.yml>
>> _.read
=> "development:\n ...
Rails.env
Same deal, you’ve probably done something like if RAILS_ENV == "production" in your Rails apps. Stop that too. Oh, you thought this would just be a String?
$ rails console
>> Rails.env
=> "development"
>> Rails.env.class
=> ActiveSupport::StringInquirer
Whaaaat? Actually, this is a really neat utility. From activesupport/lib/active_support/string_inquirer.rb:
module ActiveSupport
class StringInquirer < String
def method_missing(method_name, *arguments)
if method_name.to_s[-1,1] == "?"
self == method_name.to_s[0..-2]
else
super
end
end
end
end
Awesome. This lets us do stuff like this in Gemcutter:
if Rails.env.development? || Rails.env.test?
include Vault::FS
else
include Vault::S3
end
Rails.logger
This is your favorite Logger class, just now without an annoying constant name of RAILS_DEFAULT_LOGGER. Much easier to remember.
$ rails console
>> Rails.logger
=> #<ActiveSupport::BufferedLogger:0x21de384 ...
>> Rails.logger.info "zomg!"
=> "zomg!\n"
>> File.read("log/development.log")
=> "zomg!\n"
Rails.public_path
A helpful shortcut to what your public assets directory is called, probably to use with Rails.root. (Why this isn’t a Pathname is beyond me, sounds like a good patch to whip up!)
$ rails console
>> Rails.public_path
=> "public"
Rails.cache
Now the rabbit hole goes deeper. This is a unified interface to memory/file/you name it caching stores that can be used with Rails. If you’ve ever made some sort of caching global variable, like $memcache or CACHE, you should read up here.
$ rails console
>> Rails.cache
=> #<ActiveSupport::Cache::MemoryStore:0x21e04b8 @data={}>
>> Rails.cache.write("rush", "limelight")
=> "limelight"
>> Rails.cache.read("rush")
=> "limelight"
>> Rails.cache
=> #<ActiveSupport::Cache::MemoryStore:0x21e04b8
@data={"rush"=>"limelight"}>
Rails.application
The new Rails::Application class encapsulates a lot of what was thrown around in Railties in previous releases of Rails, and really represents the ultimate embracing of Rack’s modularity. Yehuda’s post can explain it further, but the important thing is now you can run multiple Rails::Applications in the same process if you need to, and it’s promoting decoupling even further by starting from the inside of the framework. Awesome.
$ rails console
>> Rails.application
=> #<NewApp::Application:0x13896b0 ...
>> Rails.application.routes
=> #<ActionDispatch::Routing::RouteSet:0x162877c ...
>> Rails.application.routes.recognize_path("rails/info/properties")
=> {:controller=>"rails/info", :action=>"properties"}
Rails.configuration
This gives you global access to all of the configuration data set up in your config/application.rb and various config/environments/#{Rails.env}.rb files, if you should ever need it.
$ rails console
>> Rails.configuration
=> #<Rails::Application::Configuration:0x7e1ab0 ...
>> pp Rails.configuration.middleware
[ActionDispatch::Static,
Rack::Lock,
Rack::Runtime,
Rails::Rack::Logger,
ActionDispatch::ShowExceptions,
ActionDispatch::Callbacks,
ActionDispatch::Cookies,
ActionDispatch::Session::CookieStore,
ActionDispatch::Flash,
ActionDispatch::Cascade,
ActionDispatch::ParamsParser,
Rack::MethodOverride,
ActionDispatch::Head,
ActiveRecord::ConnectionAdapters::ConnectionManagement,
ActiveRecord::QueryCache]
=> nil
Wrapup
I’m sure some are missing here (like Rails.version), but these are the ones that I think matter most to Rails developers. If something else should be covered here, let me know!
archive
- The Rails Module (in Rails 3)03 Feb 2010
- Load Rails conditionally with Rack10 Nov 2009
- Gem Bundler is the Future14 Oct 2009
- On Gem Forking09 Oct 2009
- RailsCamp NE Adventures20 Jul 2009
- RailsConf 2009 Webrat Rails Testing Evolved07 May 2009
- RailsConf 2009 Automated Code Quality Checking in Ruby and Rails07 May 2009
- RailsConf 2009 And the Greatest of these is Rack Support07 May 2009
- RailsConf 2009 Working Effectively with Legacy Rails06 May 2009
- RailsConf 2009 What Makes Ruby Go An Implementation Primer06 May 2009
- RailsConf 2009 Starting up Fast Lessons from the Rails Rumble06 May 2009
- RailsConf 2009 Robert Martin Keynote06 May 2009
- RailsConf 2009 Rails 3 Step off of the Golden Path06 May 2009
- RailsConf 2009 Getting to know Ruby 1.906 May 2009
- RailsConf 2009 Chris Wansrath Keynote06 May 2009
- RailsConf 2009 The Future of Deployment05 May 2009
- RailsConf 2009 Smacking Git Around05 May 2009
- RailsConf 2009 Rails is from Mars Ruby is From Venus05 May 2009
- RailsConf 2009 Notes05 May 2009
- RailsConf 2009 Guitar Hero Behind The Music05 May 2009
- RailsConf 2009 DHH Keynote05 May 2009
- RailsConf 2009 A Morning with GitHub05 May 2009
- Git Started with Git01 May 2009
- BarCamp Boston 4 Roundup26 Apr 2009
- Checkout tracked remote branch with Git07 Jan 2009
- If you only could follow 10 people on Twitter...11 Dec 2008
- Open Source Collaboration with Git and GitHub05 Dec 2008
- GitHub Rebase #601 Dec 2008
- Using Berkeley DB and Ruby for Large Data Sets Notes20 Nov 2008
- Ruby: Fragile or Agile? Notes20 Nov 2008
- RESTful Possibilities - REST in Rails and Beyond Notes20 Nov 2008
- Professional Ruby Conference Wrapup20 Nov 2008
- Contributing to Insoshi with Git and GitHub Notes20 Nov 2008
- Testing as Communication: Real-World Techniques Notes19 Nov 2008
- Surviving as a Windows Based Rails Developer Notes19 Nov 2008
- Sinful Ruby Notes19 Nov 2008
- Rock Solid Ruby Deployments Notes19 Nov 2008
- Rails AntiPatterns Notes19 Nov 2008
- Payment Processing with ActiveMerchant Notes19 Nov 2008
- Mobile Developments Notes19 Nov 2008
- Messaging with Ruby on Rails Notes19 Nov 2008
- Battle Royale: Merb's Role in the MVC Holy Wars Notes19 Nov 2008
- 5 Great Talks Notes19 Nov 2008
- The many facets of Ruby at AT&T Interactive Notes18 Nov 2008
- Riding Rails at the New York Times Notes18 Nov 2008
- Refactoring Complex Domains in Ruby on Rails Notes18 Nov 2008
- Professional Ruby Conference Notes18 Nov 2008
- Professional Ruby Conference Keynote Notes18 Nov 2008
- Nature Network Stays Agile in the Enterprise Notes18 Nov 2008
- JRuby: Who What Now? Notes18 Nov 2008
- Introduction to Mongrel Notes18 Nov 2008
- Four Years of Ruby Development Notes18 Nov 2008
- Demystifying Rails Plugin Development Notes18 Nov 2008
- GitHub Rebase #417 Nov 2008
- GitHub Rebase #309 Nov 2008
- GitHub Rebase #203 Nov 2008
- GitHub Rebase #126 Oct 2008
- Pumpkin Carving 200820 Oct 2008
- Where do you get Ruby news from?19 Oct 2008
- Calculating Age in Rails13 Oct 2008
- Loading custom code in Rails22 Sep 2008
- Switching to Rails18 Sep 2008
- Raytracers, snowflakes, and Pac-Man, oh my!23 May 2008
- Rock Band Rocks.27 Mar 2008
- Why I love Twitter25 Mar 2008
- Pimping the Windows Command Line20 Mar 2008
- Defraggle Rock18 Mar 2008
- Turning off the Firehose16 Mar 2008
- 5 Things That Video Games Do That Really Make Me Angry13 Mar 2008
- Flash and WPF: A pain in the ass.09 Mar 2008
- I’m afraid of low level programming.27 Nov 2007
- Digg Labs: Flash doesn’t suck all the time.31 Oct 2007
- Why every programmer should play NetHack29 Oct 2007