Skip to content

Commit

Permalink
Get everyone running on Rails.env and fix the broken environment sett…
Browse files Browse the repository at this point in the history
…ings for script/console and script/dbconsole
  • Loading branch information
dhh committed Jan 11, 2010
1 parent a9eebde commit 8cb594a
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 26 deletions.
4 changes: 1 addition & 3 deletions railties/lib/rails.rb
Expand Up @@ -29,8 +29,6 @@
Encoding.default_external = Encoding::UTF_8
end

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

module Rails
autoload :Bootstrap, 'rails/bootstrap'

Expand Down Expand Up @@ -86,7 +84,7 @@ def root
end

def env
@_env ||= ActiveSupport::StringInquirer.new(RAILS_ENV)
@_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")
end

def cache
Expand Down
7 changes: 6 additions & 1 deletion railties/lib/rails/bootstrap.rb
Expand Up @@ -38,6 +38,11 @@ def initialize(application)
config.load_once_paths.freeze
end

# TODO: Wrap in deprecation warning, everyone should be using Rails.env now
initializer :set_rails_env do
silence_warnings { Object.const_set "RAILS_ENV", Rails.env }
end

# Create tmp directories
initializer :ensure_tmp_directories_exist do
%w(cache pids sessions sockets).each do |dir_to_make|
Expand Down Expand Up @@ -71,7 +76,7 @@ def initialize(application)
begin
logger = ActiveSupport::BufferedLogger.new(config.log_path)
logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase)
if RAILS_ENV == "production"
if Rails.env.production?
logger.auto_flushing = false
end
rescue StandardError => e
Expand Down
11 changes: 5 additions & 6 deletions railties/lib/rails/commands/console.rb
Expand Up @@ -4,8 +4,6 @@

module Rails
class Console
ENVIRONMENTS = %w(production development test)

def self.start(app)
new(app).start
end
Expand All @@ -25,10 +23,6 @@ def start
opt.parse!(ARGV)
end

if env = ARGV.pop
ENV['RAILS_ENV'] = ENVIRONMENTS.find { |e| e.index(env) } || env
end

@app.initialize!
require "rails/console_app"
require "rails/console_sandbox" if options[:sandbox]
Expand All @@ -54,3 +48,8 @@ def start
end
end
end

# Has to set the RAILS_ENV before config/application is required
if ARGV.first && !ARGV.first.index("-") && env = ARGV.pop # has to pop the env ARGV so IRB doesn't freak
ENV['RAILS_ENV'] = %w(production development test).find { |e| e.index(env) } || env
end
6 changes: 5 additions & 1 deletion railties/lib/rails/commands/dbconsole.rb
Expand Up @@ -34,7 +34,6 @@ def start
abort opt.to_s unless (0..1).include?(ARGV.size)
end

env = ARGV.first || ENV['RAILS_ENV'] || 'development'
unless config = YAML::load(ERB.new(IO.read("#{@app.root}/config/database.yml")).result)[env]
abort "No database is configured for the environment '#{env}'"
end
Expand Down Expand Up @@ -97,4 +96,9 @@ def find_cmd(*commands)
end
end
end
end

# Has to set the RAILS_ENV before config/application is required
if ARGV.first && !ARGV.first.index("-") && env = ARGV.first
ENV['RAILS_ENV'] = %w(production development test).find { |e| e.index(env) } || env
end
1 change: 0 additions & 1 deletion railties/lib/rails/commands/runner.rb
Expand Up @@ -34,7 +34,6 @@
ARGV.delete(code_or_file)

ENV["RAILS_ENV"] = options[:environment]
RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV)

begin
if code_or_file.nil?
Expand Down
7 changes: 3 additions & 4 deletions railties/lib/rails/commands/server.rb
Expand Up @@ -38,15 +38,14 @@ def opt_parser
end

def start
ENV["RAILS_ENV"] = options[:environment]

puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}:#{options[:Port]}"
puts "=> Rails #{Rails.version} application starting in #{Rails.env} on http://#{options[:Host]}:#{options[:Port]}"
puts "=> Call with -d to detach" unless options[:daemonize]
trap(:INT) { exit }
puts "=> Ctrl-C to shutdown server" unless options[:daemonize]

ENV["RAILS_ENV"] = options[:environment]
RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV)

super
ensure
puts 'Exiting' unless options[:daemonize]
Expand Down
12 changes: 6 additions & 6 deletions railties/lib/rails/configuration.rb
Expand Up @@ -129,7 +129,7 @@ def paths
paths.tmp.cache "tmp/cache"
paths.config "config"
paths.config.locales "config/locales"
paths.config.environments "config/environments", :glob => "#{RAILS_ENV}.rb"
paths.config.environments "config/environments", :glob => "#{Rails.env}.rb"
paths
end
end
Expand Down Expand Up @@ -212,7 +212,7 @@ def load_paths
paths = []

# Add the old mock paths only if the directories exists
paths.concat(Dir["#{root}/test/mocks/#{RAILS_ENV}"]) if File.exists?("#{root}/test/mocks/#{RAILS_ENV}")
paths.concat(Dir["#{root}/test/mocks/#{Rails.env}"]) if File.exists?("#{root}/test/mocks/#{Rails.env}")

# Add the app's controller directory
paths.concat(Dir["#{root}/app/controllers/"])
Expand All @@ -235,15 +235,15 @@ def load_paths

def builtin_directories
# Include builtins only in the development environment.
(RAILS_ENV == 'development') ? Dir["#{RAILTIES_PATH}/builtin/*/"] : []
Rails.env.development? ? Dir["#{RAILTIES_PATH}/builtin/*/"] : []
end

def log_path
@log_path ||= File.join(root, 'log', "#{RAILS_ENV}.log")
@log_path ||= File.join(root, 'log', "#{Rails.env}.log")
end

def log_level
@log_level ||= RAILS_ENV == 'production' ? :info : :debug
@log_level ||= Rails.env.production? ? :info : :debug
end

def time_zone
Expand All @@ -265,7 +265,7 @@ def i18n
end

def environment_path
"#{root}/config/environments/#{RAILS_ENV}.rb"
"#{root}/config/environments/#{Rails.env}.rb"
end

# Holds generators configuration:
Expand Down
@@ -1,3 +1,5 @@
require File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands/console'
require File.expand_path('../../config/application', __FILE__)

Rails::Console.start(<%= app_const %>.instance)
@@ -1,3 +1,5 @@
require File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands/dbconsole'
require File.expand_path('../../config/application', __FILE__)

Rails::DBConsole.start(<%= app_const %>.instance)
@@ -1,2 +1,3 @@
require File.expand_path('../../config/environment', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands/runner'
require File.expand_path('../../config/environment', __FILE__)
2 changes: 1 addition & 1 deletion railties/lib/rails/test_help.rb
@@ -1,6 +1,6 @@
# Make double-sure the RAILS_ENV is set to test,
# so fixtures are loaded to the right database
silence_warnings { RAILS_ENV = "test" }
exit("Abort testing: Your Rails environment is not running in test mode!") unless Rails.env.test?

require 'test/unit'
require 'active_support/core_ext/kernel/requires'
Expand Down

0 comments on commit 8cb594a

Please sign in to comment.