mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
finish deprecating handling strings and symbols
since we only work with instances of classes, it greatly simplifies the `Middleware` implementation.
This commit is contained in:
parent
83b767cef9
commit
2a3c47ff5d
6 changed files with 24 additions and 80 deletions
|
@ -14,17 +14,6 @@ module ActionDispatch
|
||||||
|
|
||||||
def name; klass.name; end
|
def name; klass.name; end
|
||||||
|
|
||||||
def ==(middleware)
|
|
||||||
case middleware
|
|
||||||
when Middleware
|
|
||||||
klass == middleware.klass
|
|
||||||
when Class
|
|
||||||
klass == middleware
|
|
||||||
else
|
|
||||||
normalize(name) == normalize(middleware)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
klass.to_s
|
klass.to_s
|
||||||
end
|
end
|
||||||
|
@ -32,12 +21,6 @@ module ActionDispatch
|
||||||
def build(app)
|
def build(app)
|
||||||
klass.new(app, *args, &block)
|
klass.new(app, *args, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def normalize(object)
|
|
||||||
object.to_s.strip.sub(/^::/, '')
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
@ -92,7 +75,8 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(target)
|
def delete(target)
|
||||||
middlewares.delete target
|
target = get_class target
|
||||||
|
middlewares.delete_if { |m| m.klass == target }
|
||||||
end
|
end
|
||||||
|
|
||||||
def use(klass, *args, &block)
|
def use(klass, *args, &block)
|
||||||
|
@ -106,7 +90,8 @@ module ActionDispatch
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def assert_index(index, where)
|
def assert_index(index, where)
|
||||||
i = index.is_a?(Integer) ? index : middlewares.index(index)
|
index = get_class index
|
||||||
|
i = index.is_a?(Integer) ? index : middlewares.index { |m| m.klass == index }
|
||||||
raise "No such middleware to insert #{where}: #{index.inspect}" unless i
|
raise "No such middleware to insert #{where}: #{index.inspect}" unless i
|
||||||
i
|
i
|
||||||
end
|
end
|
||||||
|
|
|
@ -329,7 +329,7 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest
|
||||||
@app = self.class.build_app(set) do |middleware|
|
@app = self.class.build_app(set) do |middleware|
|
||||||
middleware.use ActionDispatch::Session::CookieStore, :key => SessionKey
|
middleware.use ActionDispatch::Session::CookieStore, :key => SessionKey
|
||||||
middleware.use ActionDispatch::Flash
|
middleware.use ActionDispatch::Flash
|
||||||
middleware.delete "ActionDispatch::ShowExceptions"
|
middleware.delete ActionDispatch::ShowExceptions
|
||||||
end
|
end
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
|
@ -1,56 +0,0 @@
|
||||||
require 'abstract_unit'
|
|
||||||
require 'action_dispatch/middleware/stack'
|
|
||||||
|
|
||||||
module ActionDispatch
|
|
||||||
class MiddlewareStack
|
|
||||||
class MiddlewareTest < ActiveSupport::TestCase
|
|
||||||
class Omg; end
|
|
||||||
|
|
||||||
{
|
|
||||||
'concrete' => Omg,
|
|
||||||
'anonymous' => Class.new
|
|
||||||
}.each do |name, klass|
|
|
||||||
|
|
||||||
define_method("test_#{name}_klass") do
|
|
||||||
stack = ActionDispatch::MiddlewareStack.new
|
|
||||||
stack.use klass
|
|
||||||
assert_equal klass, stack.first.klass
|
|
||||||
end
|
|
||||||
|
|
||||||
define_method("test_#{name}_==") do
|
|
||||||
stack = ActionDispatch::MiddlewareStack.new
|
|
||||||
stack.use klass
|
|
||||||
stack.use klass
|
|
||||||
assert_equal 2, stack.size
|
|
||||||
assert_equal stack.first, stack.last
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
attr_reader :stack
|
|
||||||
|
|
||||||
def setup
|
|
||||||
@stack = ActionDispatch::MiddlewareStack.new
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_double_equal_works_with_classes
|
|
||||||
k = Class.new
|
|
||||||
stack.use k
|
|
||||||
assert_operator stack.first, :==, k
|
|
||||||
|
|
||||||
result = stack.first != Class.new
|
|
||||||
assert result, 'middleware should not equal other anon class'
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_double_equal_works_with_strings
|
|
||||||
stack.use Omg
|
|
||||||
assert_operator stack.first, :==, Omg.name
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_double_equal_normalizes_strings
|
|
||||||
stack.use Omg
|
|
||||||
assert_operator stack.first, :==, "::#{Omg.name}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -4,6 +4,7 @@ class MiddlewareStackTest < ActiveSupport::TestCase
|
||||||
class FooMiddleware; end
|
class FooMiddleware; end
|
||||||
class BarMiddleware; end
|
class BarMiddleware; end
|
||||||
class BazMiddleware; end
|
class BazMiddleware; end
|
||||||
|
class HiyaMiddleware; end
|
||||||
class BlockMiddleware
|
class BlockMiddleware
|
||||||
attr_reader :block
|
attr_reader :block
|
||||||
def initialize(&block)
|
def initialize(&block)
|
||||||
|
@ -17,6 +18,20 @@ class MiddlewareStackTest < ActiveSupport::TestCase
|
||||||
@stack.use BarMiddleware
|
@stack.use BarMiddleware
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_delete_with_string_is_deprecated
|
||||||
|
assert_deprecated do
|
||||||
|
assert_difference "@stack.size", -1 do
|
||||||
|
@stack.delete FooMiddleware.name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_delete_works
|
||||||
|
assert_difference "@stack.size", -1 do
|
||||||
|
@stack.delete FooMiddleware
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
test "use should push middleware as class onto the stack" do
|
test "use should push middleware as class onto the stack" do
|
||||||
assert_difference "@stack.size" do
|
assert_difference "@stack.size" do
|
||||||
@stack.use BazMiddleware
|
@stack.use BazMiddleware
|
||||||
|
@ -100,11 +115,11 @@ class MiddlewareStackTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
test "raise an error on invalid index" do
|
test "raise an error on invalid index" do
|
||||||
assert_raise RuntimeError do
|
assert_raise RuntimeError do
|
||||||
@stack.insert("HiyaMiddleware", BazMiddleware)
|
@stack.insert(HiyaMiddleware, BazMiddleware)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_raise RuntimeError do
|
assert_raise RuntimeError do
|
||||||
@stack.insert_after("HiyaMiddleware", BazMiddleware)
|
@stack.insert_after(HiyaMiddleware, BazMiddleware)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -170,7 +170,7 @@ class CacheStoreTest < ActionDispatch::IntegrationTest
|
||||||
@app = self.class.build_app(set) do |middleware|
|
@app = self.class.build_app(set) do |middleware|
|
||||||
@cache = ActiveSupport::Cache::MemoryStore.new
|
@cache = ActiveSupport::Cache::MemoryStore.new
|
||||||
middleware.use ActionDispatch::Session::CacheStore, :key => '_session_id', :cache => @cache
|
middleware.use ActionDispatch::Session::CacheStore, :key => '_session_id', :cache => @cache
|
||||||
middleware.delete "ActionDispatch::ShowExceptions"
|
middleware.delete ActionDispatch::ShowExceptions
|
||||||
end
|
end
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
|
@ -348,7 +348,7 @@ class CookieStoreTest < ActionDispatch::IntegrationTest
|
||||||
|
|
||||||
@app = self.class.build_app(set) do |middleware|
|
@app = self.class.build_app(set) do |middleware|
|
||||||
middleware.use ActionDispatch::Session::CookieStore, options
|
middleware.use ActionDispatch::Session::CookieStore, options
|
||||||
middleware.delete "ActionDispatch::ShowExceptions"
|
middleware.delete ActionDispatch::ShowExceptions
|
||||||
end
|
end
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
Loading…
Reference in a new issue