Skip to content
Browse files

Create the application object from config/environment.rb

  This is preliminary and not necessarily reflective of the
  full plan.
  • Loading branch information...
1 parent 8ffc2e3 commit 6f6a589d4b564f5db78735ad0c7225c22ced57ac Yehuda Katz + Carl Lerche committed Sep 28, 2009
View
12 railties/lib/rails/application.rb
@@ -1,12 +1,20 @@
module Rails
class Application
- attr_accessor :middleware, :routes
+ attr_accessor :routes, :config
+
+ def self.load(environment_file)
+ environment = File.read(environment_file)
+ Object.class_eval(environment, environment_file)
+ end
def initialize
- @middleware = ActionDispatch::MiddlewareStack.new
@routes = ActionController::Routing::Routes
end
+ def middleware
+ config.middleware
+ end
+
def call(env)
@app ||= middleware.build(@routes)
@app.call(env)
View
16 railties/lib/rails/core.rb
@@ -5,6 +5,14 @@ module Rails
# ---
# TODO: w0t?
class << self
+ def application
+ @@application
+ end
+
+ def application=(application)
+ @@application = application
+ end
+
# The Configuration instance used to configure the Rails environment
def configuration
@@configuration
@@ -14,14 +22,6 @@ def configuration=(configuration)
@@configuration = configuration
end
- def application
- @@application
- end
-
- def application=(application)
- @@application = application
- end
-
def initialized?
@initialized || false
end
View
5 railties/lib/rails/generators/rails/app/templates/config.ru
@@ -1,7 +1,8 @@
# Rack Dispatcher
+# Bootstrap rails
+require ::File.dirname(__FILE__) + '/config/boot'
# Require your environment file to bootstrap Rails
-require ::File.dirname(__FILE__) + '/config/environment'
# Dispatch the request
-run ActionController::Dispatcher.new
+run Rails::Application.load(::File.dirname(__FILE__) + '/config/environment.rb')
View
4 railties/lib/rails/generators/rails/app/templates/config/boot.rb
@@ -46,7 +46,7 @@ def load_initializer
%w(railties/lib).each do |path|
$:.unshift("#{RAILS_ROOT}/vendor/rails/#{path}")
end
- require "rails/initializer"
+ require "rails"
Rails::Initializer.run(:install_gem_spec_stubs)
Rails::GemDependency.add_frozen_gem_path
end
@@ -56,7 +56,7 @@ class GemBoot < Boot
def load_initializer
self.class.load_rubygems
load_rails_gem
- require 'rails/initializer'
+ require 'rails'
end
def load_rails_gem
View
17 railties/lib/rails/initializer.rb
@@ -90,15 +90,21 @@ def run_initializer(initializer)
def run(initializer = nil)
Rails.configuration = Base.config = @config
- Rails.application = nil
if initializer
run_initializer(initializer)
else
@initializers.each {|block| run_initializer(block) }
end
- Rails.application
+ # HAX
+ # TODO: remove hax
+ unless initializer
+ app = Rails::Application.new
+ app.config = @config
+
+ Rails.application = app
+ end
end
end
@@ -605,11 +611,4 @@ def self.run(initializer = nil, config = nil)
Rails::Generators.options.deep_merge! config.generators.options
end
end
-
- Initializer.default.add :build_application do
- if configuration.frameworks.include?(:action_controller)
- Rails.application = Rails::Application.new
- Rails.application.middleware = configuration.middleware
- end
- end
end
View
51 railties/test/application/load_test.rb
@@ -6,6 +6,10 @@ module ApplicationTests
class LoadTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
+ def rackup
+ ActionDispatch::Utils.parse_config("#{app_path}/config.ru")
+ end
+
def setup
build_app
end
@@ -14,48 +18,23 @@ def setup
assert File.exist?(app_path("config"))
end
- test "running Rails::Application.load on the path returns a (vaguely) useful application" do
- app_file "config.ru", <<-CONFIG
- require File.dirname(__FILE__) + '/config/environment'
- run ActionController::Dispatcher.new
- CONFIG
-
- @app = ActionDispatch::Utils.parse_config("#{app_path}/config.ru")
+ test "config.ru can be racked up" do
+ @app = rackup
assert_welcome get("/")
end
- test "config.ru is used" do
- app_file "config.ru", <<-CONFIG
- class MyMiddleware
- def initialize(app)
- @app = app
- end
-
- def call(env)
- status, headers, body = @app.call(env)
- headers["Config-Ru-Test"] = "TESTING"
- [status, headers, body]
- end
- end
-
- use MyMiddleware
- run proc {|env| [200, {"Content-Type" => "text/html"}, ["VICTORY"]] }
- CONFIG
-
- @app = ActionDispatch::Utils.parse_config("#{app_path}/config.ru")
-
- assert_body "VICTORY", get("/omg")
- assert_header "Config-Ru-Test", "TESTING", get("/omg")
+ test "Rails.application is available after config.ru has been racked up" do
+ rackup
+ assert Rails.application.is_a?(Rails::Application)
end
- test "arbitrary.rb can be used as a config" do
- app_file "myapp.rb", <<-CONFIG
- Myapp = proc {|env| [200, {"Content-Type" => "text/html"}, ["OMG ROBOTS"]] }
- CONFIG
-
- @app = ActionDispatch::Utils.parse_config("#{app_path}/myapp.rb")
+ test "class_evaling config/environment.rb returns the application object" do
+ assert Rails::Application.load("#{app_path}/config/environment.rb").is_a?(Rails::Application)
+ end
- assert_body "OMG ROBOTS", get("/omg")
+ test "the config object is available on the application object" do
+ rackup
+ assert_equal 'UTC', Rails.application.config.time_zone
end
end
end

0 comments on commit 6f6a589

Please sign in to comment.
Something went wrong with that request. Please try again.