diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index e0df2c2e6..06f148f84 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -41,13 +41,14 @@ class UsersController < ApplicationController flash[:notice] = I18n.t("devise.passwords.updated") if user_params.key?("password") redirect_to account_path else + user_name = @user.name&.possessive || @user.email.possessive case user_params[:active] when "false" @user.update!(confirmed_at: nil, sign_in_count: 0) - flash[:notice] = I18n.t("devise.activation.deactivated", user_name: @user.name.possessive) + flash[:notice] = I18n.t("devise.activation.deactivated", user_name:) when "true" @user.send_confirmation_instructions - flash[:notice] = I18n.t("devise.activation.reactivated", user_name: @user.name.possessive) + flash[:notice] = I18n.t("devise.activation.reactivated", user_name:) end redirect_to user_path(@user) end diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index 7831a45d9..047d89174 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -792,6 +792,19 @@ RSpec.describe UsersController, type: :request do expect { patch "/users/#{other_user.id}", headers:, params: } .to change { other_user.reload.active }.from(true).to(false) end + + context "when the user name is missing" do + let(:other_user) { FactoryBot.create(:user, name: nil, organisation: user.organisation) } + + before do + patch "/users/#{other_user.id}", headers:, params: + end + + it "uses the user's email" do + follow_redirect! + expect(page).to have_content(other_user.email) + end + end end context "and tries to activate deactivated user" do @@ -805,6 +818,19 @@ RSpec.describe UsersController, type: :request do expect { patch "/users/#{other_user.id}", headers:, params: } .to change { other_user.reload.active }.from(false).to(true) end + + context "when the user name is missing" do + let(:other_user) { FactoryBot.create(:user, name: nil, organisation: user.organisation) } + + before do + patch "/users/#{other_user.id}", headers:, params: + end + + it "uses the user's email" do + follow_redirect! + expect(page).to have_content(other_user.email) + end + end end end end