Skip to content

Commit

Permalink
Remove deprecated Address fields from json representation
Browse files Browse the repository at this point in the history
When Spree::Config.use_combined_first_and_last_name_in_address
preference is set to true, no Address representation should contain any
reference to deprecated legacy name attributes.
  • Loading branch information
filippoliverani committed Apr 17, 2020
1 parent b44ae64 commit ab8bebc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/app/models/spree/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ def name=(value)
write_attribute(:lastname, name_from_value.last_name)
end

def as_json(options = {})
if Spree::Config.use_combined_first_and_last_name_in_address
super(options.merge(except: LEGACY_NAME_ATTRS)).tap do |hash|
hash['name'] = name
end
else
super
end
end

private

def validate_name
Expand Down
10 changes: 10 additions & 0 deletions core/spec/models/spree/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@

expect(address.name).to eq('')
end

it 'is included in json representation' do
address = Spree::Address.new(name: 'Jane Von Doe')

expect(address.as_json).to include('name' => 'Jane Von Doe')
end
end

context '#state_text' do
Expand Down Expand Up @@ -409,6 +415,10 @@
context 'deprecations' do
let(:address) { described_class.new }

specify 'json representation does not contain deprecated fields' do
expect(address.as_json).not_to include('firstname', 'lastname')
end

specify 'firstname is deprecated' do
expect(Spree::Deprecation).to receive(:warn).with(/firstname/, any_args)

Expand Down

0 comments on commit ab8bebc

Please sign in to comment.