diff --git a/app/models/user.rb b/app/models/user.rb index 5bb54c08a..1b2bb91d5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -332,10 +332,32 @@ Your #{config.product_name} Team end def check_name - if self.firstname && (!self.lastname || self.lastname == '') + + if !self.firstname.empty? && self.lastname.empty? + + # Lastname, Firstname + scan = self.firstname.scan(/, /) + if scan[0] + name = self.firstname.split(', ', 2) + self.lastname = name[0] + self.firstname = name[1] + return + end + + # Firstname Lastname name = self.firstname.split(' ', 2) self.firstname = name[0] self.lastname = name[1] + return + + # -no name- firstname.lastname@example.com + elsif self.firstname.empty? && self.lastname.empty? && !self.email.empty? + scan = self.email.scan(/^(.+?)\.(.+?)\@.+?$/) + if scan[0] + self.firstname = scan[0][0].capitalize + self.lastname = scan[0][1].capitalize + end + end end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb new file mode 100644 index 000000000..9e1b13ae5 --- /dev/null +++ b/test/unit/user_test.rb @@ -0,0 +1,96 @@ +# encoding: utf-8 +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + test 'user' do + tests = [ + { + :create => { + :firstname => 'Firstname', + :lastname => 'Lastname', + :email => 'some@example.com', + :login => 'some@example.com', + :created_by_id => 1, + }, + :create_verify => { + :firstname => 'Firstname', + :lastname => 'Lastname', + :email => 'some@example.com', + :login => 'some@example.com', + }, + }, + { + :create => { + :firstname => 'Firstname Lastname', + :lastname => '', + :email => 'some@example.com', + :login => 'some@example.com', + :created_by_id => 1, + }, + :create_verify => { + :firstname => 'Firstname', + :lastname => 'Lastname', + :email => 'some@example.com', + :login => 'some@example.com', + }, + }, + { + :create => { + :firstname => 'Lastname, Firstname', + :lastname => '', + :email => 'some@example.com', + :login => 'some@example.com', + :created_by_id => 1, + }, + :create_verify => { + :firstname => 'Firstname', + :lastname => 'Lastname', + :email => 'some@example.com', + :login => 'some@example.com', + }, + }, + { + :create => { + :firstname => '', + :lastname => '', + :email => 'firstname.lastname@example.com', + :login => 'login', + :created_by_id => 1, + }, + :create_verify => { + :firstname => 'Firstname', + :lastname => 'Lastname', + :email => 'firstname.lastname@example.com', + :login => 'login', + }, + }, + { + :create => { + :firstname => '', + :lastname => '', + :email => 'FIRSTNAME.lastname@example.com', + :login => 'login', + :created_by_id => 1, + }, + :create_verify => { + :firstname => 'Firstname', + :lastname => 'Lastname', + :email => 'firstname.lastname@example.com', + :login => 'login', + }, + }, + ] + + tests.each { |test| + + user = User.create( test[:create] ) + + test[:create_verify].each { |key, value| + assert_equal( user[key], value ) + } + + user.destroy + } + end +end +