Add Action Filters Order section#353
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 |
| 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.
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.
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_.
| 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.
Aren’t prepend before or append after just aliases of before/after just like rspec hooks?
There was a problem hiding this comment.
append_#{callback}_action is alias for #{callback}_action. Ref: https://github.com/rails/rails/blob/main/actionpack/lib/abstract_controller/callbacks.rb#L250C26-L250C26
|
|
||
| === 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.
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.
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