Address review comments from @nilsding

This commit is contained in:
Karina Kwiatek 2022-01-22 22:48:55 +01:00 committed by Karina Kwiatek
parent 677ce26e6c
commit 731ee4bf69
7 changed files with 30 additions and 23 deletions

View File

@ -11,9 +11,9 @@ class Ajax::RelationshipController < AjaxController
params.require :screen_name params.require :screen_name
UseCase::Relationship::Create.call( UseCase::Relationship::Create.call(
current_user: current_user.screen_name, source_user: current_user.screen_name,
target_user: params[:screen_name], target_user: params[:screen_name],
type: params[:type] type: params[:type]
) )
@response[:success] = true @response[:success] = true
@response[:message] = t("messages.friend.create.okay") @response[:message] = t("messages.friend.create.okay")
@ -25,9 +25,9 @@ class Ajax::RelationshipController < AjaxController
def destroy def destroy
UseCase::Relationship::Destroy.call( UseCase::Relationship::Destroy.call(
current_user: current_user.screen_name, source_user: current_user.screen_name,
target_user: params[:screen_name], target_user: params[:screen_name],
type: params[:type] type: params[:type]
) )
@response[:success] = true @response[:success] = true
@response[:message] = t("messages.friend.destroy.okay") @response[:message] = t("messages.friend.destroy.okay")

View File

@ -136,11 +136,13 @@ Rails.application.routes.draw do
match '/@:username/q/:id', to: 'question#show', via: 'get', as: :show_user_question_alt match '/@:username/q/:id', to: 'question#show', via: 'get', as: :show_user_question_alt
match '/@:username/followers(/p/:page)', to: 'user#followers', via: 'get', as: :show_user_followers_alt, defaults: {page: 1} match '/@:username/followers(/p/:page)', to: 'user#followers', via: 'get', as: :show_user_followers_alt, defaults: {page: 1}
match '/@:username/followings(/p/:page)', to: 'user#followings', via: 'get', as: :show_user_followings_alt, defaults: {page: 1} match '/@:username/followings(/p/:page)', to: 'user#followings', via: 'get', as: :show_user_followings_alt, defaults: {page: 1}
match '/@:username/friends(/p/:page)', to: redirect('/@%{username}/followings/p/%{page}'), via: 'get', defaults: {page: 1}
match '/:username(/p/:page)', to: 'user#show', via: 'get', as: :show_user_profile, defaults: {page: 1} match '/:username(/p/:page)', to: 'user#show', via: 'get', as: :show_user_profile, defaults: {page: 1}
match '/:username/a/:id', to: 'answer#show', via: 'get', as: :show_user_answer match '/:username/a/:id', to: 'answer#show', via: 'get', as: :show_user_answer
match '/:username/q/:id', to: 'question#show', via: 'get', as: :show_user_question match '/:username/q/:id', to: 'question#show', via: 'get', as: :show_user_question
match '/:username/followers(/p/:page)', to: 'user#followers', via: 'get', as: :show_user_followers, defaults: {page: 1} match '/:username/followers(/p/:page)', to: 'user#followers', via: 'get', as: :show_user_followers, defaults: {page: 1}
match '/:username/followings(/p/:page)', to: 'user#followings', via: 'get', as: :show_user_followings, defaults: {page: 1} match '/:username/followings(/p/:page)', to: 'user#followings', via: 'get', as: :show_user_followings, defaults: {page: 1}
match '/:username/friends(/p/:page)', to: redirect('/%{username}/followings/p/%{page}'), via: 'get', defaults: {page: 1}
match '/:username/lists(/p/:page)', to: 'user#lists', via: 'get', as: :show_user_lists, defaults: {page: 1} match '/:username/lists(/p/:page)', to: 'user#lists', via: 'get', as: :show_user_lists, defaults: {page: 1}
match '/:username/questions(/p/:page)', to: 'user#questions', via: 'get', as: :show_user_questions, defaults: {page: 1} match '/:username/questions(/p/:page)', to: 'user#questions', via: 'get', as: :show_user_questions, defaults: {page: 1}

View File

@ -323,5 +323,6 @@ ActiveRecord::Schema.define(version: 2022_01_05_171216) do
t.index ["user_id"], name: "index_users_roles_on_user_id" t.index ["user_id"], name: "index_users_roles_on_user_id"
end end
add_foreign_key "mute_rules", "users"
add_foreign_key "profiles", "users" add_foreign_key "profiles", "users"
end end

View File

