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

Let Address#build_default accept args and block #3429

Merged
merged 1 commit into from
Nov 20, 2019
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
5 changes: 3 additions & 2 deletions core/app/models/spree/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions core/spec/models/spree/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down