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.

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.rb
index 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 Spree
end
def execute
- by_url(scope) || scope.default
+ store = by_url(scope) || scope.default
+ Spree::Current.store = store
+ store
end
protected
diff --git a/core/app/mailers/spree/base_mailer.rb b/core/app/mailers/spree/base_mailer.rb
index 42a2a6f265..a6e77ad4ec 100644
--- a/core/app/mailers/spree/base_mailer.rb
+++ b/core/app/mailers/spree/base_mailer.rb
@@ -3,8 +3,9 @@ module Spree
helper Spree::MailHelper
def 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.default
end
+
helper_method :current_store
def from_address
@@ -39,7 +40,7 @@ module Spree
# http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views
def 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.url
end
def set_email_locale
diff --git a/core/app/models/spree/current.rb b/core/app/models/spree/current.rb
new file mode 100644
index 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
+end
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index dd91a6c8de..3209bdad3c 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -131,7 +131,7 @@ module Spree
delegate :iso, to: :default_country, prefix: true, allow_nil: true
def 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.store
end
# FIXME: we need to drop `or_initialize` in v5
diff --git a/core/spec/finders/spree/stores/find_current_spec.rb b/core/spec/finders/spree/stores/find_current_spec.rb
index 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 Spree
let(:scope) { nil }
let(:url) { nil }
+ before do
+ Spree::Current.store = nil
+ end
+
context 'no arguments' do
it { expect(subject).to eq(store) }
+ it { subject; expect(Spree::Current.store).to eq(store) }
end
context 'existing store' do
let(:url) { 'another.com' }
it { expect(subject).to eq(store_2) }
+ it { subject; expect(Spree::Current.store).to eq(store_2) }
end
context 'non-existing store' do