Skip to content

Commit

Permalink
Add nickname (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilco375 authored Dec 7, 2022
1 parent edad2dc commit 74bd455
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
5 changes: 3 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ def users_json
'Authorization' => "Bearer #{api_token}"))['data']
end

def find_or_create_user(user_json) # rubocop:disable Metrics/AbcSize
def find_or_create_user(user_json) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
fields = user_json['attributes']
u = User.find_or_initialize_by(uid: user_json['id'])
u.name = User.full_name_from_attributes(fields['first_name'],
fields['last_name_prefix'],
fields['last_name'])
fields['last_name'],
fields['nickname'])
u.provider = 'amber_oauth2'
u.avatar_thumb_url = fields['avatar_thumb_url']
u.email = fields['email']
Expand Down
8 changes: 6 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ def self.from_omniauth(auth)

# :nocov:

def self.full_name_from_attributes(first_name, last_name_prefix, last_name)
[first_name, last_name_prefix, last_name].reject(&:blank?).join(' ')
def self.full_name_from_attributes(first_name, last_name_prefix, last_name, nickname)
full_name = first_name
full_name += " (#{nickname})" if nickname.present?
full_name += " #{last_name_prefix}" if last_name_prefix.present?
full_name += " #{last_name}" if last_name.present?
full_name
end

def self.calculate_credits
Expand Down
3 changes: 2 additions & 1 deletion lib/omniauth/strategies/amber_oauth2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def callback_url
def full_name(raw_info)
User.full_name_from_attributes(raw_info['attributes']['first_name'],
raw_info['attributes']['last_name_prefix'],
raw_info['attributes']['last_name'])
raw_info['attributes']['last_name'],
raw_info['attributes']['nickname'])
end

def groups_from_json(json)
Expand Down
6 changes: 3 additions & 3 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@

describe 'full_name_from_attributes' do
context 'when with all attributes' do
it { expect(described_class.full_name_from_attributes('first', 'middle', 'last')).to eq 'first middle last' }
it { expect(described_class.full_name_from_attributes('first', 'middle', 'last', 'nick')).to eq 'first (nick) middle last' }
end

context 'when without middle name' do
it { expect(described_class.full_name_from_attributes('first', '', 'last')).to eq 'first last' }
context 'when without middle name and nickname' do
it { expect(described_class.full_name_from_attributes('first', '', 'last', '')).to eq 'first last' }
end
end

Expand Down

0 comments on commit 74bd455

Please sign in to comment.