Move target class code into a private method
This commit is contained in:
parent
e318763801
commit
a00d268f56
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue