Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.11 fix(Address): Set name from firstname and lastname on update #4224

Merged
merged 1 commit into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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