Skip to content

Commit

Permalink
Backport of: added block arguments to ActionController::Metal#use
Browse files Browse the repository at this point in the history
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
mikel committed Sep 12, 2010
1 parent 14f1b09 commit 91693dd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
6 changes: 3 additions & 3 deletions actionpack/lib/action_controller/metal.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions actionpack/test/controller/new_base/middleware_test.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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"]
Expand Down
17 changes: 16 additions & 1 deletion actionpack/test/dispatch/middleware_stack_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 91693dd

Please sign in to comment.