Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix NoMethodError when user name is nil in full_name method (#13158)

Jawaria Waris2b33cb6092fd
diff --git a/core/app/models/concerns/spree/user_methods.rb b/core/app/models/concerns/spree/user_methods.rb
index 8485118665..094c060f88 100644
--- a/core/app/models/concerns/spree/user_methods.rb
+++ b/core/app/models/concerns/spree/user_methods.rb
@@ -154,7 +154,7 @@ module Spree
# Returns the full name of the user
# @return [String]
def full_name
- name.full
+ name&.full
end
private
diff --git a/core/spec/models/spree/user_spec.rb b/core/spec/models/spree/user_spec.rb
index 668f62cd13..1d4d1a509d 100644
--- a/core/spec/models/spree/user_spec.rb
+++ b/core/spec/models/spree/user_spec.rb
@@ -18,10 +18,21 @@ describe Spree::LegacyUser, type: :model do # rubocop:disable RSpec/MultipleDesc
end
describe '#full_name' do
- let(:user) { create(:user, first_name: 'John', last_name: 'Doe') }
+ context 'when names are present' do
+ let(:user) { create(:user, first_name: 'John', last_name: 'Doe') }
- it 'returns the full name of the user' do
- expect(user.full_name).to eq('John Doe')
+ it 'returns the full name of the user' do
+ expect(user.full_name).to eq('John Doe')
+ end
+ end
+
+ context 'when both first and last names are nil' do
+ let(:user) { create(:user, first_name: nil, last_name: nil) }
+
+ it 'does not raise error and returns nil' do
+ expect { user.full_name }.not_to raise_error
+ expect(user.full_name).to be_nil
+ end
end
end