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 spree current user to base cache key

v10110853926f62b97
diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb
index 5152bfce06..00b0e0c75c 100644
--- a/core/app/helpers/spree/base_helper.rb
+++ b/core/app/helpers/spree/base_helper.rb
@@ -183,7 +183,8 @@ module Spree
end
def base_cache_key
- [I18n.locale, current_currency]
+ [I18n.locale, current_currency, defined?(try_spree_current_user) && try_spree_current_user.present?,
+ defined?(try_spree_current_user) && try_spree_current_user.try(:has_spree_role?, 'admin')]
end
def maximum_quantity
diff --git a/core/spec/helpers/base_helper_spec.rb b/core/spec/helpers/base_helper_spec.rb
index b6c02d2e48..8f6f09292e 100644
--- a/core/spec/helpers/base_helper_spec.rb
+++ b/core/spec/helpers/base_helper_spec.rb
@@ -128,6 +128,51 @@ describe Spree::BaseHelper, type: :helper do
end
end
+ context 'base_cache_key' do
+ let(:current_currency) { 'USD' }
+
+ context 'when try_spree_current_user defined' do
+ before do
+ I18n.locale = I18n.default_locale
+ allow_any_instance_of(described_class).to receive(:try_spree_current_user).and_return(user)
+ end
+
+ context 'when admin user' do
+ let!(:user) { create(:admin_user) }
+
+ it 'returns base cache key' do
+ expect(base_cache_key).to eq [:en, 'USD', true, true]
+ end
+ end
+
+ context 'when user without admin role' do
+ let!(:user) { create(:user) }
+
+ it 'returns base cache key' do
+ expect(base_cache_key).to eq [:en, 'USD', true, false]
+ end
+ end
+
+ context 'when spree_current_user is nil' do
+ let!(:user) { nil }
+
+ it 'returns base cache key' do
+ expect(base_cache_key).to eq [:en, 'USD', false, nil]
+ end
+ end
+ end
+
+ context 'when try_spree_current_user is undefined' do
+ let(:current_currency) { 'USD' }
+
+ before { I18n.locale = I18n.default_locale }
+
+ it 'returns base cache key' do
+ expect(base_cache_key).to eq [:en, 'USD', nil, nil]
+ end
+ end
+ end
+
# Regression test for #2396
context 'meta_data_tags' do
it 'truncates a product description to 160 characters' do