From d9e95bdb6436768c738539df74a164c885887174 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Thu, 23 Jul 2026 03:28:55 +0200 Subject: Add action_participants, recording every node a trash/destroy touches --- app/models/node.rb | 16 +++++++++------- app/models/node_action.rb | 19 ++++++++++++++----- 2 files changed, 23 insertions(+), 12 deletions(-) (limited to 'app/models') diff --git a/app/models/node.rb b/app/models/node.rb index a5a40d30..e92fa1ea 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -312,14 +312,14 @@ class Node < ApplicationRecord was_published = head_id.present? final_published_at = head&.published_at - demoted = 0 - ([self] + descendants.to_a).each do |node| - next unless node.head_id + subtree = [self] + descendants.to_a + demoted_nodes = subtree.select do |node| + next false unless node.head_id former = node.head node.head = nil node.draft_id = former.id if node.draft_id.nil? node.save! - demoted += 1 + true end self.reload @@ -332,9 +332,10 @@ class Node < ApplicationRecord metadata = { :path => { "from" => path_before, "to" => unique_name } } metadata[:was_published] = true if was_published metadata[:final_published_at] = final_published_at.iso8601 if final_published_at - metadata[:demoted_heads] = demoted if demoted > 0 + metadata[:demoted_heads] = demoted_nodes.size if demoted_nodes.any? - NodeAction.record!(:node => self, :user => current_user, :action => "trash", **metadata) + NodeAction.record!(:participants => subtree, :user => current_user, + :action => "trash", **metadata) self end end @@ -382,7 +383,8 @@ class Node < ApplicationRecord metadata = { :path => unique_name } metadata[:destroyed_descendants] = doomed.size - 1 if doomed.size > 1 - NodeAction.record!(:node => self, :user => current_user, :action => "destroy", **metadata) + NodeAction.record!(:participants => doomed, :user => current_user, + :action => "destroy", **metadata) doomed.each(&:destroy!) end end diff --git a/app/models/node_action.rb b/app/models/node_action.rb index 63a99ae8..8a3dd8b7 100644 --- a/app/models/node_action.rb +++ b/app/models/node_action.rb @@ -3,6 +3,8 @@ class NodeAction < ApplicationRecord belongs_to :page, optional: true belongs_to :user, optional: true + has_many :action_participants, :dependent => :destroy + validates :action, presence: true validates :occurred_at, presence: true @@ -81,10 +83,15 @@ class NodeAction < ApplicationRecord # This log records; it does not undo. No IP, session, or user # agent, ever. Success only. - def self.record!(node:, action:, user: nil, page: nil, locale: nil, - occurred_at: nil, inferred_from: nil, **extra) + def self.record!(node: nil, participants: [], action:, user: nil, page: nil, + locale: nil, occurred_at: nil, inferred_from: nil, **extra) + participants = participants.presence || [node].compact + raise ArgumentError, "NodeAction.record! needs at least one participant" if participants.empty? + + primary_node = node || (participants.first if participants.first.is_a?(Node)) + create!( - :node => node, + :node => primary_node, :page => page, :user => user, :action => action, @@ -94,10 +101,12 @@ class NodeAction < ApplicationRecord :metadata => { "username" => user&.login, "human_readable_node_name" => Globalize.with_locale(I18n.default_locale) { - node&.head&.title || node&.draft&.title + primary_node&.head&.title || primary_node&.draft&.title }, }.merge(extra.stringify_keys) - ) + ).tap do |na| + participants.each { |subject| na.action_participants.create!(:subject => subject) } + end end # Computes the publish-entry diff between an outgoing head and the -- cgit v1.3