提交 a6757a02 编写于 作者: J Joshua Peek

Move middleware and route configuration from AC::Dispatcher to Rails application object

上级 14866fa3
......@@ -7,12 +7,6 @@ class Dispatcher
cattr_accessor :prepare_each_request
self.prepare_each_request = false
cattr_accessor :router
self.router = Routing::Routes
cattr_accessor :middleware
self.middleware = ActionDispatch::MiddlewareStack.new
class << self
def define_dispatcher_callbacks(cache_classes)
unless cache_classes
......@@ -21,7 +15,7 @@ def define_dispatcher_callbacks(cache_classes)
# Development mode callbacks
ActionDispatch::Callbacks.before_dispatch do |app|
ActionController::Dispatcher.router.reload
ActionController::Routing::Routes.reload
end
ActionDispatch::Callbacks.after_dispatch do
......@@ -55,7 +49,8 @@ def define_dispatcher_callbacks(cache_classes)
:to => ActionDispatch::Callbacks
def new
@@middleware.build(@@router)
# DEPRECATE Rails application fallback
Rails.application
end
end
end
......
......@@ -99,7 +99,6 @@ def with_routing
temporary_routes = ActionController::Routing::RouteSet.new
ActionController::Routing.module_eval { const_set :Routes, temporary_routes }
ActionController::Dispatcher.router = temporary_routes
yield temporary_routes
ensure
......@@ -107,7 +106,6 @@ def with_routing
ActionController::Routing.module_eval { remove_const :Routes }
end
ActionController::Routing.const_set(:Routes, real_routes) if real_routes
ActionController::Dispatcher.router = ActionController::Routing::Routes
end
end
end
......@@ -484,8 +484,9 @@ class IntegrationTest < ActiveSupport::TestCase
@@app = nil
def self.app
# DEPRECATE AC::Dispatcher fallback
@@app || ActionController::Dispatcher.new
# DEPRECATE Rails application fallback
# This should be set by the initializer
@@app || (defined?(Rails.application) && Rails.application) || nil
end
def self.app=(app)
......
......@@ -14,14 +14,12 @@ def setup
ActionDispatch::Callbacks.reset_callbacks(:prepare)
ActionDispatch::Callbacks.reset_callbacks(:call)
@old_router, Dispatcher.router = Dispatcher.router, mock()
Dispatcher.router.stubs(:call).returns([200, {}, 'response'])
Dispatcher.router.stubs(:reload)
ActionController::Routing::Routes.stubs(:call).returns([200, {}, 'response'])
ActionController::Routing::Routes.stubs(:reload)
Dispatcher.stubs(:require_dependency)
end
def teardown
Dispatcher.router = @old_router
ENV.delete 'REQUEST_METHOD'
end
......@@ -31,12 +29,12 @@ def test_clears_dependencies_after_dispatch_if_in_loading_mode
end
def test_reloads_routes_before_dispatch_if_in_loading_mode
Dispatcher.router.expects(:reload).once
ActionController::Routing::Routes.expects(:reload).once
dispatch(false)
end
def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
Dispatcher.router.expects(:reload).never
ActionController::Routing::Routes.expects(:reload).never
ActiveSupport::Dependencies.expects(:clear).never
dispatch
......@@ -78,7 +76,7 @@ def dispatch(cache_classes = true)
ActionController::Dispatcher.prepare_each_request = false
Dispatcher.define_dispatcher_callbacks(cache_classes)
@dispatcher ||= ActionDispatch::Callbacks.new(Dispatcher.router)
@dispatcher ||= ActionDispatch::Callbacks.new(ActionController::Routing::Routes)
@dispatcher.call({'rack.input' => StringIO.new(''), 'action_dispatch.show_exceptions' => false})
end
......
module Rails
class Application
attr_accessor :middleware, :routes
def initialize
@app = ActionController::Dispatcher.new
@middleware = ActionDispatch::MiddlewareStack.new
@routes = ActionController::Routing::Routes
end
def call(env)
@app ||= middleware.build(@routes)
@app.call(env)
end
end
......
......@@ -27,6 +27,6 @@ def new_session
def reload!
puts "Reloading..."
ActionDispatch::Callbacks.new(lambda {}, true)
ActionController::Dispatcher.router.reload
ActionController::Routing::Routes.reload
true
end
......@@ -608,8 +608,8 @@ def self.run(initializer = nil, config = nil)
Initializer.default.add :build_application do
if configuration.frameworks.include?(:action_controller)
ActionController::Dispatcher.middleware = configuration.middleware
Rails.application = Rails::Application.new
Rails.application.middleware = configuration.middleware
end
end
end
......@@ -5,6 +5,12 @@
require 'rails/dispatcher'
require 'rails/console_app'
module Rails
def self.application
ActionController::Routing::Routes
end
end
# console_app sets Test::Unit.run to work around the at_exit hook in test/unit, which kills IRB
if Test::Unit.respond_to?(:run=)
Test::Unit.run = false
......
......@@ -5,6 +5,12 @@
require 'action_controller'
require 'rails/fcgi_handler'
module Rails
def self.application
ActionController::Routing::Routes
end
end
class RailsFCGIHandlerTest < Test::Unit::TestCase
def setup
@log = StringIO.new
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册