Skip to content

Commit

Permalink
Make Address support one word names
Browse files Browse the repository at this point in the history
Fixes issue solidusio#302

In the case that:
- SolidusSupport::combined_first_and_last_name_in_address returns true;
- Spree::Address::Name is not defined; and
- the given name argument to self.split_name is just one word (e.g
  "Bruce")

Then it would return the array of one element such as ['Bruce'].

lastname was assuming that there should be two elements and was calling
.last on the array, resulting in the same word as firstname. This made
the recipientName for example be "Bruce Bruce".
https://github.com/solidusio/solidus_paypal_braintree/blob/819bd3467e6f0c8ead8d855138ba88e10eb80a18/app/models/solidus_paypal_braintree/address.rb#L35

The #fullname method was added to fix the problem that if the given name
was only one word, then recipientName won't attempt to add a space
between the firstname and lastname's returned empty string. Otherwise,
it would cause for example: "Bruce ".
  • Loading branch information
RyanofWoods committed Dec 1, 2021
1 parent 819bd34 commit 795abb0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
9 changes: 7 additions & 2 deletions app/models/solidus_paypal_braintree/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def to_json(*_args)
postalCode: zipcode,
countryCode: country.iso,
phone: phone,
recipientName: "#{firstname} #{lastname}"
recipientName: fullname
}

if ::Spree::Config.address_requires_state && country.states_required
Expand All @@ -41,6 +41,10 @@ def to_json(*_args)
address_hash.to_json
end

def fullname
[firstname, lastname].select(&:present?).join(' ')
end

def firstname
if SolidusSupport.combined_first_and_last_name_in_address?
self.class.split_name(spree_address.name).first
Expand All @@ -51,7 +55,8 @@ def firstname

def lastname
if SolidusSupport.combined_first_and_last_name_in_address?
self.class.split_name(spree_address.name).last
split_names = self.class.split_name(spree_address.name)
split_names.size == 2 ? split_names.last : ''
else
spree_address.lastname
end
Expand Down
37 changes: 37 additions & 0 deletions spec/models/solidus_paypal_braintree/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,43 @@
end
end

describe "#fullname" do
subject { described_class.new(address).fullname }

let(:address) { instance_double('address', name: name) }

context 'when Solidus combines names but Spree::Address::Name is not defined' do
before do
allow(SolidusSupport).to receive(:combined_first_and_last_name_in_address?).and_return(true)
allow(described_class).to receive(:defined?).with(Spree::Address::Name).and_return(false)
end

context 'with a one word name' do
let(:name) { 'Bruce' }

it 'equals "Bruce"' do
expect(subject).to eq('Bruce')
end
end

context 'with a one word name but extra right padding' do
let(:name) { 'Bruce ' }

it 'equals "Bruce"' do
expect(subject).to eq('Bruce')
end
end

context "with a two word name" do
let(:name) { 'Bruce Wayne' }

it 'equals "Bruce Wayne"' do
expect(subject).to eq('Bruce Wayne')
end
end
end
end

describe '#to_json' do
subject(:address_json) { JSON.parse(described_class.new(spree_address).to_json) }

Expand Down

0 comments on commit 795abb0

Please sign in to comment.