From be947bf4e2181c001cabce84f9befc541fa660fc Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 26 Oct 2023 08:16:18 +0200 Subject: [PATCH 1/9] Add `RelationshipsController` --- app/controllers/relationships_controller.rb | 47 +++++++++++++++++++++ app/views/relationships/_create.html.haml | 13 ++++++ app/views/relationships/_destroy.html.haml | 13 ++++++ config/routes.rb | 1 + 4 files changed, 74 insertions(+) create mode 100644 app/controllers/relationships_controller.rb create mode 100644 app/views/relationships/_create.html.haml create mode 100644 app/views/relationships/_destroy.html.haml diff --git a/app/controllers/relationships_controller.rb b/app/controllers/relationships_controller.rb new file mode 100644 index 000000000..d5c134ae9 --- /dev/null +++ b/app/controllers/relationships_controller.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +class RelationshipsController < ApplicationController + include TurboStreamable + + before_action :authenticate_user! + + turbo_stream_actions :create, :destroy + + def create + UseCase::Relationship::Create.call( + source_user: current_user, + target_user: ::User.find_by!(screen_name: params[:screen_name]), + type: params[:type] + ) + + respond_to do |format| + format.turbo_stream do + render turbo_stream: [ + turbo_stream.replace("#{params[:type]}-#{params[:screen_name]}", partial: "relationships/destroy", locals: { type: params[:type], screen_name: params[:screen_name] }), + render_toast(t(".#{params[:type]}.success")) + ] + end + + format.html { redirect_back(fallback_location: user_path(username: params[:screen_name])) } + end + end + + def destroy + UseCase::Relationship::Destroy.call( + source_user: current_user, + target_user: ::User.find_by!(screen_name: params[:screen_name]), + type: params[:type] + ) + + respond_to do |format| + format.turbo_stream do + render turbo_stream: [ + turbo_stream.replace("#{params[:type]}-#{params[:screen_name]}", partial: "relationships/create", locals: { type: params[:type], screen_name: params[:screen_name] }), + render_toast(t(".#{params[:type]}.success")) + ] + end + + format.html { redirect_back(fallback_location: user_path(username: params[:screen_name])) } + end + end +end diff --git a/app/views/relationships/_create.html.haml b/app/views/relationships/_create.html.haml new file mode 100644 index 000000000..23c91782b --- /dev/null +++ b/app/views/relationships/_create.html.haml @@ -0,0 +1,13 @@ +- if type == "follow" + = button_to relationships_path(screen_name:, type:), form: { id: "#{type}-#{screen_name}" }, class: "btn btn-primary", form_class: "d-grid" do + = t("voc.follow") + +- if type == "block" + = button_to relationships_path(screen_name:, type:), form: { id: "#{type}-#{screen_name}" }, class: "dropdown-item" do + %i.fa.fa-minus-circle.fa-fw + = t("voc.block") + +- if type == "mute" + = button_to relationships_path(screen_name:, type:), form: { id: "#{type}-#{screen_name}" }, class: "dropdown-item" do + %i.fa.fa-volume-off.fa-fw + = t("voc.mute") diff --git a/app/views/relationships/_destroy.html.haml b/app/views/relationships/_destroy.html.haml new file mode 100644 index 000000000..00b81b937 --- /dev/null +++ b/app/views/relationships/_destroy.html.haml @@ -0,0 +1,13 @@ +- if type == "follow" + = button_to relationships_path(screen_name:, type:), method: :delete, form: { id: "#{type}-#{screen_name}" }, class: "btn btn-primary", form_class: "d-grid" do + = t("voc.unfollow") + +- if type == "block" + = button_to relationships_path(screen_name:, type:), method: :delete, form: { id: "#{type}-#{screen_name}" }, class: "dropdown-item" do + %i.fa.fa-minus-circle.fa-fw + = t("voc.unblock") + +- if type == "mute" + = button_to relationships_path(screen_name:, type:), method: :delete, form: { id: "#{type}-#{screen_name}" }, class: "dropdown-item" do + %i.fa.fa-volume-off.fa-fw + = t("voc.unmute") diff --git a/config/routes.rb b/config/routes.rb index 46db96a82..7e5e259c7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -147,6 +147,7 @@ get "/inbox", to: "inbox#show", as: :inbox resource :subscriptions, controller: :subscriptions, only: %i[create destroy] + resource :relationships, only: %i[create destroy] get "/user/:username", to: "user#show" get "/@:username", to: "user#show", as: :user From e9290a73f840e486a40e82ffea66b5da1b747d0b Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 26 Oct 2023 08:16:43 +0200 Subject: [PATCH 2/9] Replace action targets with relationship partials --- app/views/user/_actions.html.haml | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/app/views/user/_actions.html.haml b/app/views/user/_actions.html.haml index 23960a38f..a70f19351 100644 --- a/app/views/user/_actions.html.haml +++ b/app/views/user/_actions.html.haml @@ -9,11 +9,9 @@ - elsif user_signed_in? .d-grid.gap-2 - if own_followings&.include?(user.id) || current_user.following?(user) - %button.btn.btn-primary{ type: :button, name: 'user-action', data: { action: :unfollow, type: type, target: user.screen_name } } - = t("voc.unfollow") + = render "relationships/destroy", type: "follow", screen_name: user.screen_name - else - %button.btn.btn-primary{ type: :button, name: 'user-action', data: { action: :follow, type: type, target: user.screen_name } } - = t("voc.follow") + = render "relationships/create", type: "follow", screen_name: user.screen_name .btn-group %button.btn.btn-light.btn-sm.dropdown-toggle{ data: { bs_toggle: :dropdown }, aria: { expanded: false } } = t(".title") @@ -23,21 +21,13 @@ %i.fa.fa-list.fa-fw = t(".list") - if own_blocks&.include?(user.id) || current_user.blocking?(user) - %a.dropdown-item{ href: '#', data: { action: :unblock, target: user.screen_name } } - %i.fa.fa-minus-circle.fa-fw - %span.pe-none= t("voc.unblock") + = render "relationships/destroy", type: "block", screen_name: user.screen_name - else - %a.dropdown-item{ href: '#', data: { action: :block, target: user.screen_name } } - %i.fa.fa-minus-circle.fa-fw - %span.pe-none= t("voc.block") + = render "relationships/create", type: "block", screen_name: user.screen_name - if own_mutes&.include?(user.id) || current_user.muting?(user) - %a.dropdown-item{ href: '#', data: { action: :unmute, target: user.screen_name } } - %i.fa.fa-volume-off.fa-fw - %span.pe-none= t("voc.unmute") + = render "relationships/destroy", type: "mute", screen_name: user.screen_name - else - %a.dropdown-item{ href: '#', data: { action: :mute, target: user.screen_name } } - %i.fa.fa-volume-off.fa-fw - %span.pe-none= t("voc.mute") + = render "relationships/create", type: "mute", screen_name: user.screen_name %a.dropdown-item{ href: '#', data: { action: 'report-user', target: user.screen_name } } %i.fa.fa-exclamation-triangle.fa-fw = t("voc.report") From 82b959f7515ecfb3a0c8a3a11bd1047ed005aac1 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 26 Oct 2023 08:17:02 +0200 Subject: [PATCH 3/9] Move relationship locales to controller scope --- config/locales/controllers.en.yml | 42 +++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/config/locales/controllers.en.yml b/config/locales/controllers.en.yml index 08b549dcc..684b7f192 100644 --- a/config/locales/controllers.en.yml +++ b/config/locales/controllers.en.yml @@ -101,27 +101,6 @@ en: notfound: "Question does not exist." noauth: "You are not allowed to delete this question." success: "Successfully deleted question." - relationship: - create: - block: - success: "Successfully blocked user." - error: "You are already blocking that user." - follow: - success: "Successfully followed user." - error: "You are already following that user." - mute: - success: "Successfully muted user." - error: "You are already muting that user." - destroy: - block: - success: "Successfully unblocked user." - error: "You are not blocking that user." - follow: - success: "Successfully unfollowed user." - error: "You are not following that user." - mute: - success: "Successfully unmuted user." - error: "You are not muting that user." report: create: noauth: :ajax.noauth @@ -216,6 +195,27 @@ en: registrations: destroy: export_pending: "You may not delete your account while account data is currently being exported." + relationships: + create: + block: + success: "Successfully blocked user." + error: "You are already blocking that user." + follow: + success: "Successfully followed user." + error: "You are already following that user." + mute: + success: "Successfully muted user." + error: "You are already muting that user." + destroy: + block: + success: "Successfully unblocked user." + error: "You are not blocking that user." + follow: + success: "Successfully unfollowed user." + error: "You are not following that user." + mute: + success: "Successfully unmuted user." + error: "You are not muting that user." timeline: public: title: "Public Timeline" From 10473f4ed0ffaf20863f73b1396f2fccf61778ab Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 26 Oct 2023 21:38:21 +0200 Subject: [PATCH 4/9] Remove `Ajax::RelationshipController` and spec --- .../ajax/relationship_controller.rb | 35 ---- .../ajax/relationship_controller_spec.rb | 153 ------------------ 2 files changed, 188 deletions(-) delete mode 100644 app/controllers/ajax/relationship_controller.rb delete mode 100644 spec/controllers/ajax/relationship_controller_spec.rb diff --git a/app/controllers/ajax/relationship_controller.rb b/app/controllers/ajax/relationship_controller.rb deleted file mode 100644 index ba9f45461..000000000 --- a/app/controllers/ajax/relationship_controller.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -class Ajax::RelationshipController < AjaxController - before_action :authenticate_user! - - def create - params.require :screen_name - - UseCase::Relationship::Create.call( - source_user: current_user, - target_user: ::User.find_by!(screen_name: params[:screen_name]), - type: params[:type] - ) - @response[:success] = true - @response[:message] = t(".#{params[:type]}.success") - rescue Errors::Base => e - @response[:message] = t(e.locale_tag) - ensure - return_response - end - - def destroy - UseCase::Relationship::Destroy.call( - source_user: current_user, - target_user: ::User.find_by!(screen_name: params[:screen_name]), - type: params[:type] - ) - @response[:success] = true - @response[:message] = t(".#{params[:type]}.success") - rescue Errors::Base => e - @response[:message] = t(e.locale_tag) - ensure - return_response - end -end diff --git a/spec/controllers/ajax/relationship_controller_spec.rb b/spec/controllers/ajax/relationship_controller_spec.rb deleted file mode 100644 index 11d2ddbd1..000000000 --- a/spec/controllers/ajax/relationship_controller_spec.rb +++ /dev/null @@ -1,153 +0,0 @@ -# coding: utf-8 -# frozen_string_literal: true - -require "rails_helper" - -describe Ajax::RelationshipController, type: :controller do - shared_examples_for "params is empty" do - let(:params) { {} } - - include_examples "ajax does not succeed", "is required" - end - - let!(:user) { FactoryBot.create(:user) } - let!(:user2) { FactoryBot.create(:user) } - - describe "#create" do - shared_examples_for "valid relationship type" do - it_behaves_like "params is empty" - - context "screen_name does not exist" do - let(:screen_name) { "peter-witzig" } - - include_examples "ajax does not succeed", "not found" - end - - context "screen_name is current user" do - let(:screen_name) { user.screen_name } - - include_examples "ajax does not succeed", "yourself" - end - - context "screen_name is different from current_user" do - let(:screen_name) { user2.screen_name } - - it "creates the relationship" do - expect { subject }.to change { Relationship.count }.by(1) - expect(Relationship.last.target.screen_name).to eq(screen_name) - end - end - end - - let(:type) { "Sauerkraut" } - let(:screen_name) { user2.screen_name } - let(:params) { { type: type, screen_name: screen_name } } - - subject { post(:create, params: params) } - - it_behaves_like "requires login" - - context "user signed in" do - before(:each) { sign_in(user) } - - context "type = 'follow'" do - let(:type) { "follow" } - - include_examples "valid relationship type" - - context "target user mutes source user" do - before do - user2.mute(user) - end - - it "creates the relationship but no notification" do - expect { subject }.to change { Notification.count }.by(0) - end - end - end - - context "type = 'block'" do - let(:type) { "block" } - - include_examples "valid relationship type" - end - - context "type = 'mute'" do - let(:type) { "mute" } - - include_examples "valid relationship type" - end - - context "type = 'dick'" do - let(:type) { "dick" } - - it_behaves_like "params is empty" - include_examples "ajax does not succeed", "Invalid parameter" - end - end - end - - describe "#destroy" do - shared_examples_for "valid relationship type" do - let(:screen_name) { user2.screen_name } - - context "relationship exists" do - before do - user.public_send(type, user2) - end - - it "destroys a relationship" do - expect { subject }.to change { Relationship.count }.by(-1) - end - end - - context "relationship does not exist" do - it "does not change anything at all" do - expect { subject }.to change { Relationship.count }.by(0) - end - end - - context "screen_name does not exist" do - let(:screen_name) { "peter-witzig" } - - include_examples "ajax does not succeed", "not found" - end - end - - let(:type) { "Sauerkraut" } - let(:screen_name) { user2.screen_name } - let(:params) { { type: type, screen_name: screen_name } } - - subject { delete(:destroy, params: params) } - - it_behaves_like "requires login" - - context "user signed in" do - before(:each) { sign_in(user) } - - context "type = 'follow'" do - let(:type) { "follow" } - - include_examples "valid relationship type" - end - - context "type = 'block'" do - let(:type) { "block" } - - include_examples "valid relationship type" - end - - context "type = 'mute'" do - let(:type) { "mute" } - - include_examples "valid relationship type" - end - - context "type = 'dick'" do - let(:type) { "dick" } - - include_examples "ajax does not succeed", "Invalid parameter" - end - end - end -end From 31ccc7c11bc962104cbbd43aaa5dd88419a43289 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 26 Oct 2023 21:39:24 +0200 Subject: [PATCH 5/9] Remove TypeScript relationship functionality --- .../retrospring/features/user/action.ts | 115 ------------------ .../retrospring/features/user/index.ts | 4 - 2 files changed, 119 deletions(-) delete mode 100644 app/javascript/retrospring/features/user/action.ts diff --git a/app/javascript/retrospring/features/user/action.ts b/app/javascript/retrospring/features/user/action.ts deleted file mode 100644 index 91ad152e0..000000000 --- a/app/javascript/retrospring/features/user/action.ts +++ /dev/null @@ -1,115 +0,0 @@ -import { post } from '@rails/request.js'; -import { showNotification, showErrorNotification } from 'utilities/notifications'; -import I18n from 'retrospring/i18n'; - -export function userActionHandler(event: Event): void { - event.preventDefault(); - const button: HTMLButtonElement = event.target as HTMLButtonElement; - const target = button.dataset.target; - const action = button.dataset.action; - button.disabled = true; - - let targetURL, relationshipType; - - switch (action) { - case 'follow': - targetURL = '/ajax/create_relationship'; - relationshipType = 'follow'; - break; - case 'unfollow': - targetURL = '/ajax/destroy_relationship'; - relationshipType = 'follow'; - break; - case 'block': - targetURL = '/ajax/create_relationship'; - relationshipType = 'block'; - break; - case 'unblock': - targetURL = '/ajax/destroy_relationship'; - relationshipType = 'block'; - break; - case 'mute': - targetURL = '/ajax/create_relationship'; - relationshipType = 'mute'; - break; - case 'unmute': - targetURL = '/ajax/destroy_relationship'; - relationshipType = 'mute'; - break; - } - let success = false; - - post(targetURL, { - body: { - screen_name: target, - type: relationshipType, - }, - contentType: 'application/json' - }) - .then(async response => { - const data = await response.json; - - success = data.success; - showNotification(data.message, data.success); - }) - .catch(err => { - console.log(err); - showErrorNotification(I18n.translate('frontend.error.message')); - }) - .finally(() => { - button.disabled = false; - if (!success) return; - - switch (action) { - case 'follow': - button.dataset.action = 'unfollow'; - button.innerText = I18n.translate('voc.unfollow'); - button.classList.remove('btn-primary'); - button.classList.add('btn-default'); - break; - case 'unfollow': - resetFollowButton(button); - break; - case 'block': - button.dataset.action = 'unblock'; - button.querySelector('span').innerText = I18n.translate('voc.unblock'); - if (button.classList.contains('btn')) { - button.classList.remove('btn-primary'); - button.classList.add('btn-default'); - } - resetFollowButton(document.querySelector('button[data-action="unfollow"]')); - break; - case 'unblock': - button.dataset.action = 'block'; - button.querySelector('span').innerText = I18n.translate('voc.block'); - if (button.classList.contains('btn')) { - button.classList.remove('btn-default'); - button.classList.add('btn-primary'); - } - break; - case 'mute': - button.dataset.action = 'unmute'; - button.querySelector('span').innerText = I18n.translate('voc.unmute'); - if (button.classList.contains('btn')) { - button.classList.remove('btn-primary'); - button.classList.add('btn-default'); - } - break; - case 'unmute': - button.dataset.action = 'mute'; - button.querySelector('span').innerText = I18n.translate('voc.mute'); - if (button.classList.contains('btn')) { - button.classList.remove('btn-default'); - button.classList.add('btn-primary'); - } - break; - } - }); -} - -function resetFollowButton(button: HTMLButtonElement) { - button.dataset.action = 'follow'; - button.innerText = I18n.translate('voc.follow'); - button.classList.remove('btn-default'); - button.classList.add('btn-primary'); -} diff --git a/app/javascript/retrospring/features/user/index.ts b/app/javascript/retrospring/features/user/index.ts index 00256485c..9d56c74f7 100644 --- a/app/javascript/retrospring/features/user/index.ts +++ b/app/javascript/retrospring/features/user/index.ts @@ -1,12 +1,8 @@ -import { userActionHandler } from './action'; import { userReportHandler } from './report'; import registerEvents from 'retrospring/utilities/registerEvents'; export default (): void => { registerEvents([ - { type: 'click', target: 'button[name=user-action]', handler: userActionHandler, global: true }, - { type: 'click', target: '[data-action=block], [data-action=unblock]', handler: userActionHandler, global: true }, - { type: 'click', target: '[data-action=mute], [data-action=unmute]', handler: userActionHandler, global: true }, { type: 'click', target: 'a[data-action=report-user]', handler: userReportHandler, global: true } ]); } From fc420a8091dff107ddd941a0e1ffb6c671be91ab Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 26 Oct 2023 21:39:47 +0200 Subject: [PATCH 6/9] Add specs for `RelationshipsController` --- .../relationships_controller_spec.rb | 153 ++++++++++++++++++ spec/shared_examples/error_raising.rb | 7 + 2 files changed, 160 insertions(+) create mode 100644 spec/controllers/relationships_controller_spec.rb diff --git a/spec/controllers/relationships_controller_spec.rb b/spec/controllers/relationships_controller_spec.rb new file mode 100644 index 000000000..6b3bed06a --- /dev/null +++ b/spec/controllers/relationships_controller_spec.rb @@ -0,0 +1,153 @@ +# coding: utf-8 +# frozen_string_literal: true + +require "rails_helper" + +describe RelationshipsController, type: :controller do + shared_examples_for "params is empty" do + let(:params) { {} } + + include_examples "turbo does not succeed", "is required" + end + + let!(:user) { FactoryBot.create(:user) } + let!(:user2) { FactoryBot.create(:user) } + + describe "#create" do + shared_examples_for "valid relationship type" do + it_behaves_like "params is empty" + + context "screen_name does not exist" do + let(:screen_name) { "peter-witzig" } + + include_examples "turbo does not succeed", "not found" + end + + context "screen_name is current user" do + let(:screen_name) { user.screen_name } + + include_examples "turbo does not succeed", "yourself" + end + + context "screen_name is different from current_user" do + let(:screen_name) { user2.screen_name } + + it "creates the relationship" do + expect { subject }.to change { Relationship.count }.by(1) + expect(Relationship.last.target.screen_name).to eq(screen_name) + end + end + end + + let(:type) { "Sauerkraut" } + let(:screen_name) { user2.screen_name } + let(:params) { { type: type, screen_name: screen_name } } + + subject { post(:create, params: params, format: :turbo_stream) } + + it_behaves_like "requires login" + + context "user signed in" do + before(:each) { sign_in(user) } + + context "type = 'follow'" do + let(:type) { "follow" } + + include_examples "valid relationship type" + + context "target user mutes source user" do + before do + user2.mute(user) + end + + it "creates the relationship but no notification" do + expect { subject }.to change { Notification.count }.by(0) + end + end + end + + context "type = 'block'" do + let(:type) { "block" } + + include_examples "valid relationship type" + end + + context "type = 'mute'" do + let(:type) { "mute" } + + include_examples "valid relationship type" + end + + context "type = 'dick'" do + let(:type) { "dick" } + + it_behaves_like "params is empty" + include_examples "turbo does not succeed", "Invalid parameter" + end + end + end + + describe "#destroy" do + shared_examples_for "valid relationship type" do + let(:screen_name) { user2.screen_name } + + context "relationship exists" do + before do + user.public_send(type, user2) + end + + it "destroys a relationship" do + expect { subject }.to change { Relationship.count }.by(-1) + end + end + + context "relationship does not exist" do + it "does not change anything at all" do + expect { subject }.to change { Relationship.count }.by(0) + end + end + + context "screen_name does not exist" do + let(:screen_name) { "peter-witzig" } + + include_examples "turbo does not succeed", "not found" + end + end + + let(:type) { "Sauerkraut" } + let(:screen_name) { user2.screen_name } + let(:params) { { type: type, screen_name: screen_name } } + + subject { delete(:destroy, params: params, format: :turbo_stream) } + + it_behaves_like "requires login" + + context "user signed in" do + before(:each) { sign_in(user) } + + context "type = 'follow'" do + let(:type) { "follow" } + + include_examples "valid relationship type" + end + + context "type = 'block'" do + let(:type) { "block" } + + include_examples "valid relationship type" + end + + context "type = 'mute'" do + let(:type) { "mute" } + + include_examples "valid relationship type" + end + + context "type = 'dick'" do + let(:type) { "dick" } + + include_examples "turbo does not succeed", "Invalid parameter" + end + end + end +end diff --git a/spec/shared_examples/error_raising.rb b/spec/shared_examples/error_raising.rb index 03290783d..527318b1e 100644 --- a/spec/shared_examples/error_raising.rb +++ b/spec/shared_examples/error_raising.rb @@ -13,3 +13,10 @@ expect(assigns(:response)[:message]).to include(part_of_error_message) end end + +RSpec.shared_examples_for "turbo does not succeed" do |part_of_error_message| + it "turbo does not succeed" do + subject + expect(response.body).to include(part_of_error_message) + end +end From e222ab7c3080bd8218807563230611591ae8e13a Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 26 Oct 2023 22:06:58 +0200 Subject: [PATCH 7/9] Temporarily fix turbo stream error example --- spec/shared_examples/error_raising.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/shared_examples/error_raising.rb b/spec/shared_examples/error_raising.rb index 527318b1e..34417e227 100644 --- a/spec/shared_examples/error_raising.rb +++ b/spec/shared_examples/error_raising.rb @@ -17,6 +17,7 @@ RSpec.shared_examples_for "turbo does not succeed" do |part_of_error_message| it "turbo does not succeed" do subject - expect(response.body).to include(part_of_error_message) + # FIXME: for some reason, partials are not rendered, making the actual error message not accessible + expect(response.body).to include "turbo-stream action=\"append\" target=\"toasts\"" end end From 54fb4d09fd39c426c2c4c4116f20a8d2b67f704c Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 26 Oct 2023 22:15:51 +0200 Subject: [PATCH 8/9] Fix lint nits --- app/controllers/relationships_controller.rb | 4 ++-- app/views/relationships/_destroy.html.haml | 3 ++- spec/controllers/relationships_controller_spec.rb | 8 ++++---- spec/shared_examples/error_raising.rb | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/controllers/relationships_controller.rb b/app/controllers/relationships_controller.rb index d5c134ae9..c750d9c43 100644 --- a/app/controllers/relationships_controller.rb +++ b/app/controllers/relationships_controller.rb @@ -11,7 +11,7 @@ def create UseCase::Relationship::Create.call( source_user: current_user, target_user: ::User.find_by!(screen_name: params[:screen_name]), - type: params[:type] + type: params[:type], ) respond_to do |format| @@ -30,7 +30,7 @@ def destroy UseCase::Relationship::Destroy.call( source_user: current_user, target_user: ::User.find_by!(screen_name: params[:screen_name]), - type: params[:type] + type: params[:type], ) respond_to do |format| diff --git a/app/views/relationships/_destroy.html.haml b/app/views/relationships/_destroy.html.haml index 00b81b937..f26ec5216 100644 --- a/app/views/relationships/_destroy.html.haml +++ b/app/views/relationships/_destroy.html.haml @@ -1,5 +1,6 @@ - if type == "follow" - = button_to relationships_path(screen_name:, type:), method: :delete, form: { id: "#{type}-#{screen_name}" }, class: "btn btn-primary", form_class: "d-grid" do + = button_to relationships_path(screen_name:, type:), method: :delete, form: { id: "#{type}-#{screen_name}" }, class: "btn btn-primary", + form_class: "d-grid" do = t("voc.unfollow") - if type == "block" diff --git a/spec/controllers/relationships_controller_spec.rb b/spec/controllers/relationships_controller_spec.rb index 6b3bed06a..68c6a4e35 100644 --- a/spec/controllers/relationships_controller_spec.rb +++ b/spec/controllers/relationships_controller_spec.rb @@ -41,9 +41,9 @@ let(:type) { "Sauerkraut" } let(:screen_name) { user2.screen_name } - let(:params) { { type: type, screen_name: screen_name } } + let(:params) { { type:, screen_name: } } - subject { post(:create, params: params, format: :turbo_stream) } + subject { post(:create, params:, format: :turbo_stream) } it_behaves_like "requires login" @@ -116,9 +116,9 @@ let(:type) { "Sauerkraut" } let(:screen_name) { user2.screen_name } - let(:params) { { type: type, screen_name: screen_name } } + let(:params) { { type:, screen_name: } } - subject { delete(:destroy, params: params, format: :turbo_stream) } + subject { delete(:destroy, params:, format: :turbo_stream) } it_behaves_like "requires login" diff --git a/spec/shared_examples/error_raising.rb b/spec/shared_examples/error_raising.rb index 34417e227..8ddf0b630 100644 --- a/spec/shared_examples/error_raising.rb +++ b/spec/shared_examples/error_raising.rb @@ -14,7 +14,7 @@ end end -RSpec.shared_examples_for "turbo does not succeed" do |part_of_error_message| +RSpec.shared_examples_for "turbo does not succeed" do it "turbo does not succeed" do subject # FIXME: for some reason, partials are not rendered, making the actual error message not accessible From e802e0197dbc79745e062413545fb883beaed559 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 26 Oct 2023 22:43:01 +0200 Subject: [PATCH 9/9] Make `turbo does not succeed` example for in `RelationshipsController` --- app/controllers/relationships_controller.rb | 2 ++ spec/controllers/relationships_controller_spec.rb | 2 ++ spec/shared_examples/error_raising.rb | 5 ++--- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/controllers/relationships_controller.rb b/app/controllers/relationships_controller.rb index c750d9c43..e9965f794 100644 --- a/app/controllers/relationships_controller.rb +++ b/app/controllers/relationships_controller.rb @@ -8,6 +8,8 @@ class RelationshipsController < ApplicationController turbo_stream_actions :create, :destroy def create + params.require :screen_name + UseCase::Relationship::Create.call( source_user: current_user, target_user: ::User.find_by!(screen_name: params[:screen_name]), diff --git a/spec/controllers/relationships_controller_spec.rb b/spec/controllers/relationships_controller_spec.rb index 68c6a4e35..9642b34b8 100644 --- a/spec/controllers/relationships_controller_spec.rb +++ b/spec/controllers/relationships_controller_spec.rb @@ -4,6 +4,8 @@ require "rails_helper" describe RelationshipsController, type: :controller do + render_views + shared_examples_for "params is empty" do let(:params) { {} } diff --git a/spec/shared_examples/error_raising.rb b/spec/shared_examples/error_raising.rb index 8ddf0b630..527318b1e 100644 --- a/spec/shared_examples/error_raising.rb +++ b/spec/shared_examples/error_raising.rb @@ -14,10 +14,9 @@ end end -RSpec.shared_examples_for "turbo does not succeed" do +RSpec.shared_examples_for "turbo does not succeed" do |part_of_error_message| it "turbo does not succeed" do subject - # FIXME: for some reason, partials are not rendered, making the actual error message not accessible - expect(response.body).to include "turbo-stream action=\"append\" target=\"toasts\"" + expect(response.body).to include(part_of_error_message) end end