FIX: compatibility with Rails 6.0.3

FIX: compatibility with Rails 6.0.3

Rails 6.0.3 amends it so you can no longer sniff through the middleware stack.

Our hack that did this no longer works. It was removed and replaces with a far more predictable system.

To bypass message_bus auto configuring middleware use

config.skip_message_bus_middleware = true

diff --git a/CHANGELOG b/CHANGELOG
index ac863df..2e2c8cf 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,8 @@
+- Version 3.2.0
+
+   - FIX: compatability with Rails 6.0.3, note: apps without ActionDispatch::Flash may stop working after this upgrade
+   to correct this disable middleware injection with `config.skip_message_bus_middleware = true` and configure middleware by hand with `app.middleware.use(MessageBus::Rack::Middleware)`
+
 28-04-2020
 
 - Version 3.1.0
diff --git a/lib/message_bus/rails/railtie.rb b/lib/message_bus/rails/railtie.rb
index f37dce3..1008d45 100644
--- a/lib/message_bus/rails/railtie.rb
+++ b/lib/message_bus/rails/railtie.rb
@@ -11,27 +11,29 @@ class MessageBus::Rails::Railtie < ::Rails::Railtie
     # the Rails app is configured that might be ActionDispatch::Session::CookieStore, or potentially
     # ActionDispatch::Session::ActiveRecordStore.
     #
-    # To handle either case, we insert it before ActionDispatch::Flash.
-    #
-    # For APIs or apps that have ActionDispatch::Flash deleted from the middleware
-    # stack we just push MessageBus to the bottom.
-    if api_only?(app.config) || flash_middleware_deleted?(app.middleware)
-      app.middleware.use(MessageBus::Rack::Middleware)
-    else
-      app.middleware.insert_before(ActionDispatch::Flash, MessageBus::Rack::Middleware)
+    # given https://github.com/rails/rails/commit/fedde239dcee256b417dc9bcfe5fef603bf0d952#diff-533a9a9cc17a8a899cb830626089e5f9
+    # there is no way of walking the stack for operations
+    if !skip_middleware?(app.config)
+      if api_only?(app.config)
+        app.middleware.use(MessageBus::Rack::Middleware)
+      else
+        app.middleware.insert_before(ActionDispatch::Flash, MessageBus::Rack::Middleware)
+      end
     end
 
     MessageBus.logger = Rails.logger
   end
 
+  def skip_middleware?(config)
+    return false if !config.respond_to?(:skip_message_bus_middleware)
+
+    config.skip_message_bus_middleware
+  end
+
   def api_only?(config)
-    return false unless config.respond_to?(:api_only)
+    return false if !config.respond_to?(:api_only)
 
     config.api_only
   end
 
-  def flash_middleware_deleted?(middleware)
-    ops = middleware.instance_variable_get(:@operations)
-    ops.any? { |m| m[0] == :delete && m[1].include?(ActionDispatch::Flash) }
-  end
 end
diff --git a/lib/message_bus/version.rb b/lib/message_bus/version.rb
index d1121de..533b8bb 100644
--- a/lib/message_bus/version.rb
+++ b/lib/message_bus/version.rb
@@ -1,5 +1,5 @@
 # frozen_string_literal: true
 
 module MessageBus
-  VERSION = "3.1.0"
+  VERSION = "3.2.0"
 end

GitHub sha: 6d3a0a8b