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

Add support for Rails' recycable cache keys #2334

Merged
merged 1 commit into from
May 6, 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
10 changes: 5 additions & 5 deletions app/models/alchemy/page/page_natures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ def layout_partial_name
page_layout.parameterize.underscore
end

# Returns the key that's taken for cache path.
# Returns the version that's taken for Rails' recycable cache key.
#
# Uses the +published_at+ value that's updated when the user publishes the page.
#
# If the page is the current preview it uses the updated_at value as cache key.
# If the page is the current preview it uses the +updated_at+ value as cache key.
#
def cache_key
def cache_version
if Page.current_preview == id
"alchemy/pages/#{id}-#{updated_at}"
updated_at.to_s
else
"alchemy/pages/#{id}-#{published_at}"
published_at.to_s
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -735,15 +735,15 @@ class AnotherUrlPathClass; end
end
end

describe "#cache_key" do
describe "#cache_version" do
let(:now) { Time.current }
let(:last_week) { Time.current - 1.week }

let(:page) do
build_stubbed(:alchemy_page, updated_at: now, published_at: last_week)
end

subject { page.cache_key }
subject { page.cache_version }

before do
expect(Page).to receive(:current_preview).and_return(preview)
Expand All @@ -753,15 +753,15 @@ class AnotherUrlPathClass; end
let(:preview) { page.id }

it "uses updated_at" do
is_expected.to eq("alchemy/pages/#{page.id}-#{now}")
is_expected.to eq(now.to_s)
end
end

context "when current page not in preview mode" do
let(:preview) { nil }

it "uses published_at" do
is_expected.to eq("alchemy/pages/#{page.id}-#{last_week}")
is_expected.to eq(last_week.to_s)
end
end
end
Expand Down