litany against fear

coding with spice ¤ by nick quaranto

The Rails Module (in Rails 3)

published 03 Feb 2010

So, you may have noticed this in the Rails 3 Changelog

Railties now deprecates:

RAILS_ROOT in favour of Rails.root,
RAILS_ENV in favour of Rails.env, and
RAILS_DEFAULT_LOGGER in favour of Rails.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!



email twitter github