Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow inserting middleware at arbitrary points in the middleware stack #1390

Merged
merged 1 commit into from
May 9, 2016
Merged

Allow inserting middleware at arbitrary points in the middleware stack #1390

merged 1 commit into from
May 9, 2016

Conversation

rosa
Copy link
Contributor

@rosa rosa commented May 8, 2016

This pull request is inspired by #794. I was using Grape and found myself needing this feature, so I have built a possible way of supporting insert, insert_after and insert_before in a similar way as Rails does.

Adding a new middleware can now be done through use, insert, insert_after and insert_before, and the operation chosen to do so is stacked together with the middleware class, args, and blocks. The stack is built using the operations in Grape::Endpoint#build_stack using a new class Grape::Middleware::Stack, based on ActionDispatch::MiddlewareStack with some simplifications (for example, only classes are supported for middleware, no symbols or strings, which is being deprecated in ActionDispatch in any case).

The commit message offers more details on the implementation itself. If anyone has a better idea of how this feature could be implemented I'd be glad to modify my changes to follow a different approach.

This is my first PR in the Grape project and I have tried to follow the same style when adding new tests for the feature and updating the README, but I'd be more than happy to adapt those according to feedback 😃


class API < Grape::API
use Overwriter
insert_before Overwriter, CustomOverwriter, message: 'Overwrited again.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overwrited => Overwritten :)

@dblock
Copy link
Member

dblock commented May 9, 2016

This is excellent, code is spot on. I made minor text or other comments if you could please amend and I'll merge.

Changes include:

* Include the operation when stacking a new middleware
  The operation gets stored before the rest of the middleware data
  (class, args and block), and we use it later to determine the
  order in which the middleware stack will be built.
  Currently support `insert_after` and `insert_before`, plus `use`
  that does the same as before.
* Implement a new `Grape::Middleware::Stack` class
  This class contains the functionality to build the middleware stack
  taking the different operations into account (`use`, `insert_before`
  and `insert_after`). It's mostly based on `ActionDispatch::MiddlewareStack`
  with some simplifications.
* Build middleware stack for endpoint using `Grape::Middleware::Stack`
  Keep the same order for default middleware stack with `use` and use the
  new class's `build` method to determine the real order of the final
  stack.
* Fix small typo in `Grape::Util::StackableValues` (replace `froozen_values`
  by `frozen_values`).
* Update `CHANGELOG` with the PR number.
@rosa
Copy link
Contributor Author

rosa commented May 9, 2016

Thanks so much for the quick review and the feedback! I think I have addressed all the comments now, but feel free to add more comments if you find anything else that can be improved :)

@@ -3,16 +3,16 @@ module Util
class StackableValues
attr_accessor :inherited_values
attr_reader :new_values
attr_reader :froozen_values
attr_reader :frozen_values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those froozen values were just extra frozen. Thanks for spotting this ;)

@dblock dblock merged commit 05bb1b4 into ruby-grape:master May 9, 2016
@dblock
Copy link
Member

dblock commented May 9, 2016

Merged.

ridiculous pushed a commit to ridiculous/grape-middleware-logger that referenced this pull request May 12, 2016
* No longer necessary because of the middleware stack added in ruby-grape/grape#1390
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants