Skip to content

Commit 3c219be

Browse files
committed
Refactor user to handle given and family name
The full name field has been removed and replaced by a separate given name and family name, so we need to refactor the user model and various code paths to use the new fields.
1 parent dc85751 commit 3c219be

File tree

14 files changed

+103
-22
lines changed

14 files changed

+103
-22
lines changed

app/controllers/users/accounts_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ def set_user
5252
end
5353

5454
def user_params
55-
params.require(:user).permit(:full_name, :email, :registration)
55+
params.require(:user).permit(
56+
:email,
57+
:family_name,
58+
:given_name,
59+
:registration
60+
)
5661
end
5762
end
5863
end

app/models/user.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ class User < ApplicationRecord
4848
has_and_belongs_to_many :teams
4949

5050
encrypts :email, deterministic: true
51-
encrypts :full_name
51+
encrypts :family_name, :given_name
52+
53+
validates :family_name, :given_name, presence: true, length: { maximum: 255 }
5254

53-
validates :full_name, presence: true, length: { maximum: 255 }
5455
validates :registration, length: { maximum: 255 }
5556
validates :email,
5657
presence: true,
@@ -63,6 +64,10 @@ class User < ApplicationRecord
6364
scope :recently_active,
6465
-> { where(last_sign_in_at: 1.week.ago..Time.current) }
6566

67+
def full_name
68+
[given_name, family_name].join(" ")
69+
end
70+
6671
def team
6772
# TODO: Update the app to properly support multiple teams per user
6873
teams.first
@@ -74,7 +79,9 @@ def self.find_or_create_user_from_cis2_oidc(userinfo)
7479
provider: userinfo[:provider],
7580
uid: userinfo[:uid]
7681
)
77-
user.full_name = userinfo[:info][:name]
82+
83+
user.family_name = userinfo[:extra][:raw_info][:family_name]
84+
user.given_name = userinfo[:extra][:raw_info][:given_name]
7885
user.email = userinfo[:info][:email]
7986
user.password = Devise.friendly_token if user.new_record?
8087

app/views/users/accounts/show.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
<%= form_for @user, url: users_account_path(@user), method: :patch do |f| %>
44
<%= f.govuk_error_summary %>
55

6-
<%= f.govuk_text_field :full_name, label: { text: "Full name" } %>
6+
<%= f.govuk_text_field :given_name, label: { text: "Given name" } %>
7+
<%= f.govuk_text_field :family_name, label: { text: "Family name" } %>
8+
79
<%= f.govuk_text_field :email, label: { text: "Email address" } %>
810
<%= f.govuk_text_field :registration,
911
width: 10,

config/locales/en.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,12 @@ en:
381381
invalid: Enter a valid email address, such as j.doe@gmail.com
382382
taken: This email address is already in use
383383
too_long: Enter an email address that is less than 255 characters long
384-
full_name:
385-
blank: Enter your full name
386-
too_long: Enter a full name that is less than 255 characters long
384+
family_name:
385+
blank: Enter your family name
386+
too_long: Enter a family name that is less than 255 characters long
387+
given_name:
388+
blank: Enter your given name
389+
too_long: Enter a given name that is less than 255 characters long
387390
ods_code:
388391
blank: Enter an ODS code
389392
password:

db/seeds.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
User.find_by(email: "nurse.joy@example.com") ||
1616
FactoryBot.create(
1717
:user,
18-
full_name: "Nurse Joy",
18+
family_name: "Joy",
19+
given_name: "Nurse",
1920
email: "nurse.joy@example.com",
2021
password: "nurse.joy@example.com"
2122
)

spec/components/app_activity_log_component_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
)
2929
end
3030
let(:component) { described_class.new(patient_session) }
31-
let(:user) { create(:user, teams: [team], full_name: "Nurse Joy") }
31+
let(:user) do
32+
create(:user, teams: [team], family_name: "Joy", given_name: "Nurse")
33+
end
3234
let(:campaign) { create(:campaign, :hpv, team:) }
3335
let(:location) { create(:location, :school, name: "Hogwarts") }
3436
let(:session) { create(:session, campaign:, location:) }

spec/components/app_timestamped_entry_component_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
text: "Summary of a thing",
2929
timestamp: Time.zone.local(2024, 2, 17, 12, 23, 0),
3030
recorded_by:
31-
create(:user, full_name: "Test User", email: "test@example.com")
31+
create(
32+
:user,
33+
family_name: "User",
34+
given_name: "Test",
35+
email: "test@example.com"
36+
)
3237
)
3338
end
3439

spec/components/app_triage_notes_component_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Timecop.freeze(Time.zone.local(2023, 12, 4, 10, 4)) { example.run }
2020
end
2121

22-
let(:user) { create(:user, full_name: "Joe Gear") }
22+
let(:user) { create(:user, family_name: "Gear", given_name: "Joe") }
2323

2424
before do
2525
create(

spec/components/previews/app_consent_summary_component_preview.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ def multiple_responses
6161
recorded_by:
6262
build(
6363
:user,
64-
full_name: "Nurse Joy",
64+
family_name: "Joy",
65+
given_name: "Nurse",
6566
email: "nurse.joy@example.com"
6667
)
6768
}

spec/factories/users.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
#
3636
FactoryBot.define do
3737
factory :user do
38-
sequence(:full_name) { |n| "Test User #{n}" }
38+
sequence(:family_name) { |n| "User #{n}" }
39+
given_name { "Test" }
40+
3941
sequence(:email) { |n| "test-#{n}@example.com" }
4042
sequence(:teams) { [Team.first || create(:team)] }
4143
password { "power overwhelming!" } # avoid a password that was found in a data breach

0 commit comments

Comments
 (0)