aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>2009-09-28 17:57:36 -0700
committerYehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>2009-09-28 18:00:26 -0700
commit6f6a589d4b564f5db78735ad0c7225c22ced57ac (patch)
tree402a661bd4dac4b2271d67214fcb8a5cb2530654
parent8ffc2e3b8de38c485fa24820208b40920dad7ae3 (diff)
downloadrails-6f6a589d4b564f5db78735ad0c7225c22ced57ac.tar.gz
rails-6f6a589d4b564f5db78735ad0c7225c22ced57ac.tar.bz2
rails-6f6a589d4b564f5db78735ad0c7225c22ced57ac.zip
Create the application object from config/environment.rb
This is preliminary and not necessarily reflective of the full plan.
-rw-r--r--railties/lib/rails/application.rb12
-rw-r--r--railties/lib/rails/core.rb16
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config.ru5
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/boot.rb4
-rw-r--r--railties/lib/rails/initializer.rb17
-rw-r--r--railties/test/application/load_test.rb51
6 files changed, 46 insertions, 59 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb
index 14336d90c3..cadfc5010b 100644
--- a/railties/lib/rails/application.rb
+++ b/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)
diff --git a/railties/lib/rails/core.rb b/railties/lib/rails/core.rb
index 4c2789746c..4be90de792 100644
--- a/railties/lib/rails/core.rb
+++ b/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 @@ module Rails
@@configuration = configuration
end
- def application
- @@application
- end
-
- def application=(application)
- @@application = application
- end
-
def initialized?
@initialized || false
end
diff --git a/railties/lib/rails/generators/rails/app/templates/config.ru b/railties/lib/rails/generators/rails/app/templates/config.ru
index 541e200193..52a8a8e798 100644
--- a/railties/lib/rails/generators/rails/app/templates/config.ru
+++ b/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')
diff --git a/railties/lib/rails/generators/rails/app/templates/config/boot.rb b/railties/lib/rails/generators/rails/app/templates/config/boot.rb
index 1e09cf08ec..d2652af9b0 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/boot.rb
+++ b/railties/lib/rails/generators/rails/app/templates/config/boot.rb
@@ -46,7 +46,7 @@ module Rails
%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 @@ module Rails
def load_initializer
self.class.load_rubygems
load_rails_gem
- require 'rails/initializer'
+ require 'rails'
end
def load_rails_gem
diff --git a/railties/lib/rails/initializer.rb b/railties/lib/rails/initializer.rb
index f6aca082e8..382838f831 100644
--- a/railties/lib/rails/initializer.rb
+++ b/railties/lib/rails/initializer.rb
@@ -90,7 +90,6 @@ module Rails
def run(initializer = nil)
Rails.configuration = Base.config = @config
- Rails.application = nil
if initializer
run_initializer(initializer)
@@ -98,7 +97,14 @@ module Rails
@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 @@ Run `rake gems:install` to install the missing gems.
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
diff --git a/railties/test/application/load_test.rb b/railties/test/application/load_test.rb
index 47e425a824..f2041d54c2 100644
--- a/railties/test/application/load_test.rb
+++ b/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 @@ module ApplicationTests
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