rails/guides
Genadi Samokovarov 40fc1651ad Delayed middleware delete does not allow move operations
While trying to fix #16433, we made the middleware deletions always
happen at the end. While this works for the case of deleting the
Rack::Runtime middleware, it makes operations like the following
misbehave.

```ruby
gem "bundler", "< 1.16"

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails"
end

require "action_controller/railtie"

class TestApp < Rails::Application
  config.root = __dir__
  secrets.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger

  middleware.insert_after ActionDispatch::Session::CookieStore, ::Rails::Rack::Logger, config.log_tags
  middleware.delete ::Rails::Rack::Logger
end

require "minitest/autorun"
require "rack/test"

class BugTest < Minitest::Test
  include Rack::Test::Methods

  def test_returns_success
    get "/"
    assert last_response.ok?
  end

  private
    def app
      Rails.application
    end
end
```

In the case ☝️  the ::Rails::Rack::Logger would be deleted instead of
moved, because the order of middleware stack building execution will be:

```ruby
[:insert, ActionDispatch::Session::CookieStore, [::Rails::Rack::Logger]]
[:delete, ::Rails::Rack::Logger, [config.log_tags]]
```

This is pretty surprising and hard to reason about behaviour, unless you
go spelunking into the Rails configuration code.

I have a few solutions in mind and all of them have their drawbacks.

1. Introduce a `Rails::Configuration::MiddlewareStackProxy#delete!` that
delays the deleted operations. This will make `#delete` to be executed
in order. The drawback here is backwards incompatible behavior and a new
public method.

2. Just revert to the old operations. This won't allow people to delete
the `Rack::Runtime` middleware.

3. Legitimize the middleware moving with the new `#move_after` and
`#move_before` methods. This does not breaks any backwards
compatibility, but includes 2 new methods to the middleware stack.

I have implemented `3.` in this pull request.

Happy holidays! 🎄
2020-01-08 11:30:02 +02:00
..
assets Highlight code with Prism on 'turbolinks:load' [ci skip] 2019-12-17 19:38:07 -05:00
bug_report_templates Style 2019-12-04 21:10:58 -05:00
rails_guides Add missing languages for guides code blocks [ci skip] 2019-12-17 19:38:45 -05:00
source Delayed middleware delete does not allow move operations 2020-01-08 11:30:02 +02:00
.document adds guides/.document to tell rdoc not to process this directory 2013-03-04 22:28:23 +01:00
CHANGELOG.md Start Rails 6.1 development 2019-04-24 15:57:14 -04:00
rails_guides.rb Use safe nav operator when calling RailsGuides::Generator.new 2018-12-05 12:41:07 +09:00
Rakefile Respect ENV variables when finding DBs etc for the test suite 2019-02-06 01:20:06 +10:30
w3c_validator.rb Use frozen string literal in guides/ 2017-08-13 22:04:09 +09:00