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/models/node_action.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/models/node_action.rb (limited to 'app/models/node_action.rb') 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