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.

Raise RecordNotFound it no `current_store` (#12893)

Damian Legawiec0a580af0b96f
diff --git a/core/lib/spree/core/controller_helpers/store.rb b/core/lib/spree/core/controller_helpers/store.rb
index ac0837d308..de35685f49 100644
--- a/core/lib/spree/core/controller_helpers/store.rb
+++ b/core/lib/spree/core/controller_helpers/store.rb
@@ -9,6 +9,8 @@ module Spree
helper_method :current_store
helper_method :current_price_options
end
+
+ prepend_before_action :raise_record_not_found_if_store_is_not_found
end
def current_store
@@ -16,7 +18,7 @@ module Spree
end
def store_locale
- current_store.default_locale
+ @store_locale ||= current_store.default_locale
end
def ensure_current_store(object)
@@ -64,6 +66,10 @@ module Spree
def current_store_finder
Spree::Dependencies.current_store_finder.constantize
end
+
+ def raise_record_not_found_if_store_is_not_found
+ raise ActiveRecord::RecordNotFound if current_store.nil?
+ end
end
end
end
diff --git a/core/spec/lib/spree/core/controller_helpers/store_spec.rb b/core/spec/lib/spree/core/controller_helpers/store_spec.rb
index 89f9042d06..d7a8b08ed2 100644
--- a/core/spec/lib/spree/core/controller_helpers/store_spec.rb
+++ b/core/spec/lib/spree/core/controller_helpers/store_spec.rb
@@ -189,4 +189,16 @@ describe Spree::Core::ControllerHelpers::Store, type: :controller do
end
end
end
+
+ describe '#raise_record_not_found_if_store_is_not_found' do
+ context 'when the store is not found' do
+ before do
+ allow(controller).to receive(:current_store).and_return(nil)
+ end
+
+ it 'raises an exception' do
+ expect { controller.send(:raise_record_not_found_if_store_is_not_found) }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+ end
end