The personal finance app for everyone
Enhance member removal process by destroying associated invitations and updating flash messages in ProfilesController. Add test to verify invitation deletion upon member removal.
Josh Pigford1b1add38f292
diff --git a/app/controllers/settings/profiles_controller.rb b/app/controllers/settings/profiles_controller.rbindex d62eecd6..a5631da4 100644--- a/app/controllers/settings/profiles_controller.rb+++ b/app/controllers/settings/profiles_controller.rb@@ -23,9 +23,11 @@ class Settings::ProfilesController < ApplicationControllerendif @user.destroy- flash[:notice] = t("settings.profiles.destroy.member_removed")+ # Also destroy the invitation associated with this user for this family+ Current.family.invitations.find_by(email: @user.email)&.destroy+ flash[:notice] = "Member removed successfully."else- flash[:alert] = t("settings.profiles.destroy.member_removal_failed")+ flash[:alert] = "Failed to remove member."endredirect_to settings_profile_pathdiff --git a/test/controllers/settings/profiles_controller_test.rb b/test/controllers/settings/profiles_controller_test.rbindex 27d62c17..deae066d 100644--- a/test/controllers/settings/profiles_controller_test.rb+++ b/test/controllers/settings/profiles_controller_test.rb@@ -19,7 +19,7 @@ class Settings::ProfilesControllerTest < ActionDispatch::IntegrationTestendassert_redirected_to settings_profile_path- assert_equal I18n.t("settings.profiles.destroy.member_removed"), flash[:notice]+ assert_equal "Member removed successfully.", flash[:notice]assert_raises(ActiveRecord::RecordNotFound) { User.find(@member.id) }end@@ -44,4 +44,24 @@ class Settings::ProfilesControllerTest < ActionDispatch::IntegrationTestassert_equal I18n.t("settings.profiles.destroy.not_authorized"), flash[:alert]assert User.find(@admin.id)end++ test "admin removing a family member also destroys their invitation" do+ # Create an invitation for the member+ invitation = @admin.family.invitations.create!(+ email: @member.email,+ role: "member",+ inviter: @admin+ )++ sign_in @admin++ assert_difference [ "User.count", "Invitation.count" ], -1 do+ delete settings_profile_path(user_id: @member)+ end++ assert_redirected_to settings_profile_path+ assert_equal "Member removed successfully.", flash[:notice]+ assert_raises(ActiveRecord::RecordNotFound) { User.find(@member.id) }+ assert_raises(ActiveRecord::RecordNotFound) { Invitation.find(invitation.id) }+ endend