Skip to content

Commit

Permalink
Support different cache keys for signed/not-signed/admin users when v…
Browse files Browse the repository at this point in the history
…iewing Storefront cached pages
  • Loading branch information
damianlegawiec committed Apr 26, 2021
1 parent 24152ef commit f7fffbf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
6 changes: 4 additions & 2 deletions frontend/app/controllers/spree/store_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ def store_etag
[
current_store,
current_currency,
I18n.locale
]
I18n.locale,
try_spree_current_user.present?,
try_spree_current_user.try(:has_spree_role?, 'admin')
].compact
end

def store_last_modified
Expand Down
55 changes: 55 additions & 0 deletions frontend/spec/controllers/spree/store_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'spec_helper'

describe Spree::StoreController, type: :controller do
describe '#store_etag' do
let!(:store) { create(:store, default: true, default_locale: 'es', default_currency: 'EUR') }

before { controller.send(:set_locale) }

context 'guest visitor' do
it do
expect(controller.send(:store_etag)).to eq [
store,
'EUR',
:es,
false
]
end
end

context 'with signed in user' do
let(:user) { stub_model(Spree::LegacyUser) }

before do
allow(controller).to receive_messages try_spree_current_user: user
allow(controller).to receive_messages spree_current_user: user
end

context 'regular user' do
it do
expect(controller.send(:store_etag)).to eq [
store,
'EUR',
:es,
true,
false
]
end
end

context 'admin user' do
before { user.spree_roles << Spree::Role.find_or_create_by(name: :admin) }

it do
expect(controller.send(:store_etag)).to eq [
store,
'EUR',
:es,
true,
true
]
end
end
end
end
end

0 comments on commit f7fffbf

Please sign in to comment.