From a8974de7ed93f633a1c4431f4866242fbed17d2f Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 5 May 2022 21:31:47 +0200 Subject: [PATCH] Add support for Rails' recycable cache keys This has been a feature from Rails 5.2 and is enabled by default from Rails 6.0 and above. --- app/models/alchemy/page/page_natures.rb | 10 +++++----- spec/models/alchemy/page_spec.rb | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/models/alchemy/page/page_natures.rb b/app/models/alchemy/page/page_natures.rb index b8809b9396..18703034f5 100644 --- a/app/models/alchemy/page/page_natures.rb +++ b/app/models/alchemy/page/page_natures.rb @@ -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 diff --git a/spec/models/alchemy/page_spec.rb b/spec/models/alchemy/page_spec.rb index f48210264d..a759c5a2f7 100644 --- a/spec/models/alchemy/page_spec.rb +++ b/spec/models/alchemy/page_spec.rb @@ -735,7 +735,7 @@ class AnotherUrlPathClass; end end end - describe "#cache_key" do + describe "#cache_version" do let(:now) { Time.current } let(:last_week) { Time.current - 1.week } @@ -743,7 +743,7 @@ class AnotherUrlPathClass; end 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) @@ -753,7 +753,7 @@ 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 @@ -761,7 +761,7 @@ class AnotherUrlPathClass; end 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