3

Add `Middleware#remove` to delete middleware or raise if not found. by p8 · Pull...

 3 years ago
source link: https://github.com/rails/rails/pull/42821
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Conversation

Copy link

Member

p8 commented 4 days ago

edited

Summary

Middleware#remove works just like Middleware#delete but will
raise an error if the middleware isn't found.

This partly reverts 07558ff, where delete would raise an error.
It might be expected that delete fails silently for some environments.
Or maybe you want to make sure a middleware isn't present.

See: #42655 (comment)

cc @jhawthorn

Co-authored-by: Alex Ghiculescu [email protected]

@@ -130,7 +130,11 @@ def swap(target, *args, &block)

ruby2_keywords(:swap)

def delete(target)

middlewares.reject! { |m| m.name == target.name } || (raise "No such middleware to delete: #{target.inspect}")

middlewares.reject! { |m| m.name == target.name }

p8 4 days ago

edited

Author

Member

The original implementation used delete_if instead of reject!.
delete_if returns the middlewares if nothing is found.
reject! returns nil if nothing is found.

Array#delete also returns nil if no matching item is found, so reject! seems the better choice.

Copy link

Member

Author

p8 commented 4 days ago

@ghiculescu Yes, would be nice as well.

guilleiguaran

merged commit f4229a2 into rails:main 4 days ago

4 checks passed

Copy link

Member

morgoth commented yesterday

@p8 I'm wondering if delete! wouldn't be better? By looking at 2 methods without documentation I would never guess which one would raise en error.

p8

deleted the

p8:actionpack/middleware-remove

branch

yesterday

Copy link

Member

Author

p8 commented yesterday

@morgoth I agree smile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK