diff --git a/core/app/models/spree/address.rb b/core/app/models/spree/address.rb index a924b507c79..70c4130825d 100644 --- a/core/app/models/spree/address.rb +++ b/core/app/models/spree/address.rb @@ -30,8 +30,9 @@ class Address < Spree::Base where(value_attributes(attributes)) end - def self.build_default - new(country: Spree::Country.default) + # @return [Address] an address with default attributes + def self.build_default(*args, &block) + where(country: Spree::Country.default).build(*args, &block) end # @return [Address] an equal address already in the database or a newly created one diff --git a/core/spec/models/spree/address_spec.rb b/core/spec/models/spree/address_spec.rb index fc90613fade..ed88bf66972 100644 --- a/core/spec/models/spree/address_spec.rb +++ b/core/spec/models/spree/address_spec.rb @@ -152,6 +152,29 @@ it "sets up a new record with Spree::Config[:default_country_iso]" do expect(Spree::Address.build_default.country).to eq default_country end + + it 'accepts other attributes' do + address = Spree::Address.build_default(first_name: 'Ryan') + + expect(address.country).to eq default_country + expect(address.first_name).to eq 'Ryan' + end + + it 'accepts a block' do + address = Spree::Address.build_default do |record| + record.first_name = 'Ryan' + end + + expect(address.country).to eq default_country + expect(address.first_name).to eq 'Ryan' + end + + it 'can override the country' do + another_country = build :country + address = Spree::Address.build_default(country: another_country) + + expect(address.country).to eq another_country + end end # Regression test for https://github.com/spree/spree/issues/1142