From a680235773a4baf2ff68692fb8305369b847184a Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Wed, 17 Sep 2025 18:21:50 +0200 Subject: [PATCH] Redirect to home if remove yourself from the collection --- app/controllers/collections_controller.rb | 7 +++++-- test/controllers/collections_controller_test.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb index 285ca6964..2bc7fa08d 100644 --- a/app/controllers/collections_controller.rb +++ b/app/controllers/collections_controller.rb @@ -20,8 +20,11 @@ class CollectionsController < ApplicationController def update @collection.update! collection_params @collection.accesses.revise granted: grantees, revoked: revokees if grantees_changed? - - redirect_to edit_collection_path(@collection), notice: "Saved" + if @collection.accessible_to?(Current.user) + redirect_to edit_collection_path(@collection), notice: "Saved" + else + redirect_to root_path, notice: "Saved (you were removed from the collection)" + end end def destroy diff --git a/test/controllers/collections_controller_test.rb b/test/controllers/collections_controller_test.rb index 870f5dec5..61b90cf07 100644 --- a/test/controllers/collections_controller_test.rb +++ b/test/controllers/collections_controller_test.rb @@ -45,6 +45,18 @@ class CollectionsControllerTest < ActionDispatch::IntegrationTest assert_not collections(:writebook).all_access? end + test "update redirects to root when user removes themselves from collection" do + collection = collections(:writebook) + + patch collection_path(collection), params: { + collection: { name: "Updated name", all_access: false }, + user_ids: users(:david, :jz).pluck(:id) + } + + assert_redirected_to root_path + assert_not collection.reload.users.include?(users(:kevin)) + end + test "update collection with granular permissions, submitting no user ids" do assert_not collections(:private).all_access?