diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-07-15 01:41:34 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-07-15 04:23:09 +0200 |
| commit | d56155231814633e04f856d22646fea24ef97011 (patch) | |
| tree | 9d5b6f8c32ee5b59783a2880388e7833278d1d87 /app/models/node_action.rb | |
| parent | 28a6999b55cbec555696df848d6e924aae3166ee (diff) | |
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.
Diffstat (limited to 'app/models/node_action.rb')
| -rw-r--r-- | app/models/node_action.rb | 33 |
1 files changed, 33 insertions, 0 deletions
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 @@ | |||
| 1 | class NodeAction < ApplicationRecord | ||
| 2 | belongs_to :node, optional: true | ||
| 3 | belongs_to :page, optional: true | ||
| 4 | belongs_to :user, optional: true | ||
| 5 | |||
| 6 | validates :action, presence: true | ||
| 7 | validates :occurred_at, presence: true | ||
| 8 | |||
| 9 | def self.record!(node:, action:, user: nil, page: nil, locale: nil, **extra) | ||
| 10 | create!( | ||
| 11 | :node => node, | ||
| 12 | :page => page, | ||
| 13 | :user => user, | ||
| 14 | :action => action, | ||
| 15 | :locale => locale, | ||
| 16 | :occurred_at => Time.now, | ||
| 17 | :metadata => { | ||
| 18 | "username" => user&.login, | ||
| 19 | "human_readable_node_name" => Globalize.with_locale(I18n.default_locale) { | ||
| 20 | node&.head&.title || node&.draft&.title | ||
| 21 | }, | ||
| 22 | }.merge(extra.stringify_keys) | ||
| 23 | ) | ||
| 24 | end | ||
| 25 | |||
| 26 | def actor_name | ||
| 27 | metadata["username"] || "unknown" | ||
| 28 | end | ||
| 29 | |||
| 30 | def subject_name | ||
| 31 | metadata["human_readable_node_name"] || node&.unique_name || "deleted node" | ||
| 32 | end | ||
| 33 | end | ||