@ -6,8 +6,8 @@ require "errors"
module UseCase module UseCase
module Relationship module Relationship
class Create < UseCase::Base class Create < UseCase::Base
option :current_user, type: Types::Coercible::String option :source_user, type: Types::Coercible::String | Types.Instance(User)
option :target_user, type: Types::Coercible::String option :target_user, type: Types::Coercible::String | Types.Instance(User)
option :type, type: Types::RelationshipTypes option :type, type: Types::RelationshipTypes
def call def call
@ -28,9 +28,9 @@ module UseCase
private private
def find_source_user def find_source_user
return current_user if current_user.is_a?(::User) return source_user if source_user.is_a?(::User)
::User.find_by!(screen_name: current_user) ::User.find_by!(screen_name: source_user)
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound raise Errors::UserNotFound
end end

View File

@ -6,7 +6,7 @@ require "errors"
module UseCase module UseCase
module Relationship module Relationship
class Destroy < UseCase::Base class Destroy < UseCase::Base
option :current_user, type: Types::Coercible::String | Types.Instance(User) option :source_user, type: Types::Coercible::String | Types.Instance(User)
option :target_user, type: Types::Coercible::String | Types.Instance(User) option :target_user, type: Types::Coercible::String | Types.Instance(User)
option :type, type: Types::RelationshipTypes option :type, type: Types::RelationshipTypes
@ -28,12 +28,16 @@ module UseCase
private private
def find_source_user def find_source_user
::User.find_by!(screen_name: current_user) return source_user if source_user.is_a?(::User)
::User.find_by!(screen_name: source_user)
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound raise Errors::UserNotFound
end end
def find_target_user def find_target_user
return target_user if target_user.is_a?(::User)
::User.find_by!(screen_name: target_user) ::User.find_by!(screen_name: target_user)
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound raise Errors::UserNotFound

View File

@ -7,8 +7,8 @@ require "errors"
describe UseCase::Relationship::Create do describe UseCase::Relationship::Create do
shared_examples_for "valid relationship type" do shared_examples_for "valid relationship type" do
context "current_user does not exist" do context "source_user does not exist" do
let(:current_user) { "Schweinsbraten" } let(:source_user) { "Schweinsbraten" }
include_examples "raises an error", Errors::UserNotFound include_examples "raises an error", Errors::UserNotFound
end end
@ -19,13 +19,13 @@ describe UseCase::Relationship::Create do
include_examples "raises an error", Errors::UserNotFound include_examples "raises an error", Errors::UserNotFound
end end
context "target_user is current_user" do context "target_user is source_user" do
let(:target_user) { user1.screen_name } let(:target_user) { user1.screen_name }
include_examples "raises an error", Errors::SelfAction include_examples "raises an error", Errors::SelfAction
end end
context "target_user is different from current_user" do context "target_user is different from source_user" do
its([:status]) { is_expected.to eq(201) } its([:status]) { is_expected.to eq(201) }
its([:extra]) { is_expected.to eq(target_user: user2) } its([:extra]) { is_expected.to eq(target_user: user2) }
@ -40,13 +40,13 @@ describe UseCase::Relationship::Create do
let(:base_params) do let(:base_params) do
{ {
current_user: current_user, source_user: source_user,
target_user: target_user, target_user: target_user,
type: type type: type
} }
end end
let(:params) { base_params } let(:params) { base_params }
let(:current_user) { user1.screen_name } let(:source_user) { user1.screen_name }
let(:target_user) { user2.screen_name } let(:target_user) { user2.screen_name }
let(:type) { nil } let(:type) { nil }

View File

@ -26,15 +26,15 @@ describe UseCase::Relationship::Destroy do
end end
end end
context "current_user does not exist" do context "source_user does not exist" do
let(:current_user) { "Schweinsbraten" } let(:source_user) { "Schweinsbraten" }
let(:target_user) { user2.screen_name } let(:target_user) { user2.screen_name }
include_examples "raises an error", Errors::UserNotFound include_examples "raises an error", Errors::UserNotFound
end end
context "target_user does not exist" do context "target_user does not exist" do
let(:current_user) { user1.screen_name } let(:source_user) { user1.screen_name }
let(:target_user) { "peterwitzig" } let(:target_user) { "peterwitzig" }
include_examples "raises an error", Errors::UserNotFound include_examples "raises an error", Errors::UserNotFound
@ -43,13 +43,13 @@ describe UseCase::Relationship::Destroy do
let(:base_params) do let(:base_params) do
{ {
current_user: current_user, source_user: source_user,
target_user: target_user, target_user: target_user,
type: type type: type
} }
end end
let(:params) { base_params } let(:params) { base_params }
let(:current_user) { user1.screen_name } let(:source_user) { user1.screen_name }
let(:target_user) { user2.screen_name } let(:target_user) { user2.screen_name }
let(:type) { nil } let(:type) { nil }