aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-05-30 15:53:14 +0200
committerMikel Lindsaar <raasdnil@gmail.com>2010-06-03 23:32:10 +1000
commitc3be207ee8267e2ce8af0f4e168be35fb1d6e5e8 (patch)
tree54348cbf7a6d5d96f8236dc1a9b69420134997db /actionpack
parentfad29012af65e995a492d0159a50a3fcd98d68f4 (diff)
downloadrails-c3be207ee8267e2ce8af0f4e168be35fb1d6e5e8.tar.gz
rails-c3be207ee8267e2ce8af0f4e168be35fb1d6e5e8.tar.bz2
rails-c3be207ee8267e2ce8af0f4e168be35fb1d6e5e8.zip
Add :only and :except to controllers MiddlewareStack. This allows
you to do the following: class PostsController < ApplicationController use AutheMiddleware, :except => [:index, :show] end
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_controller/metal.rb48
-rw-r--r--actionpack/lib/action_dispatch/middleware/stack.rb8
-rw-r--r--actionpack/test/controller/new_base/middleware_test.rb33
3 files changed, 71 insertions, 18 deletions
diff --git a/actionpack/lib/action_controller/metal.rb b/actionpack/lib/action_controller/metal.rb
index 30aa34d956..775a5002e2 100644
--- a/actionpack/lib/action_controller/metal.rb
+++ b/actionpack/lib/action_controller/metal.rb
@@ -1,6 +1,48 @@
require 'active_support/core_ext/class/attribute'
+require 'active_support/core_ext/object/blank'
+require 'action_dispatch/middleware/stack'
module ActionController
+ # Extend ActionDispatch middleware stack to make it aware of options
+ # allowing the following syntax in controllers:
+ #
+ # class PostsController < ApplicationController
+ # use AuthenticationMiddleware, :except => [:index, :show]
+ # end
+ #
+ class MiddlewareStack < ActionDispatch::MiddlewareStack #:nodoc:
+ class Middleware < ActionDispatch::MiddlewareStack::Middleware #:nodoc:
+ def initialize(klass, *args)
+ options = args.extract_options!
+ @only = Array(options.delete(:only)).map(&:to_s)
+ @except = Array(options.delete(:except)).map(&:to_s)
+ args << options unless options.empty?
+ super
+ end
+
+ def valid?(action)
+ if @only.present?
+ @only.include?(action)
+ elsif @except.present?
+ !@except.include?(action)
+ else
+ true
+ end
+ end
+ end
+
+ def build(action, app=nil, &block)
+ app ||= block
+ action = action.to_s
+ raise "MiddlewareStack#build requires an app" unless app
+
+ reverse.inject(app) do |a, middleware|
+ middleware.valid?(action) ?
+ middleware.build(a) : a
+ end
+ end
+ end
+
# ActionController::Metal provides a way to get a valid Rack application from a controller.
#
# In AbstractController, dispatching is triggered directly by calling #process on a new controller.
@@ -91,10 +133,10 @@ module ActionController
end
class_attribute :middleware_stack
- self.middleware_stack = ActionDispatch::MiddlewareStack.new
+ self.middleware_stack = ActionController::MiddlewareStack.new
def self.inherited(base)
- self.middleware_stack = base.middleware_stack.dup
+ base.middleware_stack = self.middleware_stack.dup
super
end
@@ -120,7 +162,7 @@ module ActionController
# ==== Returns
# Proc:: A rack application
def self.action(name, klass = ActionDispatch::Request)
- middleware_stack.build do |env|
+ middleware_stack.build(name.to_s) do |env|
new.dispatch(name, klass.new(env))
end
end
diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb
index 0e5ab507df..e3180dba77 100644
--- a/actionpack/lib/action_dispatch/middleware/stack.rb
+++ b/actionpack/lib/action_dispatch/middleware/stack.rb
@@ -55,7 +55,7 @@ module ActionDispatch
def insert(index, *args, &block)
index = self.index(index) unless index.is_a?(Integer)
- middleware = Middleware.new(*args, &block)
+ middleware = self.class::Middleware.new(*args, &block)
super(index, middleware)
end
@@ -73,7 +73,7 @@ module ActionDispatch
end
def use(*args, &block)
- middleware = Middleware.new(*args, &block)
+ middleware = self.class::Middleware.new(*args, &block)
push(middleware)
end
@@ -82,8 +82,8 @@ module ActionDispatch
"was removed from the middleware stack", caller
end
- def build(app = nil, &blk)
- app ||= blk
+ def build(app = nil, &block)
+ app ||= block
raise "MiddlewareStack#build requires an app" unless app
reverse.inject(app) { |a, e| e.build(a) }
end
diff --git a/actionpack/test/controller/new_base/middleware_test.rb b/actionpack/test/controller/new_base/middleware_test.rb
index 65942ebc15..26a66c91a6 100644
--- a/actionpack/test/controller/new_base/middleware_test.rb
+++ b/actionpack/test/controller/new_base/middleware_test.rb
@@ -28,7 +28,6 @@ module MiddlewareTest
class MyController < ActionController::Metal
use MyMiddleware
-
middleware.insert_before MyMiddleware, ExclaimerMiddleware
def index
@@ -39,8 +38,23 @@ module MiddlewareTest
class InheritedController < MyController
end
- module MiddlewareTests
- extend ActiveSupport::Testing::Declarative
+ class ActionsController < ActionController::Metal
+ use MyMiddleware, :only => :show
+ middleware.insert_before MyMiddleware, ExclaimerMiddleware, :except => :index
+
+ def index
+ self.response_body = "index"
+ end
+
+ def show
+ self.response_body = "show"
+ end
+ end
+
+ class TestMiddleware < ActiveSupport::TestCase
+ def setup
+ @app = MyController.action(:index)
+ end
test "middleware that is 'use'd is called as part of the Rack application" do
result = @app.call(env_for("/"))
@@ -52,13 +66,13 @@ module MiddlewareTest
result = @app.call(env_for("/"))
assert_equal "First!", result[1]["Middleware-Order"]
end
- end
- class TestMiddleware < ActiveSupport::TestCase
- include MiddlewareTests
+ test "middleware stack accepts only and except as options" do
+ result = ActionsController.action(:show).call(env_for("/"))
+ assert_equal "First!", result[1]["Middleware-Order"]
- def setup
- @app = MyController.action(:index)
+ result = ActionsController.action(:index).call(env_for("/"))
+ assert_nil result[1]["Middleware-Order"]
end
def env_for(url)
@@ -70,8 +84,5 @@ module MiddlewareTest
def setup
@app = InheritedController.action(:index)
end
-
- test "middleware inherits" do
- end
end
end