Skip to content
Browse files

Backport of: added block arguments to ActionController::Metal#use

Commit: f0dbcc7a692bc375e3e52a9661af4037392ee52f
Useful for cases such as warden, where a block configuration is taken.

    class SomeController < ApplicationController
      use RailsWarden::Manager do |manager|
        manager.default_strategies :facebook_oauth
        manager.failure_app = SomeController.action(:authorize)
      end
    end
  • Loading branch information...
1 parent 14f1b09 commit 91693ddfe4ce34cf0d13233130c944f4ff5fe28a @mikel mikel committed Sep 12, 2010
View
6 actionpack/lib/action_controller/metal.rb
@@ -12,7 +12,7 @@ module ActionController
#
class MiddlewareStack < ActionDispatch::MiddlewareStack #:nodoc:
class Middleware < ActionDispatch::MiddlewareStack::Middleware #:nodoc:
- def initialize(klass, *args)
+ def initialize(klass, *args, &block)
options = args.extract_options!
@only = Array(options.delete(:only)).map(&:to_s)
@except = Array(options.delete(:except)).map(&:to_s)
@@ -147,8 +147,8 @@ def self.inherited(base)
super
end
- def self.use(*args)
- middleware_stack.use(*args)
+ def self.use(*args, &block)
+ middleware_stack.use(*args, &block)
end
def self.middleware
View
22 actionpack/test/controller/new_base/middleware_test.rb
@@ -25,8 +25,25 @@ def call(env)
result
end
end
+
+ class BlockMiddleware
+ attr_accessor :configurable_message
+ def initialize(app, &block)
+ @app = app
+ yield(self) if block_given?
+ end
+
+ def call(env)
+ result = @app.call(env)
+ result[1]["Configurable-Message"] = configurable_message
+ result
+ end
+ end
class MyController < ActionController::Metal
+ use BlockMiddleware do |config|
+ config.configurable_message = "Configured by block."
+ end
use MyMiddleware
middleware.insert_before MyMiddleware, ExclaimerMiddleware
@@ -67,6 +84,11 @@ def setup
assert_equal "First!", result[1]["Middleware-Order"]
end
+ test "middleware stack accepts block arguments" do
+ result = @app.call(env_for("/"))
+ assert_equal "Configured by block.", result[1]["Configurable-Message"]
+ end
+
test "middleware stack accepts only and except as options" do
result = ActionsController.action(:show).call(env_for("/"))
assert_equal "First!", result[1]["Middleware-Order"]
View
17 actionpack/test/dispatch/middleware_stack_test.rb
@@ -4,6 +4,12 @@ class MiddlewareStackTest < ActiveSupport::TestCase
class FooMiddleware; end
class BarMiddleware; end
class BazMiddleware; end
+ class BlockMiddleware
+ attr_reader :block
+ def initialize(&block)
+ @block = block
+ end
+ end
def setup
@stack = ActionDispatch::MiddlewareStack.new
@@ -39,7 +45,16 @@ def setup
assert_equal BazMiddleware, @stack.last.klass
assert_equal([true, {:foo => "bar"}], @stack.last.args)
end
-
+
+ test "use should push middleware class with block arguments onto the stack" do
+ proc = Proc.new {}
+ assert_difference "@stack.size" do
+ @stack.use(BlockMiddleware, &proc)
+ end
+ assert_equal BlockMiddleware, @stack.last.klass
+ assert_equal proc, @stack.last.block
+ end
+
test "insert inserts middleware at the integer index" do
@stack.insert(1, BazMiddleware)
assert_equal BazMiddleware, @stack[1].klass

0 comments on commit 91693dd

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