From d56155231814633e04f856d22646fea24ef97011 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Wed, 15 Jul 2026 01:41:34 +0200 Subject: Add NodeAction: an append-only log of who did what to a node node_id/page_id/user_id are lookup and ordering only -- all three nullify on delete, so an entry outlives its actor and its subject. Everything that must survive those deletions lives in a mandatory metadata jsonb written once at creation: the actor's username, the node's human-readable name (pinned to the default locale), and action-specific extras such as publish's title from/to. NodeAction.record! is the single constructor, so every entry gets the same baseline metadata without each call site re-implementing it. occurred_at is one field for live and backfilled entries alike; inferred_from distinguishes them -- nil means witnessed at the moment it happened, populated names how a backfilled entry was estimated. Instrumented so far: publish (crediting the actual publisher, threaded through from the controller -- previously nobody had the act of publishing recorded anywhere), revert's discard_autosave and destroy_draft branches, and translation destroy. publish_draft! now runs in a transaction so the promotion and its log entry land together. The remaining verbs follow once this mechanism has proven itself. --- app/controllers/nodes_controller.rb | 2 +- app/controllers/page_translations_controller.rb | 3 ++ app/models/node.rb | 65 ++++++++++++++----------- app/models/node_action.rb | 33 +++++++++++++ 4 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 app/models/node_action.rb (limited to 'app') diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb index bff1733..9c5d228 100644 --- a/app/controllers/nodes_controller.rb +++ b/app/controllers/nodes_controller.rb @@ -147,7 +147,7 @@ class NodesController < ApplicationController end def publish - @node.publish_draft! + @node.publish_draft!(current_user) flash[:notice] = "Draft has been published" redirect_to node_path(@node) end diff --git a/app/controllers/page_translations_controller.rb b/app/controllers/page_translations_controller.rb index 38a7c4f..be4f488 100644 --- a/app/controllers/page_translations_controller.rb +++ b/app/controllers/page_translations_controller.rb @@ -63,6 +63,9 @@ class PageTranslationsController < ApplicationController draft.translations.where(:locale => @locale).delete_all draft.reload + NodeAction.record!(:node => @node, :page => draft, :user => current_user, + :action => "translation_destroy", :locale => @locale) + flash[:notice] = "#{@locale.upcase} translation removed from the draft. Publish to make this permanent." redirect_to node_path(@node) rescue LockedByAnotherUser => e diff --git a/app/models/node.rb b/app/models/node.rb index 311c5c0..b41df06 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -170,10 +170,12 @@ class Node < ApplicationRecord self.autosave.destroy self.autosave_id = nil self.save! + NodeAction.record!(:node => self, :user => current_user, :action => "discard_autosave") elsif self.draft && self.head self.draft.destroy self.draft_id = nil self.save! + NodeAction.record!(:node => self, :user => current_user, :action => "destroy_draft") end self.unlock! unless self.draft @@ -188,43 +190,50 @@ class Node < ApplicationRecord end end - def publish_draft! + def publish_draft! current_user = nil # Return nil if nothing to publish and no staged changes return nil unless self.draft || staged_slug || staged_parent_id - if self.draft - self.head = self.draft - self.head.published_at ||= Time.now - self.head.save! - self.draft = nil - end + ActiveRecord::Base.transaction do + if self.draft + previous_title = self.head ? Globalize.with_locale(I18n.default_locale) { self.head.title } : nil + self.head = self.draft + self.head.published_at ||= Time.now + self.head.save! + self.draft = nil + + new_title = Globalize.with_locale(I18n.default_locale) { self.head.title } + NodeAction.record!(:node => self, :page => self.head, :user => current_user, + :action => "publish", :from => previous_title, :to => new_title) + end - if staged_slug && (staged_slug != slug) - self.slug = staged_slug - self.staged_slug = nil - end + if staged_slug && (staged_slug != slug) + self.slug = staged_slug + self.staged_slug = nil + end - if staged_parent_id && (staged_parent_id != parent_id) - new_parent = Node.find(staged_parent_id) + if staged_parent_id && (staged_parent_id != parent_id) + new_parent = Node.find(staged_parent_id) - if new_parent == self || self.descendants.include?(new_parent) - raise ActiveRecord::RecordInvalid.new(self), "Cannot move a node under itself or one of its own descendants" - end + if new_parent == self || self.descendants.include?(new_parent) + raise ActiveRecord::RecordInvalid.new(self), "Cannot move a node under itself or one of its own descendants" + end - self.staged_parent_id = nil - self.save! - self.move_to_child_of(new_parent) - else - unless self.save - raise ActiveRecord::RecordInvalid.new(self) + self.staged_parent_id = nil + self.save! + self.move_to_child_of(new_parent) + else + unless self.save + raise ActiveRecord::RecordInvalid.new(self) + end end - end - self.reload - self.update_unique_name - self.send(:update_unique_names_of_children) - self.unlock! - self + self.reload + self.update_unique_name + self.send(:update_unique_names_of_children) + self.unlock! + self + end end def restore_revision! revision diff --git a/app/models/node_action.rb b/app/models/node_action.rb new file mode 100644 index 0000000..f32b5b7 --- /dev/null +++ b/app/models/node_action.rb @@ -0,0 +1,33 @@ +class NodeAction < ApplicationRecord + belongs_to :node, optional: true + belongs_to :page, optional: true + belongs_to :user, optional: true + + validates :action, presence: true + validates :occurred_at, presence: true + + def self.record!(node:, action:, user: nil, page: nil, locale: nil, **extra) + create!( + :node => node, + :page => page, + :user => user, + :action => action, + :locale => locale, + :occurred_at => Time.now, + :metadata => { + "username" => user&.login, + "human_readable_node_name" => Globalize.with_locale(I18n.default_locale) { + node&.head&.title || node&.draft&.title + }, + }.merge(extra.stringify_keys) + ) + end + + def actor_name + metadata["username"] || "unknown" + end + + def subject_name + metadata["human_readable_node_name"] || node&.unique_name || "deleted node" + end +end -- cgit v1.3