From 2a3c47ff5d4ed7391d8a2d02a6baa1f1f55a8df3 Mon Sep 17 00:00:00 2001
From: Aaron Patterson <aaron.patterson@gmail.com>
Date: Fri, 7 Aug 2015 15:35:39 -0700
Subject: finish deprecating handling strings and symbols

since we only work with instances of classes, it greatly simplifies the
`Middleware` implementation.
---
 actionpack/lib/action_dispatch/middleware/stack.rb | 23 ++-------
 actionpack/test/controller/flash_test.rb           |  2 +-
 .../dispatch/middleware_stack/middleware_test.rb   | 56 ----------------------
 actionpack/test/dispatch/middleware_stack_test.rb  | 19 +++++++-
 .../test/dispatch/session/cache_store_test.rb      |  2 +-
 .../test/dispatch/session/cookie_store_test.rb     |  2 +-
 6 files changed, 24 insertions(+), 80 deletions(-)
 delete mode 100644 actionpack/test/dispatch/middleware_stack/middleware_test.rb

(limited to 'actionpack')

diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb
index 62e87a6f29..0430ce3b9a 100644
--- a/actionpack/lib/action_dispatch/middleware/stack.rb
+++ b/actionpack/lib/action_dispatch/middleware/stack.rb
@@ -14,17 +14,6 @@ module ActionDispatch
 
       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
         klass.to_s
       end
@@ -32,12 +21,6 @@ module ActionDispatch
       def build(app)
         klass.new(app, *args, &block)
       end
-
-    private
-
-      def normalize(object)
-        object.to_s.strip.sub(/^::/, '')
-      end
     end
 
     include Enumerable
@@ -92,7 +75,8 @@ module ActionDispatch
     end
 
     def delete(target)
-      middlewares.delete target
+      target = get_class target
+      middlewares.delete_if { |m| m.klass == target }
     end
 
     def use(klass, *args, &block)
@@ -106,7 +90,8 @@ module ActionDispatch
   protected
 
     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
       i
     end
diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb
index 64543f0659..b063d769a4 100644
--- a/actionpack/test/controller/flash_test.rb
+++ b/actionpack/test/controller/flash_test.rb
@@ -329,7 +329,7 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest
         @app = self.class.build_app(set) do |middleware|
           middleware.use ActionDispatch::Session::CookieStore, :key => SessionKey
           middleware.use ActionDispatch::Flash
-          middleware.delete "ActionDispatch::ShowExceptions"
+          middleware.delete ActionDispatch::ShowExceptions
         end
 
         yield
diff --git a/actionpack/test/dispatch/middleware_stack/middleware_test.rb b/actionpack/test/dispatch/middleware_stack/middleware_test.rb
deleted file mode 100644
index 28f9fa317b..0000000000
--- a/actionpack/test/dispatch/middleware_stack/middleware_test.rb
+++ /dev/null
@@ -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
diff --git a/actionpack/test/dispatch/middleware_stack_test.rb b/actionpack/test/dispatch/middleware_stack_test.rb
index 354e95cf30..33aa616474 100644
--- a/actionpack/test/dispatch/middleware_stack_test.rb
+++ b/actionpack/test/dispatch/middleware_stack_test.rb
@@ -4,6 +4,7 @@ class MiddlewareStackTest < ActiveSupport::TestCase
   class FooMiddleware; end
   class BarMiddleware; end
   class BazMiddleware; end
+  class HiyaMiddleware; end
   class BlockMiddleware
     attr_reader :block
     def initialize(&block)
@@ -17,6 +18,20 @@ class MiddlewareStackTest < ActiveSupport::TestCase
     @stack.use BarMiddleware
   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
     assert_difference "@stack.size" do
       @stack.use BazMiddleware
@@ -100,11 +115,11 @@ class MiddlewareStackTest < ActiveSupport::TestCase
 
   test "raise an error on invalid index" do
     assert_raise RuntimeError do
-      @stack.insert("HiyaMiddleware", BazMiddleware)
+      @stack.insert(HiyaMiddleware, BazMiddleware)
     end
 
     assert_raise RuntimeError do
-      @stack.insert_after("HiyaMiddleware", BazMiddleware)
+      @stack.insert_after(HiyaMiddleware, BazMiddleware)
     end
   end
 
diff --git a/actionpack/test/dispatch/session/cache_store_test.rb b/actionpack/test/dispatch/session/cache_store_test.rb
index e6a70358c8..dbb996973d 100644
--- a/actionpack/test/dispatch/session/cache_store_test.rb
+++ b/actionpack/test/dispatch/session/cache_store_test.rb
@@ -170,7 +170,7 @@ class CacheStoreTest < ActionDispatch::IntegrationTest
         @app = self.class.build_app(set) do |middleware|
           @cache = ActiveSupport::Cache::MemoryStore.new
           middleware.use ActionDispatch::Session::CacheStore, :key => '_session_id', :cache => @cache
-          middleware.delete "ActionDispatch::ShowExceptions"
+          middleware.delete ActionDispatch::ShowExceptions
         end
 
         yield
diff --git a/actionpack/test/dispatch/session/cookie_store_test.rb b/actionpack/test/dispatch/session/cookie_store_test.rb
index 715eb90566..e432c65c62 100644
--- a/actionpack/test/dispatch/session/cookie_store_test.rb
+++ b/actionpack/test/dispatch/session/cookie_store_test.rb
@@ -348,7 +348,7 @@ class CookieStoreTest < ActionDispatch::IntegrationTest
 
         @app = self.class.build_app(set) do |middleware|
           middleware.use ActionDispatch::Session::CookieStore, options
-          middleware.delete "ActionDispatch::ShowExceptions"
+          middleware.delete ActionDispatch::ShowExceptions
         end
 
         yield
-- 
cgit v1.2.3