Commit a06410d9 authored by Aishwarya Subramanian's avatar Aishwarya Subramanian Committed by Lin Jen-Shin

Using before_save method instead of setter

Removed unused method for name setter method
parent e2251a09
......@@ -161,6 +161,8 @@ class User < ApplicationRecord
#
# Note: devise :validatable above adds validations for :email and :password
validates :name, presence: true, length: { maximum: 128 }
validates :first_name, length: { maximum: 255 }
validates :last_name, length: { maximum: 255 }
validates :email, confirmation: true
validates :notification_email, presence: true
validates :notification_email, devise_email: true, if: ->(user) { user.notification_email != user.email }
......@@ -881,7 +883,15 @@ class User < ApplicationRecord
end
def first_name
name.split.first unless name.blank?
read_attribute(:first_name) || begin
name.split(' ').first unless name.blank?
end
end
def last_name
read_attribute(:last_name) || begin
name.split(' ').drop(1).join(' ') unless name.blank?
end
end
def projects_limit_left
......
---
title: Add First and Last name columns to User model
merge_request: 31985
author:
type: added
# frozen_string_literal: true
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class AddFirstLastNameToUser < ActiveRecord::Migration[5.2]
# Set this constant to true if this migration requires downtime.
DOWNTIME = false
def change
add_column(:users, :first_name, :string, null: true, limit: 255)
add_column(:users, :last_name, :string, null: true, limit: 255)
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_08_15_093949) do
ActiveRecord::Schema.define(version: 2019_08_20_163320) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
......@@ -3511,6 +3511,8 @@ ActiveRecord::Schema.define(version: 2019_08_15_093949) do
t.text "note"
t.integer "roadmap_layout", limit: 2
t.integer "bot_type", limit: 2
t.string "first_name", limit: 255
t.string "last_name", limit: 255
t.index ["accepted_term_id"], name: "index_users_on_accepted_term_id"
t.index ["admin"], name: "index_users_on_admin"
t.index ["bot_type"], name: "index_users_on_bot_type"
......
......@@ -103,6 +103,14 @@ describe User do
it { is_expected.to validate_length_of(:name).is_at_most(128) }
end
describe 'first name' do
it { is_expected.to validate_length_of(:first_name).is_at_most(255) }
end
describe 'last name' do
it { is_expected.to validate_length_of(:last_name).is_at_most(255) }
end
describe 'username' do
it 'validates presence' do
expect(subject).to validate_presence_of(:username)
......@@ -678,6 +686,18 @@ describe User do
end
end
describe 'name getters' do
let(:user) { create(:user, name: 'Kane Martin William') }
it 'derives first name from full name, if not present' do
expect(user.first_name).to eq('Kane')
end
it 'derives last name from full name, if not present' do
expect(user.last_name).to eq('Martin William')
end
end
describe '#highest_role' do
let(:user) { create(:user) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment