Skip to content

Commit

Permalink
fix(Address): Set name from firstname and lastname on update
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tvdeyen committed Jan 7, 2022
1 parent c728183 commit a09c84c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/app/models/spree/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 45 additions & 1 deletion core/spec/models/spree/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }

Expand All @@ -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) }
Expand Down

0 comments on commit a09c84c

Please sign in to comment.