ackama / rails-template

Application template for Rails 7 projects; preloaded with best practices for TDD, security, deployment, and developer productivity.
Other
294 stars 15 forks source link

Discuss adopting Rails 7.1+ config.application_controller.raise_on_missing_callback_actions #507

Closed eoinkelly closed 9 months ago

eoinkelly commented 10 months ago

Rails 7.1 enables a feature where it will raise exception if your controller callback references an action which doesn't exist. Unfortunately this check seems to be performed before the if:/unless: option of the callback is evaluated. More details on the feature:

Our generated ApplicationController includes the following:

# this example is from the devise variant but all variants have this (minus the
# `unless ...` stuff)
after_action :verify_authorized, except: :index, unless: :devise_controller?
after_action :verify_policy_scoped, only: :index, unless: :devise_controller?

These after_action raise exceptions if they are run in a controller which does not have an index action. For example, the devise SessionsController and RegistionsController e.g.

Unknown action
The index action could not be found for the :verify_policy_scoped
callback on Users::SessionsController, but it is listed in the controller's
:only option.
Raising for missing callback actions is a new default in Rails 7.1, if you'd
like to turn this off you can delete the option from the environment configurations
or set `config.action_controller.raise_on_missing_callback_actions` to `false`.

These after_actions will also fail on any controller we define in future that doesn't have an index.

What are our options for handling this?

Option 1: Just disable config.action_controller.raise_on_missing_callback_actions

Pros/Cons (++/--)

Option 2: Move the controller action name check into the if:/unless: options

I tried this locally and it does work.

Pros/Cons (++/--)

Option 3: Remove the pundit policy checks

I'm rejecting this option immediately because losing policy checks is a much worse outcome than missing out on a new safety check from Rails.

joshmcarthur commented 10 months ago

I'd prefer option one, at least for now while we see what approaches others take. Filters, particularly at the ApplicationController level, ideally shouldn't be making such assumptions about the shape a particular controller takes. I can see the value, but it feels like coding around it is worse.

lukeify commented 9 months ago

We have decided on option one.