Skip to content

Commit

Permalink
Deprecate RAILS_ROOT in favor of Rails.root (which proxies to the app…
Browse files Browse the repository at this point in the history
…lication's object root)
  • Loading branch information
Carl Lerche committed Oct 16, 2009
1 parent 6094e65 commit 2110a52
Show file tree
Hide file tree
Showing 40 changed files with 138 additions and 141 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/metal/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module Helpers
included do
# Set the default directory for helpers
extlib_inheritable_accessor(:helpers_dir) do
defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers"
defined?(Rails) ? "#{Rails.root}/app/helpers" : "app/helpers"
end
end

Expand Down
2 changes: 2 additions & 0 deletions activesupport/lib/active_support/testing/isolation.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "active_support/core_ext/load_error"

module ActiveSupport
module Testing
class ProxyTestResult
Expand Down
1 change: 1 addition & 0 deletions railties/bin/rails
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ end

ARGV << "--help" if ARGV.empty?

require 'rails'
require 'rails/generators'
require 'rails/generators/rails/app/app_generator'

Expand Down
4 changes: 2 additions & 2 deletions railties/builtin/rails_info/rails/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def to_html

protected
def rails_vendor_root
@rails_vendor_root ||= "#{RAILS_ROOT}/vendor/rails"
@rails_vendor_root ||= "#{Rails.root}/vendor/rails"
end

def git_info
Expand Down Expand Up @@ -124,7 +124,7 @@ def git_info

# The application's location on the filesystem.
property 'Application root' do
File.expand_path(RAILS_ROOT)
File.expand_path(Rails.root)
end

# The current Rails environment (development, test, or production).
Expand Down
2 changes: 1 addition & 1 deletion railties/guides/source/2_2_release_notes.textile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ If you want to generate these guides locally, inside your application:
rake doc:guides
</ruby>

This will put the guides inside +RAILS_ROOT/doc/guides+ and you may start surfing straight away by opening +RAILS_ROOT/doc/guides/index.html+ in your favourite browser.
This will put the guides inside +Rails.root/doc/guides+ and you may start surfing straight away by opening +Rails.root/doc/guides/index.html+ in your favourite browser.

* Lead Contributors: "Rails Documentation Team":http://guides.rails.info/credits.html
* Major contributions from "Xavier Noria":http://advogato.org/person/fxn/diary.html and "Hongli Lai":http://izumi.plan99.net/blog/.
Expand Down
2 changes: 1 addition & 1 deletion railties/guides/source/action_controller_overview.textile
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ class ClientsController < ApplicationController
# Stream a file that has already been generated and stored on disk.
def download_pdf
client = Client.find(params[:id])
send_data("#{RAILS_ROOT}/files/clients/#{client.id}.pdf",
send_data("#{Rails.root}/files/clients/#{client.id}.pdf",
:filename => "#{client.name}.pdf",
:type => "application/pdf")
end
Expand Down
8 changes: 4 additions & 4 deletions railties/guides/source/command_line.textile
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,10 @@ INFO: For a good rundown on generators, see "Understanding Generators":http://wi

Generators are code that generates code. Let's experiment by building one. Our generator will generate a text file.

The Rails generator by default looks in these places for available generators, where RAILS_ROOT is the root of your Rails application, like /home/foobar/commandsapp:
The Rails generator by default looks in these places for available generators, where Rails.root is the root of your Rails application, like /home/foobar/commandsapp:

* RAILS_ROOT/lib/generators
* RAILS_ROOT/vendor/generators
* Rails.root/lib/generators
* Rails.root/vendor/generators
* Inside any plugin with a directory like "generators" or "rails_generators"
* ~/.rails/generators
* Inside any Gem you have installed with a name ending in "_generator"
Expand Down Expand Up @@ -465,7 +465,7 @@ We take whatever args are supplied, save them to an instance variable, and liter

* Check there's a *public* directory. You bet there is.
* Run the ERb template called "tutorial.erb".
* Save it into "RAILS_ROOT/public/tutorial.txt".
* Save it into "Rails.root/public/tutorial.txt".
* Pass in the arguments we saved through the +:assign+ parameter.

Next we'll build the template:
Expand Down
4 changes: 2 additions & 2 deletions railties/lib/rails/backtrace_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BacktraceCleaner < ActiveSupport::BacktraceCleaner

def initialize
super
add_filter { |line| line.sub("#{RAILS_ROOT}/", '') }
add_filter { |line| line.sub("#{Rails.root}/", '') }
add_filter { |line| line.sub(ERB_METHOD_SIG, '') }
add_filter { |line| line.sub('./', '/') } # for tests

Expand All @@ -37,7 +37,7 @@ def add_gem_filters
add_filter { |line| line.sub(/(#{path})\/gems\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) \4')}
end

vendor_gems_path = Rails::GemDependency.unpacked_path.sub("#{RAILS_ROOT}/",'')
vendor_gems_path = Rails::GemDependency.unpacked_path.sub("#{Rails.root}/",'')
add_filter { |line| line.sub(/(#{vendor_gems_path})\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) [v] \4')}
end
end
Expand Down
1 change: 0 additions & 1 deletion railties/lib/rails/commands/about.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "#{RAILS_ROOT}/config/application"
Rails.application.new
require 'rails/info'
puts Rails::Info
2 changes: 1 addition & 1 deletion railties/lib/rails/commands/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end

libs = " -r irb/completion"
libs << %( -r "#{RAILS_ROOT}/config/environment")
libs << %( -r "#{Rails.root}/config/environment")
libs << " -r rails/console_app"
libs << " -r rails/console_sandbox" if options[:sandbox]
libs << " -r rails/console_with_helpers"
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/commands/dbconsole.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
end

env = ARGV.first || ENV['RAILS_ENV'] || 'development'
unless config = YAML::load(ERB.new(IO.read(RAILS_ROOT + "/config/database.yml")).result)[env]
unless config = YAML::load(ERB.new(IO.read(Rails.root + "/config/database.yml")).result)[env]
abort "No database is configured for the environment '#{env}'"
end

Expand Down
1 change: 0 additions & 1 deletion railties/lib/rails/commands/destroy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'generators'))
require "#{RAILS_ROOT}/config/application"

if ARGV.size == 0
Rails::Generators.help
Expand Down
4 changes: 2 additions & 2 deletions railties/lib/rails/commands/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
:Port => 3000,
:Host => "0.0.0.0",
:environment => (ENV['RAILS_ENV'] || "development").dup,
:config => RAILS_ROOT + "/config.ru",
:config => Rails.root + "/config.ru",
:detach => false,
:debugger => false
}
Expand Down Expand Up @@ -46,7 +46,7 @@

if options[:detach]
Process.daemon
pid = "#{RAILS_ROOT}/tmp/pids/server.pid"
pid = "#{Rails.root}/tmp/pids/server.pid"
File.open(pid, 'w'){ |f| f.write(Process.pid) }
at_exit { File.delete(pid) if File.exist?(pid) }
end
Expand Down
34 changes: 11 additions & 23 deletions railties/lib/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,25 @@ def after_initialize(&blk)

def root
@root ||= begin
if defined?(RAILS_ROOT)
root = RAILS_ROOT
else
call_stack = caller.map { |p| p.split(':').first }
root_path = call_stack.detect { |p| p !~ %r[railties/lib/rails] }
root_path = File.dirname(root_path)

while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/config.ru")
parent = File.dirname(root_path)
root_path = parent != root_path && parent
end

Object.class_eval("RAILS_ROOT = ''")
call_stack = caller.map { |p| p.split(':').first }
root_path = call_stack.detect { |p| p !~ %r[railties/lib/rails] }
root_path = File.dirname(root_path)

root = File.exist?("#{root_path}/config.ru") ? root_path : Dir.pwd
while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/config.ru")
parent = File.dirname(root_path)
root_path = parent != root_path && parent
end

root = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ?
Pathname.new(root).expand_path.to_s :
Pathname.new(root).realpath.to_s
root = File.exist?("#{root_path}/config.ru") ? root_path : Dir.pwd

# TODO: Remove RAILS_ROOT
RAILS_ROOT.replace(root)
root
RUBY_PLATFORM =~ /(:?mswin|mingw)/ ?
Pathname.new(root).expand_path :
Pathname.new(root).realpath
end
end

def root=(root)
Object.class_eval("RAILS_ROOT = ''") unless defined?(RAILS_ROOT)
RAILS_ROOT.replace(root)
@root = root
@root = Pathname.new(root).expand_path
end

def paths
Expand Down
4 changes: 2 additions & 2 deletions railties/lib/rails/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Rails
# TODO: w0t?
class << self
def application
@@application
@@application ||= nil
end

def application=(application)
Expand Down Expand Up @@ -43,7 +43,7 @@ def backtrace_cleaner
end

def root
Pathname.new(RAILS_ROOT) if defined?(RAILS_ROOT)
application && application.config.root
end

def env
Expand Down
17 changes: 17 additions & 0 deletions railties/lib/rails/deprecation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "active_support/string_inquirer"
require "active_support/deprecation"

RAILS_ROOT = (Class.new(ActiveSupport::Deprecation::DeprecationProxy) do
def target
Rails.root
end

def replace(val)
puts OMG
end

def warn(callstack, called, args)
msg = "RAILS_ROOT is deprecated! Use Rails.root instead."
ActiveSupport::Deprecation.warn(msg, callstack)
end
end).new
2 changes: 1 addition & 1 deletion railties/lib/rails/gem_dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GemDependency < Gem::Dependency
attr_accessor :lib, :source, :dep

def self.unpacked_path
@unpacked_path ||= File.join(RAILS_ROOT, 'vendor', 'gems')
@unpacked_path ||= File.join(Rails.root, 'vendor', 'gems')
end

@@framework_gems = {}
Expand Down
10 changes: 5 additions & 5 deletions railties/lib/rails/generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def self.gems_generators_paths
generator_path = File.join(spec.full_gem_path, "lib/generators")
paths << generator_path if File.exist?(generator_path)
end
elsif defined?(RAILS_ROOT)
paths += Dir[File.join(RAILS_ROOT, "vendor", "gems", "gems", "*", "lib", "generators")]
elsif defined?(Rails.root)
paths += Dir[File.join(Rails.root, "vendor", "gems", "gems", "*", "lib", "generators")]
end

paths
Expand All @@ -102,8 +102,8 @@ def self.gems_generators_paths
# Load paths from plugin.
#
def self.plugins_generators_paths
return [] unless defined?(RAILS_ROOT)
Dir[File.join(RAILS_ROOT, "vendor", "plugins", "*", "lib", "generators")]
return [] unless Rails.root
Dir[File.join(Rails.root, "vendor", "plugins", "*", "lib", "generators")]
end

# Hold configured generators fallbacks. If a plugin developer wants a
Expand Down Expand Up @@ -143,7 +143,7 @@ def self.no_color!
def self.load_paths
@load_paths ||= begin
paths = []
paths << File.join(RAILS_ROOT, "lib", "generators") if defined?(RAILS_ROOT)
paths << File.join(Rails.root, "lib", "generators") if Rails.root
paths << File.join(Thor::Util.user_home, ".rails", "generators")
paths += self.plugins_generators_paths
paths += self.gems_generators_paths
Expand Down
4 changes: 2 additions & 2 deletions railties/lib/rails/generators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ def self.inherited(base) #:nodoc:
super
base.source_root # Cache source root

if defined?(RAILS_ROOT) && base.name !~ /Base$/
path = File.expand_path(File.join(RAILS_ROOT, 'lib', 'templates'))
if Rails.root && base.name !~ /Base$/
path = File.expand_path(File.join(Rails.root, 'lib', 'templates'))
if base.name.include?('::')
base.source_paths << File.join(path, base.base_name, base.generator_name)
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# -- all .rb files in that directory are automatically loaded.

# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
# config.load_paths += %W( #{root}/extras )

# Specify gems that this application depends on and have them installed with rake gems:install
# config.gem "bj"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb

RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)

module Rails
# Mark the version of Rails that generated the boot.rb file. This is
# a temporary solution and will most likely be removed as Rails 3.0
Expand Down Expand Up @@ -70,7 +68,6 @@ class VendorBoot < Boot
def load_initializer
require "rails"
install_gem_spec_stubs
Rails::GemDependency.add_frozen_gem_path
end

def install_gem_spec_stubs
Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'rails/paths'
require 'rails/core'
require 'rails/configuration'
require 'rails/deprecation'

RAILS_ENV = (ENV['RAILS_ENV'] || 'development').dup unless defined?(RAILS_ENV)

Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/plugin/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def effective_order_of(plugin)
end

def application_lib_index
$LOAD_PATH.index(File.join(RAILS_ROOT, 'lib')) || 0
$LOAD_PATH.index(File.join(Rails.root, 'lib')) || 0
end

def enabled?(plugin)
Expand Down
4 changes: 2 additions & 2 deletions railties/lib/rails/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@

# Load any custom rakefile extensions
# TODO: Don't hardcode these paths.
Dir["#{RAILS_ROOT}/vendor/plugins/*/**/tasks/**/*.rake"].sort.each { |ext| load ext }
Dir["#{RAILS_ROOT}/lib/tasks/**/*.rake"].sort.each { |ext| load ext }
Dir["#{Rails.root}/vendor/plugins/*/**/tasks/**/*.rake"].sort.each { |ext| load ext }
Dir["#{Rails.root}/lib/tasks/**/*.rake"].sort.each { |ext| load ext }

2 comments on commit 2110a52

@jpartogi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In "railties/lib/rails/generators/rails/app/templates/config/boot.rb" there's still RAILS_ROOT:

Should that be changed too? Otherwise this error will showup:
config/boot.rb:35:in `preinitializer_path': uninitialized constant Module::RAILS_ROOT (NameError)

@carllerche
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.. thanks for pointing it out :)

Please sign in to comment.