Skip to content

Commit

Permalink
Touch nodes and all their ancestors on page update
Browse files Browse the repository at this point in the history
Menu partials might be cached.
So we need to make sure the menu node a page might be
attached to and all their ancestors get touched as well.

Closes #2080
  • Loading branch information
tvdeyen committed Dec 31, 2021
1 parent 7305bdc commit bf7ee77
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 6 additions & 1 deletion app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Page < BaseRecord
after_update :create_legacy_url,
if: :saved_change_to_urlname?

after_update -> { nodes.update_all(updated_at: Time.current) }
after_update :touch_nodes

# Concerns
include PageScopes
Expand Down Expand Up @@ -603,5 +603,10 @@ def set_language_code
def create_legacy_url
legacy_urls.find_or_create_by(urlname: urlname_before_last_save)
end

def touch_nodes
ids = node_ids + nodes.map { |n| n.ancestors.pluck(:id) }
Node.where(id: ids).touch_all
end
end
end
11 changes: 6 additions & 5 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2050,17 +2050,18 @@ class AnotherUrlPathClass; end

describe "#nodes" do
let(:page) { create(:alchemy_page) }
let(:node) { create(:alchemy_node, page: page, updated_at: 1.hour.ago) }
let(:parent) { create(:alchemy_node, updated_at: 1.hour.ago) }
let(:node) { create(:alchemy_node, page: page, parent: parent, updated_at: 1.hour.ago) }

it "returns all nodes the page is attached to" do
expect(page.nodes).to include(node)
end

describe "after page updates" do
it "touches all nodes" do
expect {
page.update(name: "foo")
}.to change { node.reload.updated_at }
subject { page.update(name: "foo") }

it "touches all nodes and all their ancestors" do
expect { subject }.to change { node.reload.updated_at }.and change { parent.reload.updated_at }
end
end
end
Expand Down

0 comments on commit bf7ee77

Please sign in to comment.