From a09c84ca659251b5889277666140cf3b9ffb069b Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 16 Dec 2021 12:34:46 +0100 Subject: [PATCH] fix(Address): Set name from firstname and lastname on update If use_combined_first_and_last_name_in_address is false (default) and firstname and lastname are given in the changeset (ie. from a form) we need to make sure that name is set from it, otherwise data will be inconsistent. Closes #4094 --- core/app/models/spree/address.rb | 1 + core/spec/models/spree/address_spec.rb | 46 +++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/core/app/models/spree/address.rb b/core/app/models/spree/address.rb index ad7d82d739a..17394e3ce8f 100644 --- a/core/app/models/spree/address.rb +++ b/core/app/models/spree/address.rb @@ -84,6 +84,7 @@ def self.immutable_merge(existing_address, new_attributes) # @return [Hash] hash of attributes contributing to value equality with optional merge def self.value_attributes(base_attributes, merge_attributes = {}) base = base_attributes.stringify_keys.merge(merge_attributes.stringify_keys) + base.delete("name") unless Spree::Config.use_combined_first_and_last_name_in_address name_from_attributes = Spree::Address::Name.from_attributes(base) if base['firstname'].presence || base['first_name'].presence diff --git a/core/spec/models/spree/address_spec.rb b/core/spec/models/spree/address_spec.rb index d9e66302b26..a28fae96214 100644 --- a/core/spec/models/spree/address_spec.rb +++ b/core/spec/models/spree/address_spec.rb @@ -109,7 +109,7 @@ end end - context "when saving a record" do + context "when creating a record" do context "when the `name` field is not explicitly set" do subject { build :address, name: nil, firstname: 'John', lastname: 'Doe' } @@ -119,6 +119,50 @@ end end + context "when updating a record" do + let(:address) { create(:address, firstname: "John", lastname: "Doe") } + + context "if use_combined_first_and_last_name_in_address is set to false (default)" do + before do + allow(Spree::Config).to receive(:use_combined_first_and_last_name_in_address) { false } + end + + context "and the `name` attribute is not in changeset" do + it "sets `name` from `firstname` and `lastname`" do + new_address = described_class.immutable_merge(address, firstname: "Jane", lastname: "Fonda") + expect(new_address.read_attribute(:name)).to eq("Jane Fonda") + end + end + + context "and the `name` attribute is in changeset" do + it "does not update name" do + new_address = described_class.immutable_merge(address, name: "Jane Fonda") + expect(new_address.read_attribute(:name)).to eq("John Von Doe") + end + end + end + + context "if use_combined_first_and_last_name_in_address is set to true" do + before do + allow(Spree::Config).to receive(:use_combined_first_and_last_name_in_address) { true } + end + + context "and the `name` attribute is not in changeset" do + it "keeps old name" do + new_address = described_class.immutable_merge(address, firstname: "Jane", lastname: "Fonda") + expect(new_address.read_attribute(:name)).to eq("John Von Doe") + end + end + + context "and the `name` attribute is in changeset" do + it "updates name" do + new_address = described_class.immutable_merge(address, name: "Jane Fonda") + expect(new_address.read_attribute(:name)).to eq("Jane Fonda") + end + end + end + end + context ".build_default" do context "no user given" do let!(:default_country) { create(:country) }