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

Fix Order#tax_address method #4429

Merged
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
Fix Order#tax_address method
Calling `#tax_address` on an order that has no address but also no
store associates raises the following error:

    undefined method `default_cart_tax_location' for nil:NilClass

Given that the `store` association is optional, and/or it may not
have been already set on the `Spree::Order` instance, we should
avoid raising an error.

A simple way to expose the issue is by entering the following
command in the Rails console:

    Spree::Order.new.tax_address
  • Loading branch information
spaghetticode committed Jun 22, 2022
commit f7f85bf5b9b4e3f9d39f434b8e89828d916e2295
2 changes: 1 addition & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def tax_address
ship_address
else
bill_address
end || store.default_cart_tax_location
end || store&.default_cart_tax_location
end

def updater
Expand Down
24 changes: 20 additions & 4 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -467,16 +467,32 @@ def merge!(other_order, user = nil)
context "when tax_using_ship_address is true" do
let(:tax_using_ship_address) { true }

it 'returns the stores default cart tax location' do
expect(subject).to eq(store.default_cart_tax_location)
context "when the order is associated with a store" do
it 'returns the stores default cart tax location' do
expect(subject).to eq(store.default_cart_tax_location)
end
end

context "when the order is not associated with a store" do
let(:store) { nil }

it { is_expected.to be_nil }
end
end

context "when tax_using_ship_address is not true" do
let(:tax_using_ship_address) { false }

it 'returns the stores default cart tax location' do
expect(subject).to eq(store.default_cart_tax_location)
context "when the order is associated with a store" do
it 'returns the stores default cart tax location' do
expect(subject).to eq(store.default_cart_tax_location)
end
end

context "when the order is not associated with a store" do
let(:store) { nil }

it { is_expected.to be_nil }
end
end
end
Expand Down