diff options
author | José Valim <jose.valim@gmail.com> | 2012-05-17 23:45:43 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2012-05-17 23:45:43 -0700 |
commit | a6a8957132f86684fd15fbfa2737350b8a07cc5b (patch) | |
tree | e20508a1ac70ec40be636faec79a20e771e2adc4 | |
parent | 6bb86e1f3f7f9e11ee1c056d8742125173ecbce1 (diff) | |
parent | 793205cccfb3addd8f6aca59d60655b55e4fc38e (diff) | |
download | rails-a6a8957132f86684fd15fbfa2737350b8a07cc5b.tar.gz rails-a6a8957132f86684fd15fbfa2737350b8a07cc5b.tar.bz2 rails-a6a8957132f86684fd15fbfa2737350b8a07cc5b.zip |
Merge pull request #6372 from richo/bugs/middleware_stack_as_array
Add some common Array methods to the middleware stack
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/stack.rb | 5 | ||||
-rw-r--r-- | actionpack/test/dispatch/middleware_stack_test.rb | 9 |
2 files changed, 12 insertions, 2 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index 12bc438be3..bbf734f103 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -75,6 +75,11 @@ module ActionDispatch middlewares[i] end + def unshift(*args, &block) + middleware = self.class::Middleware.new(*args, &block) + middlewares.unshift(middleware) + end + def initialize_copy(other) self.middlewares = other.middlewares.dup end diff --git a/actionpack/test/dispatch/middleware_stack_test.rb b/actionpack/test/dispatch/middleware_stack_test.rb index 4191ed1ff4..948a690979 100644 --- a/actionpack/test/dispatch/middleware_stack_test.rb +++ b/actionpack/test/dispatch/middleware_stack_test.rb @@ -45,7 +45,7 @@ class MiddlewareStackTest < ActiveSupport::TestCase 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 @@ -54,7 +54,7 @@ class MiddlewareStackTest < ActiveSupport::TestCase 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 @@ -87,6 +87,11 @@ class MiddlewareStackTest < ActiveSupport::TestCase assert_equal FooMiddleware, @stack[0].klass end + test "unshift adds a new middleware at the beginning of the stack" do + @stack.unshift :"MiddlewareStackTest::BazMiddleware" + assert_equal BazMiddleware, @stack.first.klass + end + test "raise an error on invalid index" do assert_raise RuntimeError do @stack.insert("HiyaMiddleware", BazMiddleware) |