Repository navigation
[User] Store first and last name separately in DB #13109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
manu-gurudath
wants to merge
31
commits into
main
Choose a base branch
from
mxg-store-first-and-last-name-separately
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
a3006e5
fix user stuff
manu-gurudath bc8771c
UI changes
manu-gurudath b6287e1
update schema
manu-gurudath f723e8f
Merge main into mxg-store-first-and-last-name-separately
manu-gurudath 99638ef
Remove redundant constraint migrations
manu-gurudath 6a0d620
Fix nil check for user full_name in user factory
manu-gurudath 84f37f6
Refactor user name handling: replace full_name with first_name in var…
manu-gurudath 3bce120
Refactor user name parsing: update user factory and migration to hand…
manu-gurudath e1bc9ae
Merge remote-tracking branch 'origin/main' into mxg-store-first-and-l…
manu-gurudath 8e9cef6
lint
manu-gurudath 28d8120
Refactor name parsing logic and update user model validations for ful…
manu-gurudath 6088847
lint
manu-gurudath b3e1bd8
Update first name in card grant response to include full name
manu-gurudath 0dd64bf
Update spec/factories/user_factory.rb
manu-gurudath ca72615
Update db/migrate/20260301000002_populate_first_and_last_names.rb
manu-gurudath 6785ba4
Update app/services/user_service/parse_name.rb
manu-gurudath 073f493
Update app/models/user.rb
manu-gurudath 812492e
Update app/models/user.rb
manu-gurudath c39e5ab
Update app/models/user.rb
manu-gurudath 580e703
Update app/services/user_service/parse_name.rb
manu-gurudath 45c00a9
Merge origin/main into mxg-store-first-and-last-name-separately
manu-gurudath dd63be9
Merge branch 'main' into mxg-store-first-and-last-name-separately
Luke-Oldenburg 71db97f
Merge branch 'main' into mxg-store-first-and-last-name-separately
manu-gurudath 568cd67
revive pr
manu-gurudath 07449f7
Merge branch 'mxg-store-first-and-last-name-separately' of https://gi…
manu-gurudath 905ca53
Update specs that still relied on full_name as the user name input
manu-gurudath 9f2b9cb
Use add_column instead of change_table in first/last name migration
manu-gurudath d1dcafb
Merge branch 'main' into mxg-store-first-and-last-name-separately
manu-gurudath d9fbaa8
fix merge conflicts
manu-gurudath fdfe631
fix name corruption
manu-gurudath 37847f1
fix bugs with name separation
manu-gurudath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "namae" | ||
|
|
||
| module UserService | ||
| # Parses a full name into first and last name components using the Namae gem. | ||
| # Handles complex names with particles (von, de, etc.) and suffixes (Jr., Sr., III, etc.) | ||
| class ParseName | ||
| attr_reader :given_name, :particle, :family_name, :suffix | ||
|
|
||
| def initialize(full_name:) | ||
| @full_name = full_name | ||
| @given_name = nil | ||
| @particle = nil | ||
| @family_name = nil | ||
| @suffix = nil | ||
| end | ||
|
|
||
| # Parse the full name and extract components | ||
| def run | ||
| return self if @full_name.blank? | ||
|
|
||
| parsed = Namae.parse(@full_name).first | ||
| return self if parsed.nil? | ||
|
|
||
| @given_name = parsed.given | ||
| @particle = parsed.particle | ||
| @family_name = parsed.family | ||
| @suffix = parsed.suffix | ||
|
|
||
| self | ||
| end | ||
|
|
||
| # Returns the formatted first name (given name + particle if present). | ||
| # Stores what should go in the database first_name column. | ||
| # Example: "von Braun" for "Wernher von Braun" | ||
|
manu-gurudath marked this conversation as resolved.
Resolved
|
||
| def first_name | ||
| return nil if @given_name.nil? && @family_name.nil? | ||
|
|
||
| parts = [@given_name, @particle].compact_blank | ||
| return @family_name if parts.empty? | ||
|
|
||
| parts.join(" ").presence | ||
| end | ||
|
|
||
| # Returns the formatted last name (family name + suffix if present). | ||
| # Stores what should go in the database last_name column. | ||
| # Example: "King Jr." for "Martin Luther King Jr." | ||
| def last_name | ||
| parts = [@family_name, @suffix].compact_blank | ||
| parts.empty? ? nil : parts.join(" ").presence | ||
| end | ||
|
manu-gurudath marked this conversation as resolved.
Resolved
|
||
|
|
||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
db/migrate/20260301000001_add_first_and_last_names_to_users.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class AddFirstAndLastNamesToUsers < ActiveRecord::Migration[8.0] | ||
| def change | ||
| add_column :users, :first_name, :string | ||
| add_column :users, :last_name, :string | ||
| end | ||
| end |
29 changes: 29 additions & 0 deletions
29
db/migrate/20260301000002_populate_first_and_last_names.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| class PopulateFirstAndLastNames < ActiveRecord::Migration[8.0] | ||
| BATCH_SIZE = 500 | ||
|
|
||
| def up | ||
| require "namae" | ||
|
|
||
| updates = [] | ||
|
|
||
| User.where.not(full_name: nil).select(:id, :full_name).find_each do |user| | ||
| namae = Namae.parse(user.full_name).first | ||
| updates << { | ||
| id: user.id, | ||
| first_name: [namae&.given, namae&.particle].compact_blank.join(" ").presence || namae&.family, | ||
| last_name: [namae&.family, namae&.suffix].compact_blank.join(" ").presence, | ||
| } | ||
|
|
||
| if updates.size >= BATCH_SIZE | ||
| User.upsert_all(updates, update_only: [:first_name, :last_name], unique_by: :id) | ||
| updates.clear | ||
| end | ||
| end | ||
|
|
||
| User.upsert_all(updates, update_only: [:first_name, :last_name], unique_by: :id) if updates.any? | ||
|
manu-gurudath marked this conversation as resolved.
Resolved
|
||
| end | ||
|
|
||
| def down | ||
| User.update_all(first_name: nil, last_name: nil) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.