From 1494262977737a19c9840f5c501aaa62bc24161d Mon Sep 17 00:00:00 2001 From: Anil Wadghule Date: Sun, 16 May 2010 21:23:37 +0530 Subject: [PATCH] Backport middleware stack from Rails 3 to Rails 2-3 [#4367 state:resolved] --- .../lib/action_controller/middleware_stack.rb | 45 +++++++++++++------- 1 files changed, 29 insertions(+), 16 deletions(-) diff --git a/actionpack/lib/action_controller/middleware_stack.rb b/actionpack/lib/action_controller/middleware_stack.rb index b739a6d..e24c431 100644 --- a/actionpack/lib/action_controller/middleware_stack.rb +++ b/actionpack/lib/action_controller/middleware_stack.rb @@ -27,15 +27,13 @@ module ActionController end def klass - if @klass.respond_to?(:call) - @klass.call - elsif @klass.is_a?(Class) + if @klass.respond_to?(:new) @klass + elsif @klass.respond_to?(:call) + @klass.call else - @klass.to_s.constantize + ActiveSupport::Inflector.constantize(@klass.to_s) end - rescue NameError - @klass end def active? @@ -55,14 +53,16 @@ module ActionController when Class klass == middleware else - klass == middleware.to_s.constantize + if lazy_compare?(@klass) && lazy_compare?(middleware) + normalize(@klass) == normalize(middleware) + else + klass.name == middleware.to_s + end end end def inspect - str = klass.to_s - args.each { |arg| str += ", #{arg.inspect}" } - str + klass.to_s end def build(app) @@ -75,9 +75,17 @@ module ActionController private - def build_args - Array(args).map { |arg| arg.respond_to?(:call) ? arg.call : arg } - end + 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 + Array(args).map { |arg| arg.respond_to?(:call) ? arg.call : arg } + end end def initialize(*args, &block) @@ -94,8 +102,9 @@ module ActionController alias_method :insert_before, :insert def insert_after(index, *args, &block) - index = self.index(index) unless index.is_a?(Integer) - insert(index + 1, *args, &block) + i = index.is_a?(Integer) ? index : self.index(index) + raise "No such middleware to insert after: #{index.inspect}" unless i + insert(i + 1, *args, &block) end def swap(target, *args, &block) @@ -112,7 +121,11 @@ module ActionController find_all { |middleware| middleware.active? } end - def build(app) + def build(app = nil, &blk) + app ||= blk + + raise "MiddlewareStack#build requires an app" unless app + active.reverse.inject(app) { |a, e| e.build(a) } end end -- 1.7.0.4