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
Introduce `Spree::Current` model (#12298)
Damian Legawiec66395e8da60e
diff --git a/core/app/finders/spree/stores/find_current.rb b/core/app/finders/spree/stores/find_current.rbindex 647d23f6c5..dc56f08052 100644--- a/core/app/finders/spree/stores/find_current.rb+++ b/core/app/finders/spree/stores/find_current.rb@@ -7,7 +7,9 @@ module Spreeenddef execute- by_url(scope) || scope.default+ store = by_url(scope) || scope.default+ Spree::Current.store = store+ storeendprotecteddiff --git a/core/app/mailers/spree/base_mailer.rb b/core/app/mailers/spree/base_mailer.rbindex 42a2a6f265..a6e77ad4ec 100644--- a/core/app/mailers/spree/base_mailer.rb+++ b/core/app/mailers/spree/base_mailer.rb@@ -3,8 +3,9 @@ module Spreehelper Spree::MailHelperdef current_store- @current_store ||= @order&.store.presence || Spree::Dependencies.current_store_finder.constantize.new.execute+ @current_store ||= @order&.store.presence || Spree::Store.current || Spree::Store.defaultend+helper_method :current_storedef from_address@@ -39,7 +40,7 @@ module Spree# http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-viewsdef ensure_default_action_mailer_url_host(store_url = nil)ActionMailer::Base.default_url_options ||= {}- ActionMailer::Base.default_url_options[:host] ||= store_url || current_store.url+ ActionMailer::Base.default_url_options[:host] ||= store_url.presence || current_store.urlenddef set_email_localediff --git a/core/app/models/spree/current.rb b/core/app/models/spree/current.rbnew file mode 100644index 0000000000..e5378a93ff--- /dev/null+++ b/core/app/models/spree/current.rb@@ -0,0 +1,9 @@+module Spree+ class Current < ::ActiveSupport::CurrentAttributes+ attribute :store++ def store+ super || Spree::Store.default+ end+ end+enddiff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex dd91a6c8de..3209bdad3c 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -131,7 +131,7 @@ module Spreedelegate :iso, to: :default_country, prefix: true, allow_nil: truedef self.current(url = nil)- Spree::Dependencies.current_store_finder.constantize.new(url: url).execute+ Spree::Dependencies.current_store_finder.constantize.new(url: url).execute || Spree::Current.storeend# FIXME: we need to drop `or_initialize` in v5diff --git a/core/spec/finders/spree/stores/find_current_spec.rb b/core/spec/finders/spree/stores/find_current_spec.rbindex dcadae992d..4f20a61943 100644--- a/core/spec/finders/spree/stores/find_current_spec.rb+++ b/core/spec/finders/spree/stores/find_current_spec.rb@@ -10,14 +10,20 @@ module Spreelet(:scope) { nil }let(:url) { nil }+ before do+ Spree::Current.store = nil+ end+context 'no arguments' doit { expect(subject).to eq(store) }+ it { subject; expect(Spree::Current.store).to eq(store) }endcontext 'existing store' dolet(:url) { 'another.com' }it { expect(subject).to eq(store_2) }+ it { subject; expect(Spree::Current.store).to eq(store_2) }endcontext 'non-existing store' do