diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index 41672325bd..c21434a100 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -29,8 +29,6 @@ else 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' @@ -86,7 +84,7 @@ module Rails end def env - @_env ||= ActiveSupport::StringInquirer.new(RAILS_ENV) + @_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development") end def cache diff --git a/railties/lib/rails/bootstrap.rb b/railties/lib/rails/bootstrap.rb index 15d5b2b59d..3808695f10 100644 --- a/railties/lib/rails/bootstrap.rb +++ b/railties/lib/rails/bootstrap.rb @@ -38,6 +38,11 @@ module Rails 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| @@ -71,7 +76,7 @@ module Rails 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 diff --git a/railties/lib/rails/commands/console.rb b/railties/lib/rails/commands/console.rb index d154808029..27ac7fd20a 100644 --- a/railties/lib/rails/commands/console.rb +++ b/railties/lib/rails/commands/console.rb @@ -4,8 +4,6 @@ require "irb/completion" module Rails class Console - ENVIRONMENTS = %w(production development test) - def self.start(app) new(app).start end @@ -25,10 +23,6 @@ module Rails 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] @@ -54,3 +48,8 @@ module Rails 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 diff --git a/railties/lib/rails/commands/dbconsole.rb b/railties/lib/rails/commands/dbconsole.rb index 77c3404343..b836a17a49 100644 --- a/railties/lib/rails/commands/dbconsole.rb +++ b/railties/lib/rails/commands/dbconsole.rb @@ -34,7 +34,6 @@ module Rails 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 @@ -97,4 +96,9 @@ module Rails 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 \ No newline at end of file diff --git a/railties/lib/rails/commands/runner.rb b/railties/lib/rails/commands/runner.rb index 0246348c77..4487d2e7b1 100644 --- a/railties/lib/rails/commands/runner.rb +++ b/railties/lib/rails/commands/runner.rb @@ -34,7 +34,6 @@ end 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? diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb index 09d7207d51..b21ae2a17b 100644 --- a/railties/lib/rails/commands/server.rb +++ b/railties/lib/rails/commands/server.rb @@ -38,15 +38,14 @@ module Rails 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] diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 800a8215b8..08f5dda8ee 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -129,7 +129,7 @@ module Rails 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 @@ -212,7 +212,7 @@ module Rails 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/"]) @@ -235,15 +235,15 @@ module Rails 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 @@ -265,7 +265,7 @@ module Rails end def environment_path - "#{root}/config/environments/#{RAILS_ENV}.rb" + "#{root}/config/environments/#{Rails.env}.rb" end # Holds generators configuration: diff --git a/railties/lib/rails/generators/rails/app/templates/script/console.tt b/railties/lib/rails/generators/rails/app/templates/script/console.tt index 9ddd4cfe62..daba8ba2f1 100755 --- a/railties/lib/rails/generators/rails/app/templates/script/console.tt +++ b/railties/lib/rails/generators/rails/app/templates/script/console.tt @@ -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) diff --git a/railties/lib/rails/generators/rails/app/templates/script/dbconsole.tt b/railties/lib/rails/generators/rails/app/templates/script/dbconsole.tt index 96e0bc191b..a7f114a97f 100755 --- a/railties/lib/rails/generators/rails/app/templates/script/dbconsole.tt +++ b/railties/lib/rails/generators/rails/app/templates/script/dbconsole.tt @@ -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) diff --git a/railties/lib/rails/generators/rails/app/templates/script/runner b/railties/lib/rails/generators/rails/app/templates/script/runner index 7a70828e90..3354ed4a28 100755 --- a/railties/lib/rails/generators/rails/app/templates/script/runner +++ b/railties/lib/rails/generators/rails/app/templates/script/runner @@ -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__) diff --git a/railties/lib/rails/test_help.rb b/railties/lib/rails/test_help.rb index e76c476bfb..350d0b3961 100644 --- a/railties/lib/rails/test_help.rb +++ b/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'