From ab8bebc9b851859874cd05616fb97f131e505c1f Mon Sep 17 00:00:00 2001 From: Filippo Liverani Date: Fri, 10 Apr 2020 15:20:22 +0200 Subject: [PATCH] Remove deprecated Address fields from json representation 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. --- core/app/models/spree/address.rb | 10 ++++++++++ core/spec/models/spree/address_spec.rb | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/core/app/models/spree/address.rb b/core/app/models/spree/address.rb index 225383bdf61..0aa2a66139e 100644 --- a/core/app/models/spree/address.rb +++ b/core/app/models/spree/address.rb @@ -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 diff --git a/core/spec/models/spree/address_spec.rb b/core/spec/models/spree/address_spec.rb index 9eb66457424..3ff91ba1b7d 100644 --- a/core/spec/models/spree/address_spec.rb +++ b/core/spec/models/spree/address_spec.rb @@ -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 @@ -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)