From a00d268f56ffd00839a98cd3177ce9ce0deec083 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 5 Nov 2023 11:16:11 +0100 Subject: [PATCH] Move target class code into a private method --- app/controllers/reactions_controller.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/controllers/reactions_controller.rb b/app/controllers/reactions_controller.rb index 350df903..5e0384e7 100644 --- a/app/controllers/reactions_controller.rb +++ b/app/controllers/reactions_controller.rb @@ -15,9 +15,8 @@ class ReactionsController < ApplicationController def create params.require :id - params.require :type - target = params[:type].constantize.find_by!(id: params[:id]) + target = target_class.find_by!(id: params[:id]) UseCase::Reaction::Create.call( source_user: current_user, @@ -40,9 +39,8 @@ class ReactionsController < ApplicationController def destroy params.require :id - params.require :type - target = params[:type].constantize.find_by!(id: params[:id]) + target = target_class.find_by!(id: params[:id]) UseCase::Reaction::Destroy.call( source_user: current_user, @@ -62,4 +60,16 @@ class ReactionsController < ApplicationController format.html { redirect_back(fallback_location: root_path) } end end + + private + + ALLOWED_TYPES = %w[Answer Comment].freeze + private_constant :ALLOWED_TYPES + + def target_class + params.require :type + raise NameError unless ALLOWED_TYPES.include?(params[:type]) + + params[:type].constantize + end end