Show name and email for deleted poll officer's user account

Avoid to raise an exception `Module::DelegationError' when trying to
show the name and/or email of a poll officer whose user account has
been deleted.
We'll show a message "User deleted" and "Email deleted" instead.
This commit is contained in:
Julian Herrero
2019-05-31 18:29:11 +02:00
parent 90c1d681d0
commit 1f76b25e2a
4 changed files with 37 additions and 1 deletions

View File

@@ -7,7 +7,13 @@ class Poll
validates :user_id, presence: true, uniqueness: true validates :user_id, presence: true, uniqueness: true
delegate :name, :email, to: :user def name
user&.name || I18n.t("shared.author_info.author_deleted")
end
def email
user&.email || I18n.t("shared.author_info.email_deleted")
end
def voting_days_assigned_polls def voting_days_assigned_polls
officer_assignments.voting_days.includes(booth_assignment: :poll). officer_assignments.voting_days.includes(booth_assignment: :poll).

View File

@@ -724,6 +724,7 @@ en:
to: "To" to: "To"
author_info: author_info:
author_deleted: User deleted author_deleted: User deleted
email_deleted: Email deleted
back: Go back back: Go back
check: Select check: Select
check_all: All check_all: All

View File

@@ -722,6 +722,7 @@ es:
to: "Hasta" to: "Hasta"
author_info: author_info:
author_deleted: Usuario eliminado author_deleted: Usuario eliminado
email_deleted: Email eliminado
back: Volver back: Volver
check: Seleccionar check: Seleccionar
check_all: Todos check_all: Todos

View File

@@ -2,6 +2,34 @@ require "rails_helper"
describe Poll::Officer do describe Poll::Officer do
describe "#name" do
let(:officer) { create(:poll_officer) }
it "returns user name if user is not deleted" do
expect(officer.name).to eq officer.user.name
end
it "returns 'User deleted' if user is deleted" do
officer.user.destroy
expect(officer.reload.name).to eq "User deleted"
end
end
describe "#email" do
let(:officer) { create(:poll_officer) }
it "returns user email if user is not deleted" do
expect(officer.email).to eq officer.user.email
end
it "returns 'Email deleted' if user is deleted" do
officer.user.destroy
expect(officer.reload.email).to eq "Email deleted"
end
end
describe "#voting_days_assigned_polls" do describe "#voting_days_assigned_polls" do
it "returns all polls with this officer assigned during voting days" do it "returns all polls with this officer assigned during voting days" do
officer = create(:poll_officer) officer = create(:poll_officer)