From 117763493b17494af1185e6591a46cb9fbe8c267 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 17 May 2018 14:51:48 +0800 Subject: [PATCH] Refactor `User#suggest_name`. * Rename `email` to `string` as variable can be an email, username or any valid string. --- app/models/user.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 27156250ac1..568886d418d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -286,15 +286,11 @@ class User < ActiveRecord::Base user end - def self.suggest_name(email) - return "" if email.blank? - if email[/\A[^@]+/].present? - # email provided - email[/\A[^@]+/].tr(".", " ").titleize - else - # username/name provided - email[/[^@]+\z/].tr(".", " ").titleize - end + def self.suggest_name(string) + return "" if string.blank? + local_part = string[/\A[^@]+/] + suggestion = local_part.present? ? local_part : string[/[^@]+\z/] + suggestion.tr(".", " ").titleize end def self.find_by_username_or_email(username_or_email)