Move target class code into a private method

This commit is contained in:
Andreas Nedbal 2023-11-05 11:16:11 +01:00 committed by Andreas Nedbal
parent e318763801
commit a00d268f56
1 changed files with 14 additions and 4 deletions

View File

@ -15,9 +15,8 @@ class ReactionsController < ApplicationController
def create def create
params.require :id 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( UseCase::Reaction::Create.call(
source_user: current_user, source_user: current_user,
@ -40,9 +39,8 @@ class ReactionsController < ApplicationController
def destroy def destroy
params.require :id 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( UseCase::Reaction::Destroy.call(
source_user: current_user, source_user: current_user,
@ -62,4 +60,16 @@ class ReactionsController < ApplicationController
format.html { redirect_back(fallback_location: root_path) } format.html { redirect_back(fallback_location: root_path) }
end end
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 end