Fixing person's name construction

* When only has last_name, return last_name not diaspora_handle
* Strip contstructed name to avoid trailing or preceding space
when having only first or last name present
This commit is contained in:
Gonzalo Rodriguez 2011-10-18 01:46:11 -02:00
parent 5b40336113
commit aa1623a95f
3 changed files with 18 additions and 6 deletions

View file

@ -148,7 +148,7 @@ class Person < ActiveRecord::Base
end
def self.name_from_attrs(first_name, last_name, diaspora_handle)
first_name.blank? ? diaspora_handle : "#{first_name.to_s.strip} #{last_name.to_s.strip}"
first_name.blank? && last_name.blank? ? diaspora_handle : "#{first_name.to_s.strip} #{last_name.to_s.strip}".strip
end
def first_name

View file

@ -270,7 +270,7 @@ describe AspectsController do
describe '#edit' do
before do
eve.profile.first_name = nil
eve.profile.first_name = eve.profile.last_name = nil
eve.profile.save
eve.save

View file

@ -151,13 +151,25 @@ describe Person do
@profile = @person.profile
end
context 'with first name' do
it 'should return their name for name' do
Person.name_from_attrs(@profile.first_name, @profile.last_name, @profile.diaspora_handle).should match /#{@profile.first_name}|#{@profile.last_name}/
context 'with only first name' do
it 'should return their first name for name' do
Person.name_from_attrs(@profile.first_name, nil, @profile.diaspora_handle).should == @profile.first_name.strip
end
end
context 'without first name' do
context 'with only last name' do
it 'should return their last name for name' do
Person.name_from_attrs(nil, @profile.last_name, @profile.diaspora_handle).should == @profile.last_name.strip
end
end
context 'with both first and last name' do
it 'should return their composed name for name' do
Person.name_from_attrs(@profile.first_name, @profile.last_name, @profile.diaspora_handle).should == "#{@profile.first_name.strip} #{@profile.last_name.strip}"
end
end
context 'without first nor last name' do
it 'should display their diaspora handle' do
Person.name_from_attrs(nil, nil, @profile.diaspora_handle).should == @profile.diaspora_handle
end