diff --git a/core/app/models/spree/country.rb b/core/app/models/spree/country.rb index d06fc3cd694..90ceff276e8 100644 --- a/core/app/models/spree/country.rb +++ b/core/app/models/spree/country.rb @@ -17,9 +17,11 @@ def self.default def self.available(restrict_to_zone: Spree::Config[:checkout_zone]) checkout_zone = Zone.find_by(name: restrict_to_zone) - return checkout_zone.country_list if checkout_zone.try(:kind) == 'country' - - all + if checkout_zone.try(:kind) == 'country' + checkout_zone.country_list + else + all + end end def <=>(other) diff --git a/core/app/models/spree/zone.rb b/core/app/models/spree/zone.rb index 3a74337ec64..438cbf6ad0a 100644 --- a/core/app/models/spree/zone.rb +++ b/core/app/models/spree/zone.rb @@ -86,11 +86,11 @@ def include?(address) # convenience method for returning the countries contained within a zone def country_list - @countries ||= case kind - when 'country' then zoneables - when 'state' then zoneables.collect(&:country) - else [] - end.flatten.compact.uniq + case kind + when 'country' then countries + when 'state' then Spree::Country.where(id: states.select(:id)) + else Spree::Country.none + end end def <=>(other) diff --git a/core/spec/models/spree/country_spec.rb b/core/spec/models/spree/country_spec.rb index 7166ee77a68..4ee2705ebea 100644 --- a/core/spec/models/spree/country_spec.rb +++ b/core/spec/models/spree/country_spec.rb @@ -43,24 +43,28 @@ context 'with no arguments' do it 'returns "Checkout Zone" countries' do + expect(described_class.available).to be_an(ActiveRecord::Relation) expect(described_class.available).to contain_exactly(united_states, canada) end end context 'setting nil as restricting zone' do it 'returns all countries' do + expect(described_class.available(restrict_to_zone: nil)).to be_an(ActiveRecord::Relation) expect(described_class.available(restrict_to_zone: nil)).to contain_exactly(united_states, canada, italy) end end context 'setting "Custom Zone" as restricting zone' do it 'returns "Custom Zone" countries' do + expect(described_class.available(restrict_to_zone: 'Custom Zone')).to be_an(ActiveRecord::Relation) expect(described_class.available(restrict_to_zone: 'Custom Zone')).to contain_exactly(united_states, italy) end end context 'setting "Checkout Zone" as restricting zone' do it 'returns "Checkout Zone" countries' do + expect(described_class.available(restrict_to_zone: 'Checkout Zone')).to be_an(ActiveRecord::Relation) expect(described_class.available(restrict_to_zone: 'Checkout Zone')).to contain_exactly(united_states, canada) end end diff --git a/core/spec/models/spree/zone_spec.rb b/core/spec/models/spree/zone_spec.rb index 1a41b8d6a07..bfe08a87af9 100644 --- a/core/spec/models/spree/zone_spec.rb +++ b/core/spec/models/spree/zone_spec.rb @@ -57,6 +57,7 @@ before { country_zone.members.create(zoneable: country) } it 'should return a list of countries' do + expect(country_zone.country_list).to be_a(ActiveRecord::Relation) expect(country_zone.country_list).to eq([country]) end end @@ -67,9 +68,19 @@ before { state_zone.members.create(zoneable: state) } it 'should return a list of countries' do + expect(state_zone.country_list).to be_a(ActiveRecord::Relation) expect(state_zone.country_list).to eq([state.country]) end end + + context "when zone doesn't consist of either countries nor states" do + let(:zone) { create(:zone, name: 'Zone') } + + it 'should return an empty list' do + expect(zone.country_list).to be_a(ActiveRecord::Relation) + expect(zone.country_list).to be_empty + end + end end context "#include?" do