Skip to content

Commit 2110a52

Browse files
author
Carl Lerche
committed
Deprecate RAILS_ROOT in favor of Rails.root (which proxies to the application's object root)
1 parent 6094e65 commit 2110a52

40 files changed

+138
-141
lines changed

actionpack/lib/action_controller/metal/helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module Helpers
5454
included do
5555
# Set the default directory for helpers
5656
extlib_inheritable_accessor(:helpers_dir) do
57-
defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers"
57+
defined?(Rails) ? "#{Rails.root}/app/helpers" : "app/helpers"
5858
end
5959
end
6060

activesupport/lib/active_support/testing/isolation.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "active_support/core_ext/load_error"
2+
13
module ActiveSupport
24
module Testing
35
class ProxyTestResult

railties/bin/rails

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ end
1919

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

22+
require 'rails'
2223
require 'rails/generators'
2324
require 'rails/generators/rails/app/app_generator'
2425

railties/builtin/rails_info/rails/info.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def to_html
7575

7676
protected
7777
def rails_vendor_root
78-
@rails_vendor_root ||= "#{RAILS_ROOT}/vendor/rails"
78+
@rails_vendor_root ||= "#{Rails.root}/vendor/rails"
7979
end
8080

8181
def git_info
@@ -124,7 +124,7 @@ def git_info
124124

125125
# The application's location on the filesystem.
126126
property 'Application root' do
127-
File.expand_path(RAILS_ROOT)
127+
File.expand_path(Rails.root)
128128
end
129129

130130
# The current Rails environment (development, test, or production).

railties/guides/source/2_2_release_notes.textile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ If you want to generate these guides locally, inside your application:
5151
rake doc:guides
5252
</ruby>
5353

54-
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.
54+
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.
5555

5656
* Lead Contributors: "Rails Documentation Team":http://guides.rails.info/credits.html
5757
* Major contributions from "Xavier Noria":http://advogato.org/person/fxn/diary.html and "Hongli Lai":http://izumi.plan99.net/blog/.

railties/guides/source/action_controller_overview.textile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ class ClientsController < ApplicationController
653653
# Stream a file that has already been generated and stored on disk.
654654
def download_pdf
655655
client = Client.find(params[:id])
656-
send_data("#{RAILS_ROOT}/files/clients/#{client.id}.pdf",
656+
send_data("#{Rails.root}/files/clients/#{client.id}.pdf",
657657
:filename => "#{client.name}.pdf",
658658
:type => "application/pdf")
659659
end

railties/guides/source/command_line.textile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,10 @@ INFO: For a good rundown on generators, see "Understanding Generators":http://wi
424424

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

427-
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:
427+
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:
428428

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

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

471471
Next we'll build the template:

railties/lib/rails/backtrace_cleaner.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class BacktraceCleaner < ActiveSupport::BacktraceCleaner
1818

1919
def initialize
2020
super
21-
add_filter { |line| line.sub("#{RAILS_ROOT}/", '') }
21+
add_filter { |line| line.sub("#{Rails.root}/", '') }
2222
add_filter { |line| line.sub(ERB_METHOD_SIG, '') }
2323
add_filter { |line| line.sub('./', '/') } # for tests
2424

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

40-
vendor_gems_path = Rails::GemDependency.unpacked_path.sub("#{RAILS_ROOT}/",'')
40+
vendor_gems_path = Rails::GemDependency.unpacked_path.sub("#{Rails.root}/",'')
4141
add_filter { |line| line.sub(/(#{vendor_gems_path})\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) [v] \4')}
4242
end
4343
end

railties/lib/rails/commands/about.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require "#{RAILS_ROOT}/config/application"
21
Rails.application.new
32
require 'rails/info'
43
puts Rails::Info

railties/lib/rails/commands/console.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
end
1313

1414
libs = " -r irb/completion"
15-
libs << %( -r "#{RAILS_ROOT}/config/environment")
15+
libs << %( -r "#{Rails.root}/config/environment")
1616
libs << " -r rails/console_app"
1717
libs << " -r rails/console_sandbox" if options[:sandbox]
1818
libs << " -r rails/console_with_helpers"

railties/lib/rails/commands/dbconsole.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
end
2626

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

railties/lib/rails/commands/destroy.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'generators'))
2-
require "#{RAILS_ROOT}/config/application"
32

43
if ARGV.size == 0
54
Rails::Generators.help

railties/lib/rails/commands/server.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
:Port => 3000,
88
:Host => "0.0.0.0",
99
:environment => (ENV['RAILS_ENV'] || "development").dup,
10-
:config => RAILS_ROOT + "/config.ru",
10+
:config => Rails.root + "/config.ru",
1111
:detach => false,
1212
:debugger => false
1313
}
@@ -46,7 +46,7 @@
4646

4747
if options[:detach]
4848
Process.daemon
49-
pid = "#{RAILS_ROOT}/tmp/pids/server.pid"
49+
pid = "#{Rails.root}/tmp/pids/server.pid"
5050
File.open(pid, 'w'){ |f| f.write(Process.pid) }
5151
at_exit { File.delete(pid) if File.exist?(pid) }
5252
end

railties/lib/rails/configuration.rb

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,25 @@ def after_initialize(&blk)
3434

3535
def root
3636
@root ||= begin
37-
if defined?(RAILS_ROOT)
38-
root = RAILS_ROOT
39-
else
40-
call_stack = caller.map { |p| p.split(':').first }
41-
root_path = call_stack.detect { |p| p !~ %r[railties/lib/rails] }
42-
root_path = File.dirname(root_path)
43-
44-
while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/config.ru")
45-
parent = File.dirname(root_path)
46-
root_path = parent != root_path && parent
47-
end
48-
49-
Object.class_eval("RAILS_ROOT = ''")
37+
call_stack = caller.map { |p| p.split(':').first }
38+
root_path = call_stack.detect { |p| p !~ %r[railties/lib/rails] }
39+
root_path = File.dirname(root_path)
5040

51-
root = File.exist?("#{root_path}/config.ru") ? root_path : Dir.pwd
41+
while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/config.ru")
42+
parent = File.dirname(root_path)
43+
root_path = parent != root_path && parent
5244
end
5345

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

58-
# TODO: Remove RAILS_ROOT
59-
RAILS_ROOT.replace(root)
60-
root
48+
RUBY_PLATFORM =~ /(:?mswin|mingw)/ ?
49+
Pathname.new(root).expand_path :
50+
Pathname.new(root).realpath
6151
end
6252
end
6353

6454
def root=(root)
65-
Object.class_eval("RAILS_ROOT = ''") unless defined?(RAILS_ROOT)
66-
RAILS_ROOT.replace(root)
67-
@root = root
55+
@root = Pathname.new(root).expand_path
6856
end
6957

7058
def paths

railties/lib/rails/core.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Rails
66
# TODO: w0t?
77
class << self
88
def application
9-
@@application
9+
@@application ||= nil
1010
end
1111

1212
def application=(application)
@@ -43,7 +43,7 @@ def backtrace_cleaner
4343
end
4444

4545
def root
46-
Pathname.new(RAILS_ROOT) if defined?(RAILS_ROOT)
46+
application && application.config.root
4747
end
4848

4949
def env

railties/lib/rails/deprecation.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "active_support/string_inquirer"
2+
require "active_support/deprecation"
3+
4+
RAILS_ROOT = (Class.new(ActiveSupport::Deprecation::DeprecationProxy) do
5+
def target
6+
Rails.root
7+
end
8+
9+
def replace(val)
10+
puts OMG
11+
end
12+
13+
def warn(callstack, called, args)
14+
msg = "RAILS_ROOT is deprecated! Use Rails.root instead."
15+
ActiveSupport::Deprecation.warn(msg, callstack)
16+
end
17+
end).new

railties/lib/rails/gem_dependency.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class GemDependency < Gem::Dependency
1111
attr_accessor :lib, :source, :dep
1212

1313
def self.unpacked_path
14-
@unpacked_path ||= File.join(RAILS_ROOT, 'vendor', 'gems')
14+
@unpacked_path ||= File.join(Rails.root, 'vendor', 'gems')
1515
end
1616

1717
@@framework_gems = {}

railties/lib/rails/generators.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def self.gems_generators_paths
9292
generator_path = File.join(spec.full_gem_path, "lib/generators")
9393
paths << generator_path if File.exist?(generator_path)
9494
end
95-
elsif defined?(RAILS_ROOT)
96-
paths += Dir[File.join(RAILS_ROOT, "vendor", "gems", "gems", "*", "lib", "generators")]
95+
elsif defined?(Rails.root)
96+
paths += Dir[File.join(Rails.root, "vendor", "gems", "gems", "*", "lib", "generators")]
9797
end
9898

9999
paths
@@ -102,8 +102,8 @@ def self.gems_generators_paths
102102
# Load paths from plugin.
103103
#
104104
def self.plugins_generators_paths
105-
return [] unless defined?(RAILS_ROOT)
106-
Dir[File.join(RAILS_ROOT, "vendor", "plugins", "*", "lib", "generators")]
105+
return [] unless Rails.root
106+
Dir[File.join(Rails.root, "vendor", "plugins", "*", "lib", "generators")]
107107
end
108108

109109
# Hold configured generators fallbacks. If a plugin developer wants a
@@ -143,7 +143,7 @@ def self.no_color!
143143
def self.load_paths
144144
@load_paths ||= begin
145145
paths = []
146-
paths << File.join(RAILS_ROOT, "lib", "generators") if defined?(RAILS_ROOT)
146+
paths << File.join(Rails.root, "lib", "generators") if Rails.root
147147
paths << File.join(Thor::Util.user_home, ".rails", "generators")
148148
paths += self.plugins_generators_paths
149149
paths += self.gems_generators_paths

railties/lib/rails/generators/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ def self.inherited(base) #:nodoc:
203203
super
204204
base.source_root # Cache source root
205205

206-
if defined?(RAILS_ROOT) && base.name !~ /Base$/
207-
path = File.expand_path(File.join(RAILS_ROOT, 'lib', 'templates'))
206+
if Rails.root && base.name !~ /Base$/
207+
path = File.expand_path(File.join(Rails.root, 'lib', 'templates'))
208208
if base.name.include?('::')
209209
base.source_paths << File.join(path, base.base_name, base.generator_name)
210210
else

railties/lib/rails/generators/rails/app/templates/config/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# -- all .rb files in that directory are automatically loaded.
88

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

1212
# Specify gems that this application depends on and have them installed with rake gems:install
1313
# config.gem "bj"

railties/lib/rails/generators/rails/app/templates/config/boot.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Don't change this file!
22
# Configure your app in config/environment.rb and config/environments/*.rb
33

4-
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5-
64
module Rails
75
# Mark the version of Rails that generated the boot.rb file. This is
86
# a temporary solution and will most likely be removed as Rails 3.0
@@ -70,7 +68,6 @@ class VendorBoot < Boot
7068
def load_initializer
7169
require "rails"
7270
install_gem_spec_stubs
73-
Rails::GemDependency.add_frozen_gem_path
7471
end
7572

7673
def install_gem_spec_stubs

railties/lib/rails/initializer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require 'rails/paths'
1010
require 'rails/core'
1111
require 'rails/configuration'
12+
require 'rails/deprecation'
1213

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

railties/lib/rails/plugin/loader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def effective_order_of(plugin)
147147
end
148148

149149
def application_lib_index
150-
$LOAD_PATH.index(File.join(RAILS_ROOT, 'lib')) || 0
150+
$LOAD_PATH.index(File.join(Rails.root, 'lib')) || 0
151151
end
152152

153153
def enabled?(plugin)

railties/lib/rails/tasks.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020

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

0 commit comments

Comments
 (0)