From cc7fa787e8b12822fe4de6128731f47e863c040d Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Tue, 19 Mar 2024 22:06:57 +0100 Subject: [PATCH] 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. --- app/controllers/reactions_controller.rb | 4 ++-- lib/use_case/reaction/create.rb | 8 +++++++- lib/use_case/reaction/destroy.rb | 8 +++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/controllers/reactions_controller.rb b/app/controllers/reactions_controller.rb index 215adf27..b1914e46 100644 --- a/app/controllers/reactions_controller.rb +++ b/app/controllers/reactions_controller.rb @@ -19,7 +19,7 @@ class ReactionsController < ApplicationController target = target_class.find(params[:id]) UseCase::Reaction::Create.call( - source_user: current_user, + source_user_id: current_user.id, target:, ) @@ -43,7 +43,7 @@ class ReactionsController < ApplicationController target = target_class.find(params[:id]) UseCase::Reaction::Destroy.call( - source_user: current_user, + source_user_id: current_user.id, target:, ) diff --git a/lib/use_case/reaction/create.rb b/lib/use_case/reaction/create.rb index a126bba3..1939f9d6 100644 --- a/lib/use_case/reaction/create.rb +++ b/lib/use_case/reaction/create.rb @@ -3,7 +3,7 @@ module UseCase module Reaction class Create < UseCase::Base - option :source_user, type: Types.Instance(::User) + option :source_user_id, type: Types::Coercible::Integer option :target, type: Types.Instance(::Answer) | Types.Instance(::Comment) option :content, type: Types::Coercible::String, optional: true @@ -15,6 +15,12 @@ module UseCase resource: reaction, } end + + private + + def source_user + @source_user ||= ::User.find(source_user_id) + end end end end diff --git a/lib/use_case/reaction/destroy.rb b/lib/use_case/reaction/destroy.rb index 52fe3b7a..8d77219a 100644 --- a/lib/use_case/reaction/destroy.rb +++ b/lib/use_case/reaction/destroy.rb @@ -3,7 +3,7 @@ module UseCase module Reaction class Destroy < UseCase::Base - option :source_user, type: Types.Instance(::User) + option :source_user_id, type: Types::Coercible::Integer option :target, type: Types.Instance(::Answer) | Types.Instance(::Comment) def call @@ -14,6 +14,12 @@ module UseCase resource: nil, } end + + private + + def source_user + @source_user ||= ::User.find(source_user_id) + end end end end