-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add Action Filters Order section #353
base: master
Are you sure you want to change the base?
Conversation
I agree with the rule, but as with Callbacks Order, I think it's sufficient to give a small example (e.g. two filters) and have a link to the full list (e.g. the post referenced above). |
I couldn't find this information, specifically the list, in the official Rails guide.
Done. |
8a53e04
to
5109d04
Compare
|
||
[source,ruby] | ||
---- | ||
# bad |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels unnecessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All bad examples?
README.adoc
Outdated
around_action :around_action_filter1 | ||
around_action :around_action_filter2 | ||
after_action :after_action_filter | ||
append_before_action :append_before_action_filter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to use an append/prepend declarations in the same class as with regular before/after?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to use an append/prepend declarations in the same class as with regular before/after?
Yes, I use them in my project, especially when working with built-in action filters from gems (for example, to run recaptcha checks before devise's built-in action filters). Otherwise, I use action filter without prepend_
.
README.adoc
Outdated
append_before_action :append_before_action_filter | ||
append_around_action :append_around_action_filter | ||
append_after_action :append_after_action_filter | ||
prepend_before_action :prepend_before_action_filter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren’t prepend before or append after just aliases of before/after just like rspec hooks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
append_#{callback}_action
is alias for #{callback}_action
. Ref: https://github.com/rails/rails/blob/main/actionpack/lib/abstract_controller/callbacks.rb#L250C26-L250C26
@@ -266,6 +266,28 @@ class UsersController < ApplicationController | |||
end | |||
---- | |||
|
|||
=== Action Filters Order [[action-filters-order]] | |||
|
|||
Order controller filter declarations in the order in which they will be executed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is getting complicated with inheritance and concerns.
When it comes to a point where order matters, isn’t it a sign that a different approach is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that the action filters order will significantly impact the complexity of the code. Instead, it provides clarity in understanding the order in which the action filters are executed.
I try to avoid inheriting controllers other than inheriting from the application controller.
And as for the concerns, I haven't had any problems with them other than this one.
5109d04
to
87c18a5
Compare
We have a related guideline https://github.com/rubocop/rails-style-guide#lexically-scoped-action-filter The consequence on our case is that the order of those filters is hard to predict. And ordering just local ones would just solve part of this disorder. |
We would have the "correct" order in all controllers. And even with inherited controllers or concerns, the code would still be easier to read. At least it definitely won’t be more difficult. |
It is similar to this rule, but for action filters.
ref: https://dev.to/timkrins/an-experiment-with-controller-action-callbacks-in-rails-c3p