¿Son múltiples las llamadas before_action con un estilo de código incorrecto?
Estoy trabajando en una aplicación con controlador que tiene muchas acciones antes. La mayoría de ellos están conectados entre sí por variables de instancia que establecen. Por ejemplo:
def first_action
@first_variable = Something.new
end
def second_action
if @first_variable
@second_variable = Other.new
end
end
El controlador se ve así:
class ExampleController < ApplicationController
before_action :first_action, only: [:index, :show, :create]
before_action :second_action, only: [:index, :show, :create]
before_action :third_action, only: [:index, :show, :create]
before_action :fourth_action, only: [:index, :show, :create]
before_action :fifth_action, only: [:index, :show, :create]
before_action :sixth_action, only: [:index, :show, :create]
before_action :seventh_action, only: [:index, :show, :create]
def index
# some code
end
def show
# some code
end
def create
# some code
end
private
# all of the before_action methods
end
Es realmente difícil de entender desde el punto de vista mío. Cada uno de esos métodos tiene mucho código. Además, hay controladores que heredan de este y también usan parte o todas esas acciones.
Escuché que es mejor ser explícito sobre las variables cargadas en cada método, pero esto:
class ExampleController < ApplicationController
def index
first_action
second_action
third_action
fourth_action
fifth_action
sixth_action
seventh_action
# some code
end
def show
first_action
second_action
third_action
fourth_action
fifth_action
sixth_action
seventh_action
# some code
end
def create
first_action
second_action
third_action
fourth_action
fifth_action
sixth_action
seventh_action
# some code
end
private
# all of the before_action methods
end
No se ve mucho mejor. ¿Hay alguna forma de refactorizarlo para una mayor legibilidad o debería seguir con la solución actual?