Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cache_key_with_version in page_etag #2365

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/alchemy/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ def signup_required?
#
# IMPORTANT:
#
# If your user does not have a +cache_key+ method (i.e. it's not an ActiveRecord model),
# If your user does not have a +cache_key_with_version+ method (i.e. it's not an ActiveRecord model),
# you have to ensure to implement it and return a unique identifier for that particular user.
# Otherwise all users will see the same cached page, regardless of user's state.
#
def page_etag
@page.cache_key + current_alchemy_user.try(:cache_key).to_s
[@page, current_alchemy_user]
end

# We only render the page if either the cache is disabled for this page
Expand Down
7 changes: 3 additions & 4 deletions spec/controllers/alchemy/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,21 +273,20 @@ module Alchemy
subject { controller.send(:page_etag) }

before do
expect(page).to receive(:cache_key).and_return("aaa")
controller.instance_variable_set("@page", page)
end

it "returns the etag for response headers" do
expect(subject).to eq("aaa")
expect(subject).to include(page)
end

context "with user logged in" do
before do
authorize_user(mock_model(Alchemy.user_class, cache_key: "bbb"))
authorize_user(mock_model(Alchemy.user_class, cache_key_with_version: "bbb"))
end

it "returns another etag for response headers" do
expect(subject).to eq("aaabbb")
expect(subject).to include(an_instance_of(Alchemy.user_class))
end
end
end
Expand Down