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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Added `Current.zone` to hold the current zone
Damian Legawiec544807821b00
diff --git a/core/app/models/spree/current.rb b/core/app/models/spree/current.rbindex 119394920d..bd7572dfbf 100644--- a/core/app/models/spree/current.rb+++ b/core/app/models/spree/current.rb@@ -1,6 +1,6 @@module Spreeclass Current < ::ActiveSupport::CurrentAttributes- attribute :store, :currency+ attribute :store, :currency, :zonedef storesuper || Spree::Store.default@@ -9,5 +9,9 @@ module Spreedef currencysuper || store&.default_currencyend++ def zone+ super || Spree::Zone.default_tax || store&.checkout_zone+ endendenddiff --git a/core/lib/spree/core/controller_helpers/store.rb b/core/lib/spree/core/controller_helpers/store.rbindex e0b1b86457..04dca5f9db 100644--- a/core/lib/spree/core/controller_helpers/store.rb+++ b/core/lib/spree/core/controller_helpers/store.rb@@ -60,7 +60,11 @@ module Spreeprivatedef current_tax_zone- @current_tax_zone ||= @current_order&.tax_zone || Spree::Zone.default_tax+ @current_tax_zone ||= begin+ zone = @current_order&.tax_zone || Spree::Zone.default_tax || current_store.checkout_zone+ Spree::Current.zone = zone+ zone+ endenddef current_store_finderdiff --git a/core/lib/spree/core/pricing/context.rb b/core/lib/spree/core/pricing/context.rbindex 6c23184fbe..3da39ce79f 100644--- a/core/lib/spree/core/pricing/context.rb+++ b/core/lib/spree/core/pricing/context.rb@@ -15,8 +15,8 @@ module Spreedef initialize(variant:, currency:, store: nil, zone: nil, user: nil, quantity: nil, date: nil, order: nil)@variant = variant@currency = currency- @store = store || Spree::Store.default- @zone = zone+ @store = store || Spree::Current.store+ @zone = zone || Spree::Current.zone@user = user@quantity = quantity@date = date || Time.currentdiff --git a/core/spec/lib/spree/core/controller_helpers/store_spec.rb b/core/spec/lib/spree/core/controller_helpers/store_spec.rbindex dfc92fa277..20b23bbb42 100644--- a/core/spec/lib/spree/core/controller_helpers/store_spec.rb+++ b/core/spec/lib/spree/core/controller_helpers/store_spec.rb@@ -131,6 +131,8 @@ describe Spree::Core::ControllerHelpers::Store, type: :controller dodescribe '#current_price_options' dosubject(:current_price_options) { controller.current_price_options }+ after { Spree::Current.reset }+context 'when there is a default tax zone' dolet(:default_zone) { Spree::Zone.new }@@ -142,6 +144,11 @@ describe Spree::Core::ControllerHelpers::Store, type: :controller doit 'returns the default tax zone' doexpect(subject).to include(tax_zone: default_zone)end++ it 'sets Spree::Current.zone to the default tax zone' do+ subject+ expect(Spree::Current.zone).to eq(default_zone)+ endendcontext 'when there is a current order' do@@ -155,6 +162,11 @@ describe Spree::Core::ControllerHelpers::Store, type: :controller doendit { is_expected.to include(tax_zone: other_zone) }++ it 'sets Spree::Current.zone to the order tax zone' do+ subject+ expect(Spree::Current.zone).to eq(other_zone)+ endendend@@ -164,8 +176,39 @@ describe Spree::Core::ControllerHelpers::Store, type: :controller doendcontext 'when there is no current order' do- it 'return nil when asked for the current tax zone' do- expect(current_price_options[:tax_zone]).to be_nil+ context 'when store has a checkout_zone' do+ let(:checkout_zone) { Spree::Zone.new }++ before do+ allow(controller).to receive(:current_store).and_return(+ double('Store', checkout_zone: checkout_zone, url: 'test.com')+ )+ end++ it 'returns the store checkout_zone' do+ expect(current_price_options[:tax_zone]).to eq(checkout_zone)+ end++ it 'sets Spree::Current.zone to the store checkout_zone' do+ subject+ expect(Spree::Current.zone).to eq(checkout_zone)+ end+ end++ context 'when store has no checkout_zone' do+ before do+ allow(controller.current_store).to receive(:checkout_zone).and_return(nil)+ end++ it 'returns nil when asked for the current tax zone' do+ expect(current_price_options[:tax_zone]).to be_nil+ end++ it 'sets Spree::Current.zone to nil' do+ subject+ # Spree::Current.zone will call default_tax again, so we check the attributes hash+ expect(Spree::Current.attributes[:zone]).to be_nil+ endendend@@ -180,6 +223,11 @@ describe Spree::Core::ControllerHelpers::Store, type: :controller doendit { is_expected.to include(tax_zone: other_zone) }++ it 'sets Spree::Current.zone to the order tax zone' do+ subject+ expect(Spree::Current.zone).to eq(other_zone)+ endendendenddiff --git a/core/spec/lib/spree/core/pricing/context_spec.rb b/core/spec/lib/spree/core/pricing/context_spec.rbindex 3f83602088..eaf3161dd4 100644--- a/core/spec/lib/spree/core/pricing/context_spec.rb+++ b/core/spec/lib/spree/core/pricing/context_spec.rb@@ -41,17 +41,19 @@ module Spreedescribed_class.new(variant: variant, currency: currency)end+ after { Spree::Current.reset }+it 'sets required attributes' doexpect(subject.variant).to eq(variant)expect(subject.currency).to eq(currency)end- it 'defaults store to default store' do- expect(subject.store).to eq(Spree::Store.default)+ it 'defaults store to Spree::Current.store' do+ expect(subject.store).to eq(Spree::Current.store)end- it 'defaults zone to nil' do- expect(subject.zone).to be_nil+ it 'defaults zone to Spree::Current.zone' do+ expect(subject.zone).to eq(Spree::Current.zone)endit 'defaults user to nil' do@@ -68,6 +70,36 @@ module Spreeendendend++ context 'when Spree::Current.store is set' do+ let(:current_store) { create(:store) }++ before { Spree::Current.store = current_store }+ after { Spree::Current.reset }++ subject do+ described_class.new(variant: variant, currency: currency)+ end++ it 'uses Spree::Current.store as default' do+ expect(subject.store).to eq(current_store)+ end+ end++ context 'when Spree::Current.zone is set' do+ let(:current_zone) { create(:zone) }++ before { Spree::Current.zone = current_zone }+ after { Spree::Current.reset }++ subject do+ described_class.new(variant: variant, currency: currency)+ end++ it 'uses Spree::Current.zone as default' do+ expect(subject.zone).to eq(current_zone)+ end+ endenddescribe '.from_currency' do@@ -196,18 +228,21 @@ module Spreedescribed_class.new(variant: variant, currency: currency)end- it 'generates a cache key excluding nil values' do+ after { Spree::Current.reset }++ it 'generates a cache key with default values from Spree::Current' doTimecop.freeze do- expected_key = [+ expected_parts = ['spree','pricing',variant.id,currency,- Spree::Store.default.id,- Time.current.to_i- ].join('/')+ Spree::Current.store.id+ ]+ expected_parts << Spree::Current.zone.id if Spree::Current.zone+ expected_parts << Time.current.to_i- expect(subject.cache_key).to eq(expected_key)+ expect(subject.cache_key).to eq(expected_parts.join('/'))endendend@@ -222,20 +257,23 @@ module Spree)end+ after { Spree::Current.reset }+it 'includes present optional attributes in correct order' doTimecop.freeze do- expected_key = [+ expected_parts = ['spree','pricing',variant.id,currency,- Spree::Store.default.id,- user.id,- quantity,- Time.current.to_i- ].join('/')-- expect(subject.cache_key).to eq(expected_key)+ Spree::Current.store.id+ ]+ expected_parts << Spree::Current.zone.id if Spree::Current.zone+ expected_parts << user.id+ expected_parts << quantity+ expected_parts << Time.current.to_i++ expect(subject.cache_key).to eq(expected_parts.join('/'))endendenddiff --git a/core/spec/models/spree/current_spec.rb b/core/spec/models/spree/current_spec.rbnew file mode 100644index 0000000000..ba620f5735--- /dev/null+++ b/core/spec/models/spree/current_spec.rb@@ -0,0 +1,109 @@+require 'spec_helper'++RSpec.describe Spree::Current do+ after do+ described_class.reset+ end++ describe '#store' do+ context 'when store is set' do+ let(:store) { create(:store) }++ before { described_class.store = store }++ it 'returns the set store' do+ expect(described_class.store).to eq(store)+ end+ end++ context 'when store is not set' do+ it 'returns the default store' do+ expect(described_class.store).to eq(Spree::Store.default)+ end+ end+ end++ describe '#currency' do+ context 'when currency is set' do+ before { described_class.currency = 'EUR' }++ it 'returns the set currency' do+ expect(described_class.currency).to eq('EUR')+ end+ end++ context 'when currency is not set' do+ let!(:store) { create(:store, default: true, default_currency: 'GBP') }++ it 'returns the default currency from the store' do+ expect(described_class.currency).to eq('GBP')+ end+ end+ end++ describe '#zone' do+ context 'when zone is set' do+ let(:zone) { create(:zone) }++ before { described_class.zone = zone }++ it 'returns the set zone' do+ expect(described_class.zone).to eq(zone)+ end+ end++ context 'when zone is not set' do+ let!(:default_zone) { create(:zone, default_tax: true) }++ it 'returns the default tax zone' do+ expect(described_class.zone).to eq(default_zone)+ end+ end++ context 'when zone is not set and no default tax zone exists' do+ before do+ Spree::Zone.update_all(default_tax: false)+ end++ context 'when store has a checkout_zone' do+ let(:checkout_zone) { create(:zone) }+ let!(:store) { create(:store, default: true, checkout_zone: checkout_zone) }++ it 'returns the store checkout_zone' do+ expect(described_class.zone).to eq(checkout_zone)+ end+ end++ context 'when store has no checkout_zone' do+ it 'returns nil' do+ expect(described_class.zone).to be_nil+ end+ end+ end+ end++ describe '.reset' do+ let(:store) { create(:store) }+ let(:zone) { create(:zone) }++ before do+ described_class.store = store+ described_class.currency = 'EUR'+ described_class.zone = zone+ end++ it 'resets all attributes' do+ described_class.reset++ # After reset, store returns default, not the set store+ expect(described_class.store).not_to eq(store)+ expect(described_class.store).to eq(Spree::Store.default)++ # Currency falls back to store default+ expect(described_class.currency).not_to eq('EUR')++ # Zone falls back to default tax zone+ expect(described_class.zone).not_to eq(zone)+ end+ end+end