Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #315 from SumOfUs/first-last-full-name-connection
Browse files Browse the repository at this point in the history
Hack to make full_name and first/last name play together nicely.
  • Loading branch information
Eric Boersma committed Jan 13, 2016
2 parents c4ac680 + 4220848 commit b3bf7c9
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def action_params
end

def base_params
%w{page_id form_id}
%w{page_id form_id name}
end

def fields
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/braintree_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def find_or_create_user
customer.update(
card_vault_token: result.customer.payment_methods.first.token,
customer_id: result.customer.id,
first_name: user[:firstname] || user[:name],
first_name: user[:first_name] || user[:name],
last_name: user[:last_name],
card_last_4: result.customer.payment_methods.first.last_4
)
Expand Down
10 changes: 10 additions & 0 deletions app/models/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ def self.find_from_request(akid: nil, id: nil)
end
id.present? ? find_by(id: id) : nil
end

def name
"#{first_name} #{last_name}".strip
end

def name=(full_name)
splitter = NameSplitter.new(full_name: full_name)
self.first_name = splitter.first_name
self.last_name = splitter.last_name
end
end
3 changes: 3 additions & 0 deletions app/services/action_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def previous_action
def member
return @user if @user.present?
@user = Member.find_or_create_by(email: @params[:email])
if @params.has_key? :name
@user.name = @params[:name]
end
@user.assign_attributes(filtered_params)
@user.save if @user.changed
@user
Expand Down
37 changes: 37 additions & 0 deletions app/services/name_splitter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class NameSplitter
def initialize(full_name:)
@full_name = full_name
@split_name = @full_name.split
end

def first_name
@first_name || find_first_name
end

def last_name
@last_name || find_last_name
end

private
def find_first_name
@first_name =
if @split_name.length == 1
@full_name
elsif @split_name.length == 2
@split_name[0]
else
@split_name.slice(0, (@split_name.length / 2)).join(' ') #integer division, so will always round to lower whole
end
end

def find_last_name
@last_name =
if @split_name.length == 1
''
elsif @split_name.length == 2
@split_name[1]
else
@split_name.slice((@split_name.length/ 2), @split_name.length).join(' ') #integer division, so will always round to lower whole
end
end
end
50 changes: 50 additions & 0 deletions spec/models/member_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'rails_helper'

describe Member do
let(:first_name) { 'Emilio' }
let(:last_name) { 'Estevez' }
let(:full_name) { "#{first_name} #{last_name}" }
let(:unicode_first) { 'Éöíñ'}
let(:unicode_last) { 'Ńūñèž' }
let(:unicode_full) { "#{unicode_first} #{unicode_last}" }
let(:chinese_first) { '台'}
let(:chinese_last) { '北' }
let(:chinese_full) { "#{chinese_first} #{chinese_last}"}

it 'correctly joins first and last name' do
member = Member.new
member.first_name = first_name
member.last_name = last_name
expect(member.name).to eq(full_name)
end

it 'correctly splits full_name into first and last name' do
member = Member.new
member.name = full_name
member.save
new_member = Member.find(member.id)
expect(new_member.first_name).to eq(first_name)
expect(new_member.last_name).to eq(last_name)
expect(new_member.name).to eq(full_name)
end

it 'correctly handles unicode characters' do
member = Member.new
member.name = unicode_full
member.save
new_member = Member.find(member.id)
expect(new_member.first_name).to eq(unicode_first)
expect(new_member.last_name).to eq(unicode_last)
expect(new_member.name).to eq(unicode_full)
end

it 'correctly handles high-value unicode characters' do
member = Member.new
member.name = chinese_full
member.save
new_member = Member.find(member.id)
expect(new_member.first_name).to eq(chinese_first)
expect(new_member.last_name).to eq(chinese_last)
expect(new_member.name).to eq(chinese_full)
end
end
63 changes: 63 additions & 0 deletions spec/services/name_splitter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'spec_helper'

describe NameSplitter do
it 'splits names in two if there are only two elements' do
first_name = 'Eric'
last_name = 'Boersma'
splitter = NameSplitter.new(full_name: "#{first_name} #{last_name}")
expect(splitter.first_name).to eq(first_name)
expect(splitter.last_name).to eq(last_name)
end

it 'splits the names in half if there are an even number of names given' do
first_name = 'John Jacob'
last_name = 'Jingleheimer Schmidt'
splitter = NameSplitter.new(full_name: "#{first_name} #{last_name}")
expect(splitter.first_name).to eq(first_name)
expect(splitter.last_name).to eq(last_name)
end

it 'correctly handles single name full names' do
first_name = 'Cher'
last_name = ''
splitter = NameSplitter.new(full_name: "#{first_name}")
expect(splitter.first_name).to eq(first_name)
expect(splitter.last_name).to eq(last_name)
end

it 'correctly handles names with three provided names' do
first_name = 'Jennifer'
last_name = 'Jason Leigh'
splitter = NameSplitter.new(full_name: "#{first_name} #{last_name}")
expect(splitter.first_name).to eq(first_name)
expect(splitter.last_name).to eq(last_name)
end

it 'correctly handles names with five provided names' do
first_name = 'Charles Philip'
last_name = 'Arthur George Mountbatten-Windsor'
splitter = NameSplitter.new(full_name: "#{first_name} #{last_name}")
expect(splitter.first_name).to eq(first_name)
expect(splitter.last_name).to eq(last_name)
end

it 'correctly handles names with six provided names' do
first_name = 'Seal Henry Olusegun'
last_name = 'Olumide Adeola Samuel'
splitter = NameSplitter.new(full_name: "#{first_name} #{last_name}")
expect(splitter.first_name).to eq(first_name)
expect(splitter.last_name).to eq(last_name)
end

it 'does not group common name prefixes' do
# This is an explicit recognition of a place where this class doesn't effectively handle an edge case of
# a name which doesn't follow conventional Anglo naming standards. Hopefully, some day this test can be deleted,
# but for now it stands as a recognition that the proper treatment of these names is not included in the
# expected functionality of this class at this time.
first_name = 'Josefina de los Sagrados'
last_name = 'Corazones Fernández del Solar'
splitter = NameSplitter.new(full_name: "#{first_name} #{last_name}")
expect(splitter.first_name).to eq(first_name)
expect(splitter.last_name).to eq(last_name)
end
end

0 comments on commit b3bf7c9

Please sign in to comment.