From a23cdd809093cadfe691e6128604142240380fd4 Mon Sep 17 00:00:00 2001 From: Gabriel Torres <127896538+Gabe-Torres@users.noreply.github.com> Date: Thu, 19 Dec 2024 23:28:24 -0600 Subject: [PATCH] Update: User Registration Redirect Path This update utilizes Devise's after_update_path_for method to handle user redirection after updating their registration. If the user is part of the staff roster, associated with a valid organization, and authorized to access the organization's dashboard, they will be redirected to their staff dashboard. If the user is an adopter or fosterer and authorized to access the adopter/foster dashboard, they will be redirected there. If neither condition is met, the user will be redirected to the root path. Adds: Controller tests for user registration updates: Test case for adopter/fosterer user redirection. Test case for staff user redirection. --- app/controllers/registrations_controller.rb | 10 ++++++++ .../registrations_controller_test.rb | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index a803016f0..c9813c286 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -56,6 +56,16 @@ def account_update_params :avatar) end + def after_update_path_for(resource) + if User.staff.include?(resource) && resource.organization_id == Current.organization.id + return root_path unless allowed_to?(:index?, with: Organizations::DashboardPolicy, context: {organization: Current.organization}) + staff_dashboard_index_path + else + return root_path unless allowed_to?(:index?, with: Organizations::AdopterFosterDashboardPolicy, context: {organization: Current.organization}) + adopter_fosterer_dashboard_index_path + end + end + def after_sign_up_path_for(resource) return root_path unless allowed_to?(:index?, with: Organizations::AdopterFosterDashboardPolicy, context: {organization: Current.organization}) diff --git a/test/controllers/registrations_controller_test.rb b/test/controllers/registrations_controller_test.rb index 100d24c85..1b3ad0dc3 100644 --- a/test/controllers/registrations_controller_test.rb +++ b/test/controllers/registrations_controller_test.rb @@ -37,4 +37,27 @@ class RegistrationsControllerTest < ActionDispatch::IntegrationTest get edit_user_registration_url(script_name: "/#{organization.slug}") assert_select "nav.navbar-vertical", 0 end + + test "should redirect to adopter foster dashboard when updated" do + user = create(:adopter_fosterer, password: "123456") + sign_in user + + updated_params = {user: {first_name: "not the same name", current_password: "123456"}} + + put user_registration_url, params: updated_params + + assert_redirected_to adopter_fosterer_dashboard_index_url + end + + test "should redirect to staff dashboard when updated" do + user = create(:admin, password: "123456") + organization = user.organization + sign_in user + + updated_params = {user: {first_name: "Sean", current_password: "123456"}} + + put user_registration_url(script_name: "/#{organization.slug}"), params: updated_params + + assert_redirected_to staff_dashboard_index_url + end end