提交 02908e11 编写于 作者: J José Valim

As first step setup the load path and lazy compare middlewares.

上级 1177a40e
...@@ -55,7 +55,11 @@ def ==(middleware) ...@@ -55,7 +55,11 @@ def ==(middleware)
when Class when Class
klass == middleware klass == middleware
else else
klass == ActiveSupport::Inflector.constantize(middleware.to_s) if lazy_compare?(@klass) && lazy_compare?(middleware)
normalize(@klass) == normalize(middleware)
else
klass == ActiveSupport::Inflector.constantize(middleware.to_s)
end
end end
end end
...@@ -72,6 +76,14 @@ def build(app) ...@@ -72,6 +76,14 @@ def build(app)
end end
private private
def lazy_compare?(object)
object.is_a?(String) || object.is_a?(Symbol)
end
def normalize(object)
object.to_s.strip.sub(/^::/, '')
end
def build_args def build_args
Array(args).map { |arg| arg.respond_to?(:call) ? arg.call : arg } Array(args).map { |arg| arg.respond_to?(:call) ? arg.call : arg }
end end
......
...@@ -87,4 +87,10 @@ def setup ...@@ -87,4 +87,10 @@ def setup
end end
assert_equal [:foo], @stack.last.send(:build_args) assert_equal [:foo], @stack.last.send(:build_args)
end end
test "lazy compares so unloaded constants can be loaded" do
@stack.use "UnknownMiddleware"
@stack.use :"MiddlewareStackTest::BazMiddleware"
assert @stack.include?("::MiddlewareStackTest::BazMiddleware")
end
end end
...@@ -49,9 +49,9 @@ class Railtie < Rails::Railtie ...@@ -49,9 +49,9 @@ class Railtie < Rails::Railtie
# Setup database middleware after initializers have run # Setup database middleware after initializers have run
initializer "active_record.initialize_database_middleware" do |app| initializer "active_record.initialize_database_middleware" do |app|
middleware = app.config.middleware middleware = app.config.middleware
if middleware.include?(ActiveRecord::SessionStore) if middleware.include?("ActiveRecord::SessionStore")
middleware.insert_before ActiveRecord::SessionStore, ActiveRecord::ConnectionAdapters::ConnectionManagement middleware.insert_before "ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement
middleware.insert_before ActiveRecord::SessionStore, ActiveRecord::QueryCache middleware.insert_before "ActiveRecord::SessionStore", ActiveRecord::QueryCache
else else
middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement
middleware.use ActiveRecord::QueryCache middleware.use ActiveRecord::QueryCache
......
...@@ -76,6 +76,10 @@ module Bootstrap ...@@ -76,6 +76,10 @@ module Bootstrap
initializer :initialize_dependency_mechanism do |app| initializer :initialize_dependency_mechanism do |app|
ActiveSupport::Dependencies.mechanism = app.config.cache_classes ? :require : :load ActiveSupport::Dependencies.mechanism = app.config.cache_classes ? :require : :load
end end
initializer :bootstrap_load_path do
# This is just an initializer used as hook so all load paths are loaded together
end
end end
end end
end end
\ No newline at end of file
...@@ -47,7 +47,7 @@ def load_tasks ...@@ -47,7 +47,7 @@ def load_tasks
end end
# Add configured load paths to ruby load paths and remove duplicates. # Add configured load paths to ruby load paths and remove duplicates.
initializer :set_load_path do initializer :set_load_path, :before => :bootstrap_load_path do
config.load_paths.reverse_each do |path| config.load_paths.reverse_each do |path|
$LOAD_PATH.unshift(path) if File.directory?(path) $LOAD_PATH.unshift(path) if File.directory?(path)
end end
...@@ -56,7 +56,7 @@ def load_tasks ...@@ -56,7 +56,7 @@ def load_tasks
# Set the paths from which Rails will automatically load source files, # Set the paths from which Rails will automatically load source files,
# and the load_once paths. # and the load_once paths.
initializer :set_autoload_paths do |app| initializer :set_autoload_paths, :before => :bootstrap_load_path do |app|
ActiveSupport::Dependencies.load_paths.unshift(*config.load_paths) ActiveSupport::Dependencies.load_paths.unshift(*config.load_paths)
if reloadable?(app) if reloadable?(app)
......
...@@ -48,6 +48,7 @@ def app ...@@ -48,6 +48,7 @@ def app
RUBY RUBY
boot_rails boot_rails
assert $foo
end end
test "plugin paths get added to the AS::Dependency list" do test "plugin paths get added to the AS::Dependency list" do
...@@ -252,26 +253,6 @@ def self.call(env) ...@@ -252,26 +253,6 @@ def self.call(env)
assert_equal "FooMetal", last_response.body assert_equal "FooMetal", last_response.body
end end
test "use plugin middleware in application config" do
plugin "foo" do |plugin|
plugin.write "lib/foo.rb", <<-RUBY
class Foo
def initialize(app)
@app = app
end
def call(env)
@app.call(env)
end
end
RUBY
end
add_to_config "config.middleware.use :Foo"
boot_rails
end
test "namespaced controllers with namespaced routes" do test "namespaced controllers with namespaced routes" do
@plugin.write "config/routes.rb", <<-RUBY @plugin.write "config/routes.rb", <<-RUBY
ActionController::Routing::Routes.draw do ActionController::Routing::Routes.draw do
...@@ -332,6 +313,23 @@ class Engine < Rails::Engine ...@@ -332,6 +313,23 @@ class Engine < Rails::Engine
assert rescued, "Expected boot rails to fail" assert rescued, "Expected boot rails to fail"
end end
test "use plugin middleware in application config" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
def initialize(app)
@app = app
end
def call(env)
@app.call(env)
end
end
RUBY
add_to_config "config.middleware.use \"Bukkits\""
boot_rails
end
end end
class VendoredOrderingTest < Test::Unit::TestCase class VendoredOrderingTest < Test::Unit::TestCase
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册