retrospring/app/controllers/reactions_controller.rb
Andreas Nedbal cc7fa787e8 Pass IDs to Reaction usecases instead of user instances
For some wild reason this locally sometimes causes coercion errors in the user instance check, restarting fixes it (temporarily?) so letting the UseCase resolve users is a cleaner solution here.
2024-03-19 22:45:19 +01:00

75 lines
1.7 KiB
Ruby

# frozen_string_literal: true
class ReactionsController < ApplicationController
include TurboStreamable
before_action :authenticate_user!, only: %w[create destroy]
turbo_stream_actions :create, :destroy
def index
answer = Answer.includes([smiles: { user: :profile }]).find(params[:id])
render "index", locals: { a: answer }
end
def create
params.require :id
target = target_class.find(params[:id])
UseCase::Reaction::Create.call(
source_user_id: current_user.id,
target:,
)
target.reload
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace("reaction-#{params[:type]}-#{params[:id]}", partial: "reactions/destroy", locals: { type: params[:type], target: }),
render_toast(t(".#{params[:type].downcase}.success"))
]
end
format.html { redirect_back(fallback_location: root_path) }
end
end
def destroy
params.require :id
target = target_class.find(params[:id])
UseCase::Reaction::Destroy.call(
source_user_id: current_user.id,
target:,
)
target.reload
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace("reaction-#{params[:type]}-#{params[:id]}", partial: "reactions/create", locals: { type: params[:type], target: }),
render_toast(t(".#{params[:type].downcase}.success"))
]
end
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