Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Add default country kind to spree_zones db table + specs reflections

Nathaniel Nande0f66998537fe
diff --git a/api/app/serializers/spree/v2/storefront/country_serializer.rb b/api/app/serializers/spree/v2/storefront/country_serializer.rb
index e0d14f440e..75b222e343 100644
--- a/api/app/serializers/spree/v2/storefront/country_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/country_serializer.rb
@@ -13,7 +13,7 @@ module Spree
has_many :states, if: proc { |_record, params| params && params[:include_states] }
- attribute :checkout_zone_applicable_states, if: proc { |_record, params| params && params[:current_store] } do |object, params|
+ attribute :checkout_zone_applicable_states, if: proc { |_record, params| params && params[:current_store].present? } do |object, params|
current_store = params[:current_store]
current_store.states_available_for_checkout(object)
diff --git a/core/db/migrate/20201127084048_add_default_country_kind_to_spree_zones.rb b/core/db/migrate/20201127084048_add_default_country_kind_to_spree_zones.rb
new file mode 100644
index 0000000000..ee33416787
--- /dev/null
+++ b/core/db/migrate/20201127084048_add_default_country_kind_to_spree_zones.rb
@@ -0,0 +1,5 @@
+class AddDefaultCountryKindToSpreeZones < ActiveRecord::Migration[6.0]
+ def change
+ change_column_default(:spree_zones, :kind, :state)
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/zone_factory.rb b/core/lib/spree/testing_support/factories/zone_factory.rb
index bb7707d610..72d7e09980 100644
--- a/core/lib/spree/testing_support/factories/zone_factory.rb
+++ b/core/lib/spree/testing_support/factories/zone_factory.rb
@@ -1,24 +1,27 @@
FactoryBot.define do
- factory :global_zone, class: Spree::Zone do
- sequence(:name) { |n| "GlobalZone_#{n}" }
- description { generate(:random_string) }
- zone_members do |proxy|
- zone = proxy.instance_eval { @instance }
- Spree::Country.all.map do |c|
- Spree::ZoneMember.create(zoneable: c, zone: zone)
- end
- end
- end
-
factory :zone, class: Spree::Zone do
name { generate(:random_string) }
description { generate(:random_string) }
factory :zone_with_country do
+ kind { :country }
+
zone_members do |proxy|
zone = proxy.instance_eval { @instance }
- country = create(:country)
- [Spree::ZoneMember.create(zoneable: country, zone: zone)]
+
+ [Spree::ZoneMember.create(zoneable: create(:country), zone: zone)]
+ end
+
+ factory :global_zone, class: Spree::Zone do
+ sequence(:name) { |n| "GlobalZone_#{n}" }
+
+ zone_members do |proxy|
+ zone = proxy.instance_eval { @instance }
+
+ Spree::Country.all.map do |country|
+ Spree::ZoneMember.where(zoneable: country, zone: zone).first_or_create
+ end
+ end
end
end
end
diff --git a/core/spec/helpers/base_helper_spec.rb b/core/spec/helpers/base_helper_spec.rb
index 8a6171d399..4a73534ade 100644
--- a/core/spec/helpers/base_helper_spec.rb
+++ b/core/spec/helpers/base_helper_spec.rb
@@ -15,7 +15,7 @@ describe Spree::BaseHelper, type: :helper do
context 'with checkout zone assigned to the store' do
before do
Spree::Config[:checkout_zone] = nil
- @zone = create(:zone, name: 'No Limits')
+ @zone = create(:zone, name: 'No Limits', kind: 'country')
@zone.members.create(zoneable: country)
current_store.update(checkout_zone_id: @zone.id)
end
@@ -40,7 +40,7 @@ describe Spree::BaseHelper, type: :helper do
context 'with a checkout zone defined' do
context 'checkout zone is of type country' do
before do
- @country_zone = create(:zone, name: 'CountryZone')
+ @country_zone = create(:zone, name: 'CountryZone', kind: 'country')
@country_zone.members.create(zoneable: country)
Spree::Config[:checkout_zone] = @country_zone.name
end
diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb
index 095538889c..80ff7f8c0c 100644
--- a/core/spec/models/spree/store_spec.rb
+++ b/core/spec/models/spree/store_spec.rb
@@ -115,4 +115,151 @@ describe Spree::Store, type: :model do
end
end
end
+
+ shared_context 'with checkout zone set' do
+ let!(:country1) { create(:country) }
+ let!(:country2) { create(:country) }
+
+ let!(:state1) { create(:state, country: country1) }
+ let!(:state2) { create(:state, country: country2) }
+
+ let(:zone) do
+ create(:zone, kind: 'country').tap do |zone|
+ zone.members.create(zoneable: country1)
+ zone.members.create(zoneable: country2)
+ end
+ end
+
+ before { subject.update(checkout_zone: zone) }
+ end
+
+ shared_context 'with checkout zone not set' do
+ before { subject.update(checkout_zone: nil) }
+ end
+
+ shared_context 'with default checkout zone set' do
+ let!(:country3) { create(:country) }
+
+ let!(:state3) { create(:state, country: country3) }
+
+ let(:default_zone) do
+ create(:zone, kind: 'country').tap do |zone|
+ zone.members.create(zoneable: country3)
+ end
+ end
+
+ before { allow(Spree::Zone).to receive(:default_checkout_zone) { default_zone } }
+ end
+
+ shared_context 'with default checkout zone not set' do
+ before { allow(Spree::Zone).to receive(:default_checkout_zone) { nil } }
+ end
+
+ describe '#countries_available_for_checkout' do
+ subject { create(:store) }
+
+ context do
+ include_context 'with checkout zone set'
+
+ it 'returns country list for checkout zone' do
+ expect(subject.countries_available_for_checkout).to eq [country1, country2]
+ end
+ end
+
+ context do
+ include_context 'with checkout zone not set'
+
+ context do
+ include_context 'with default checkout zone set'
+
+ it 'returns country list for default checkout zone' do
+ expect(subject.countries_available_for_checkout).to eq [country3]
+ end
+ end
+
+ context do
+ include_context 'with default checkout zone not set'
+
+ it 'returns list of all countries' do
+ checkout_available_countries_ids = subject.countries_available_for_checkout.pluck(:id)
+ all_countries_ids = Spree::Country.all.pluck(:id)
+
+ expect(checkout_available_countries_ids).to eq(all_countries_ids)
+ end
+ end
+ end
+ end
+
+ describe '#states_available_for_checkout' do
+ context do
+ include_context 'with checkout zone set'
+
+ it 'returns states list for checkout zone' do
+ expect(subject.states_available_for_checkout(country1)).to eq [state1]
+ expect(subject.states_available_for_checkout(country2)).to eq [state2]
+ end
+ end
+
+ context do
+ include_context 'with checkout zone not set'
+
+ context do
+ include_context 'with default checkout zone set'
+
+ it 'returns states list for default checkout zone' do
+ expect(subject.states_available_for_checkout(country3)).to eq [state3]
+ end
+ end
+
+ context do
+ include_context 'with default checkout zone not set'
+
+ let(:country_with_states) do
+ create(:country).tap do |country|
+ country.states << create(:state)
+ end
+ end
+
+ it 'returns list of states associated to country' do
+
+ checkout_available_states_ids3 = subject.states_available_for_checkout(country_with_states).pluck(:id)
+ all_countries_ids = country_with_states.states.pluck(:id)
+
+ expect(checkout_available_states_ids3).to eq(all_countries_ids)
+ end
+ end
+ end
+ end
+
+ describe '#checkout_zone_or_default' do
+ subject { described_class.new }
+
+ context do
+ include_context 'with checkout zone set'
+
+ it 'returns checkout zone' do
+ expect(subject.checkout_zone_or_default).to eq zone
+ end
+ end
+
+ context do
+ include_context 'with checkout zone not set'
+
+ context do
+ include_context 'with default checkout zone set'
+
+ it 'returns default checkout zone' do
+ expect(subject.checkout_zone_or_default).to eq default_zone
+ end
+ end
+
+ context do
+ include_context 'with default checkout zone not set'
+
+ it 'returns nil' do
+ expect(subject.checkout_zone_or_default).to be_nil
+ end
+ end
+ end
+ end
end
diff --git a/core/spec/models/spree/tax_rate_spec.rb b/core/spec/models/spree/tax_rate_spec.rb
index 2114832a4b..f48d3ed1b2 100644
--- a/core/spec/models/spree/tax_rate_spec.rb
+++ b/core/spec/models/spree/tax_rate_spec.rb
@@ -19,7 +19,7 @@ describe Spree::TaxRate, type: :model do
context 'when there is no default tax zone' do
before do
- @zone = create(:zone, name: 'Country Zone', default_tax: false, zone_members: [])
+ @zone = create(:zone, name: 'Country Zone', kind: 'country', default_tax: false, zone_members: [])
@zone.zone_members.create(zoneable: country)
end
@@ -61,7 +61,7 @@ describe Spree::TaxRate, type: :model do
context 'when the tax_zone is contained within a rate zone' do
before do
- sub_zone = create(:zone, name: 'State Zone', zone_members: [])
+ sub_zone = create(:zone, name: 'State Zone', kind: 'state', zone_members: [])
sub_zone.zone_members.create(zoneable: create(:state, country: country))
allow(order).to receive_messages tax_zone: sub_zone
@rate = Spree::TaxRate.create(
@@ -82,7 +82,7 @@ describe Spree::TaxRate, type: :model do
subject { Spree::TaxRate.match(order.tax_zone) }
before do
- @zone = create(:zone, name: 'Country Zone', default_tax: true, zone_members: [])
+ @zone = create(:zone, name: 'Country Zone', kind: 'country', default_tax: true, zone_members: [])
@zone.zone_members.create(zoneable: country)
end
@@ -113,7 +113,7 @@ describe Spree::TaxRate, type: :model do
context 'when the order has a different tax zone' do
before do
- allow(order).to receive_messages tax_zone: create(:zone, name: 'Other Zone')
+ allow(order).to receive_messages tax_zone: create(:zone, kind: 'country', name: 'Other Zone')
end
context 'when the tax is a VAT' do
@@ -202,9 +202,9 @@ describe Spree::TaxRate, type: :model do
let(:germany) { create :country, name: 'Germany' }
let(:india) { create :country, name: 'India' }
let(:france) { create :country, name: 'France' }
- let(:france_zone) { create :zone_with_country, name: 'France Zone' }
- let(:germany_zone) { create :zone_with_country, name: 'Germany Zone', default_tax: true }
- let(:india_zone) { create :zone_with_country, name: 'India' }
+ let(:france_zone) { create :zone_with_country, kind: 'country', name: 'France Zone' }
+ let(:germany_zone) { create :zone_with_country, kind: 'country', name: 'Germany Zone', default_tax: true }
+ let(:india_zone) { create :zone_with_country, kind: 'country', name: 'India' }
let(:moss_category) { Spree::TaxCategory.create(name: 'Digital Goods') }
let(:normal_category) { Spree::TaxCategory.create(name: 'Analogue Goods') }
let(:eu_zone) { create(:zone, name: 'EU') }
@@ -379,7 +379,7 @@ describe Spree::TaxRate, type: :model do
describe '#adjust' do
before do
@country = create(:country)
- @zone = create(:zone, name: 'Country Zone', default_tax: true, zone_members: [])
+ @zone = create(:zone, name: 'Country Zone', kind: 'country', default_tax: true, zone_members: [])
@zone.zone_members.create(zoneable: @country)
@category = Spree::TaxCategory.create name: 'Taxable Foo'
@category2 = Spree::TaxCategory.create(name: 'Non Taxable')
diff --git a/core/spec/models/spree/zone_spec.rb b/core/spec/models/spree/zone_spec.rb
index 746123a025..054d2e6b46 100644
--- a/core/spec/models/spree/zone_spec.rb
+++ b/core/spec/models/spree/zone_spec.rb
@@ -54,7 +54,7 @@ describe Spree::Zone, type: :model do
context 'when there are two qualified zones with same member type' do
let(:address) { create(:address, country: country, state: state) }
- let(:second_zone) { create(:zone, name: 'SecondZone') }
+ let(:second_zone) { create(:zone, name: 'SecondZone', kind: 'country') }
before { second_zone.members.create(zoneable: country) }
@@ -172,165 +172,172 @@ describe Spree::Zone, type: :model do
let(:state2) { create(:state) }
let(:state3) { create(:state) }
- before do
- @source = create(:zone, name: 'source', zone_members: [])
- @target = create(:zone, name: 'target', zone_members: [])
- end
+ let(:source) { create(:zone, name: 'source', kind: 'country', zone_members: []) }
+ let(:target) { create(:zone, name: 'target', kind: 'country', zone_members: []) }
context 'when the target has no members' do
- before { @source.members.create(zoneable: country1) }
+ before { source.members.create(zoneable: country1) }
it 'is false' do
- expect(@source.contains?(@target)).to be false
+ expect(source.contains?(target)).to be false
end
end
context 'when the source has no members' do
- before { @target.members.create(zoneable: country1) }
+ before { target.members.create(zoneable: country1) }
it 'is false' do
- expect(@source.contains?(@target)).to be false
+ expect(source.contains?(target)).to be false
end
end
context 'when both zones are the same zone' do
before do
- @source.members.create(zoneable: country1)
- @target = @source
+ source.members.create(zoneable: country1)
+ target.members.create(zoneable: country1)
end
it 'is true' do
- expect(@source.contains?(@target)).to be true
+ expect(source.contains?(target)).to be true
end
end
context 'when checking countries against countries' do
before do
- @source.members.create(zoneable: country1)
- @source.members.create(zoneable: country2)
+ source.members.create(zoneable: country1)
+ source.members.create(zoneable: country2)
end
context 'when all members are included in the zone we check against' do
before do
- @target.members.create(zoneable: country1)
- @target.members.create(zoneable: country2)
+ target.members.create(zoneable: country1)
+ target.members.create(zoneable: country2)
end
it 'is true' do
- expect(@source.contains?(@target)).to be true
+ expect(source.contains?(target)).to be true
end
end
context 'when some members are included in the zone we check against' do
before do
- @target.members.create(zoneable: country1)
- @target.members.create(zoneable: country2)
- @target.members.create(zoneable: create(:country))
+ target.members.create(zoneable: country1)
+ target.members.create(zoneable: country2)
+ target.members.create(zoneable: create(:country))
end
it 'is false' do
- expect(@source.contains?(@target)).to be false
+ expect(source.contains?(target)).to be false
end
end
context 'when none of the members are included in the zone we check against' do
before do
- @target.members.create(zoneable: create(:country))
- @target.members.create(zoneable: create(:country))
+ target.members.create(zoneable: create(:country))
+ target.members.create(zoneable: create(:country))
end
it 'is false' do
- expect(@source.contains?(@target)).to be false
+ expect(source.contains?(target)).to be false
end
end
end
context 'when checking states against states' do
+ let(:source) { create(:zone, name: 'source', kind: 'state', zone_members: []) }
+ let(:target) { create(:zone, name: 'target', kind: 'state', zone_members: []) }
+
before do
- @source.members.create(zoneable: state1)
- @source.members.create(zoneable: state2)
+ source.members.create(zoneable: state1)
+ source.members.create(zoneable: state2)
end
context 'when all members are included in the zone we check against' do
before do
- @target.members.create(zoneable: state1)
- @target.members.create(zoneable: state2)
+ target.members.create(zoneable: state1)
+ target.members.create(zoneable: state2)
end
it 'is true' do
- expect(@source.contains?(@target)).to be true
+ expect(source.contains?(target)).to be true
end
end
context 'when some members are included in the zone we check against' do
before do
- @target.members.create(zoneable: state1)
- @target.members.create(zoneable: state2)
- @target.members.create(zoneable: create(:state))
+ target.members.create(zoneable: state1)
+ target.members.create(zoneable: state2)
+ target.members.create(zoneable: create(:state))
end
it 'is false' do
- expect(@source.contains?(@target)).to be false
+ expect(source.contains?(target)).to be false
end
end
context 'when none of the members are included in the zone we check against' do
before do
- @target.members.create(zoneable: create(:state))
- @target.members.create(zoneable: create(:state))
+ target.members.create(zoneable: create(:state))
+ target.members.create(zoneable: create(:state))
end
it 'is false' do
- expect(@source.contains?(@target)).to be false
+ expect(source.contains?(target)).to be false
end
end
end
context 'when checking country against state' do
+ let(:source) { create(:zone, name: 'source', kind: 'state', zone_members: []) }
+ let(:target) { create(:zone, name: 'target', kind: 'country', zone_members: []) }
+
before do
- @source.members.create(zoneable: create(:state))
- @target.members.create(zoneable: country1)
+ source.members.create(zoneable: create(:state))
+ target.members.create(zoneable: country1)
end
it 'is false' do
- expect(@source.contains?(@target)).to be false
+ expect(source.contains?(target)).to be false
end
end
context 'when checking state against country' do
- before { @source.members.create(zoneable: country1) }
+ let(:source) { create(:zone, name: 'source', kind: 'country', zone_members: []) }
+ let(:target) { create(:zone, name: 'target', kind: 'state', zone_members: []) }
+
+ before { source.members.create(zoneable: country1) }
context 'when all states contained in one of the countries we check against' do
before do
state1 = create(:state, country: country1)
- @target.members.create(zoneable: state1)
+ target.members.create(zoneable: state1)
end
it 'is true' do
- expect(@source.contains?(@target)).to be true
+ expect(source.contains?(target)).to be true
end
end
context 'when some states contained in one of the countries we check against' do
before do
state1 = create(:state, country: country1)
- @target.members.create(zoneable: state1)
- @target.members.create(zoneable: create(:state, country: country2))
+ target.members.create(zoneable: state1)
+ target.members.create(zoneable: create(:state, country: country2))
end
it 'is false' do
- expect(@source.contains?(@target)).to be false
+ expect(source.contains?(target)).to be false
end
end
context 'when none of the states contained in any of the countries we check against' do
before do
- @target.members.create(zoneable: create(:state, country: country2))
- @target.members.create(zoneable: create(:state, country: country2))
+ target.members.create(zoneable: create(:state, country: country2))
+ target.members.create(zoneable: create(:state, country: country2))
end
it 'is false' do
- expect(@source.contains?(@target)).to be false
+ expect(source.contains?(target)).to be false
end
end
end
@@ -346,14 +353,17 @@ describe Spree::Zone, type: :model do
end
context 'when a zone member country is added to an existing zone consisting of state members' do
- it 'removes existing state members' do
- zone = create(:zone, name: 'foo', zone_members: [])
- state = create(:state)
+ it 'removes existing country members' do
+ zone = create(:zone, name: 'foo', kind: 'state', zone_members: [])
+ state = create(:state)
country = create(:country)
- zone.members.create(zoneable: state)
- country_member = zone.members.create(zoneable: country)
+
+ state_member = zone.members.create(zoneable: state)
+ _country_member = zone.members.create(zoneable: country)
+
zone.save
- expect(zone.reload.members).to eq([country_member])
+
+ expect(zone.reload.members).to eq([state_member])
end
end
end
@@ -365,13 +375,14 @@ describe Spree::Zone, type: :model do
end
context 'when the zone consists of country zone members' do
+ let(:zone) { create(:zone, name: 'country', kind: 'country', zone_members: []) }
+
before do
- @zone = create(:zone, name: 'country', zone_members: [])
- @zone.members.create(zoneable: create(:country))
+ zone.members.create(zoneable: create(:country))
end
it 'returns the kind of zone member' do
- expect(@zone.kind).to eq('country')
+ expect(zone.kind).to eq('country')
end
end
end
@@ -386,24 +397,26 @@ describe Spree::Zone, type: :model do
context 'finding potential matches for a country zone' do
let!(:zone) do
- create(:zone).tap do |z|
+ create(:zone, kind: 'country').tap do |z|
z.members.create(zoneable: country)
z.members.create(zoneable: country2)
z.save!
end
end
let!(:zone2) do
- create(:zone).tap { |z| z.members.create(zoneable: country) && z.save! }
+ create(:zone, kind: 'country').tap do |zone|
+ zone.members.create(zoneable: country)
+ end
end
+ let(:result) { Spree::Zone.potential_matching_zones(zone) }
- before { @result = Spree::Zone.potential_matching_zones(zone) }
it 'will find all zones with countries covered by the passed in zone' do
- expect(@result).to include(zone, zone2)
+ expect(result).to include(zone, zone2)
end
it 'only returns each zone once' do
- expect(@result.select { |z| z == zone }.size).to be 1
+ expect(result.select { |z| z == zone }.size).to be 1
end
end
@@ -487,4 +500,67 @@ describe Spree::Zone, type: :model do
end
end
end
+
+ describe '#state_list' do
+ let!(:country) { create(:country) }
+ let!(:state) { create(:state, country: country) }
+
+ context 'has countries associated' do
+ let(:country_zone) do
+ create(:zone, kind: 'country').tap do |zone|
+ zone.members.create(zoneable: country)
+ end
+ end
+
+ it 'returns states associated to that countries' do
+ expect(country_zone.state_list).to contain_exactly(state)
+ end
+ end
+
+ context 'has states associated' do
+ let(:state_zone) do
+ create(:zone, kind: 'state').tap do |zone|
+ zone.members.create(zoneable: state)
+ end
+ end
+
+ it 'returns that states' do
+ expect(state_zone.state_list).to contain_exactly(state)
+ end
+ end
+ end
+
+ describe '#state_list_for' do
+ let!(:country1) { create(:country) }
+ let!(:country2) { create(:country) }
+
+ let!(:state1) { create(:state, country: country1) }
+ let!(:state2) { create(:state, country: country2) }
+
+ context 'has countries associated' do
+ let(:country_zone) do
+ create(:zone, kind: 'country').tap do |zone|
+ zone.members.create(zoneable: country1)
+ zone.members.create(zoneable: country2)
+ end
+ end
+
+ it 'returns states associated with the country' do
+ expect(country_zone.state_list_for(country1)).to contain_exactly(state1)
+ end
+ end
+
+ context 'has states associated' do
+ let(:state_zone) do
+ create(:zone, kind: 'state').tap do |zone|
+ zone.members.create(zoneable: state1)
+ zone.members.create(zoneable: state2)
+ end
+ end
+
+ it 'returns states associated with the country' do
+ expect(state_zone.state_list_for(country1)).to contain_exactly(state1)
+ end
+ end
+ end
end