diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-07-08 16:22:17 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-07-08 16:22:17 +0200 |
| commit | a006440f59bf99380e179cc2963cb98d4d894d3a (patch) | |
| tree | ae74b9bdf00e7218b90a6cb2c119504419fa8ae4 /app/models | |
| parent | 08b4cdb711ffe39153264a5c4024fac95cd8d91a (diff) | |
Add head/draft/autosave hierarchy to Node
Introduces autosave_id as a third, unversioned layer above draft/head,
with lock_for_editing!, autosave!, and save_draft! as the new entry
points. Also fixes a real bug in wipe_draft!: its "no draft" branch
unconditionally released the lock, which was safe when "no draft" only
ever meant "nothing is happening" — no longer true now that a lock can
exist with only an autosave beneath it. lock_for_editing! deliberately
does not call wipe_draft! at all, for the same reason: an intruder
calling it while a lock was genuinely held would otherwise silently
steal it via wipe_draft!'s own unlock side effect, caught by the new
two-user lock test.
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/node.rb | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/app/models/node.rb b/app/models/node.rb index fc23dc1..9eb0fe4 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -6,6 +6,7 @@ class Node < ApplicationRecord | |||
| 6 | has_many :pages, -> { order("revision ASC") }, :dependent => :destroy | 6 | has_many :pages, -> { order("revision ASC") }, :dependent => :destroy |
| 7 | belongs_to :head, :class_name => "Page", :foreign_key => :head_id, :dependent => :destroy, optional: true | 7 | belongs_to :head, :class_name => "Page", :foreign_key => :head_id, :dependent => :destroy, optional: true |
| 8 | belongs_to :draft, :class_name => "Page", :foreign_key => :draft_id, :dependent => :destroy, optional: true | 8 | belongs_to :draft, :class_name => "Page", :foreign_key => :draft_id, :dependent => :destroy, optional: true |
| 9 | belongs_to :autosave, :class_name => "Page", :foreign_key => :autosave_id, :dependent => :destroy, optional: true | ||
| 9 | has_many :permissions, :dependent => :destroy | 10 | has_many :permissions, :dependent => :destroy |
| 10 | has_many :events, :dependent => :destroy | 11 | has_many :events, :dependent => :destroy |
| 11 | belongs_to :lock_owner, :class_name => "User", :foreign_key => :locking_user_id, optional: true | 12 | belongs_to :lock_owner, :class_name => "User", :foreign_key => :locking_user_id, optional: true |
| @@ -49,6 +50,71 @@ class Node < ApplicationRecord | |||
| 49 | 50 | ||
| 50 | # Instance Methods | 51 | # Instance Methods |
| 51 | 52 | ||
| 53 | # Acquires (or reaffirms) the editing lock without creating a draft or | ||
| 54 | # an autosave -- both are now deferred until there is real content to | ||
| 55 | # hold. | ||
| 56 | def lock_for_editing! current_user | ||
| 57 | if self.lock_owner.nil? || self.lock_owner == current_user | ||
| 58 | lock_for! current_user | ||
| 59 | self | ||
| 60 | else | ||
| 61 | raise( | ||
| 62 | LockedByAnotherUser, | ||
| 63 | "Page is locked by another user who is working on it! " \ | ||
| 64 | "Last modification: #{(self.autosave || self.draft || self.head).updated_at.to_fs(:db)}" | ||
| 65 | ) | ||
| 66 | end | ||
| 67 | end | ||
| 68 | |||
| 69 | # Creates or updates the autosave buffer from the given attributes. | ||
| 70 | # Autosave rows are never associated to the node via node_id -- they | ||
| 71 | # must never appear in self.pages / the revisions list, which is the | ||
| 72 | # whole reason autosave exists as a separate, unversioned layer. | ||
| 73 | def autosave! attributes, current_user | ||
| 74 | assert_locked_by! current_user | ||
| 75 | |||
| 76 | unless self.autosave | ||
| 77 | self.autosave = Page.create!(:editor => current_user) | ||
| 78 | self.save! | ||
| 79 | end | ||
| 80 | self.autosave.assign_attributes(attributes) | ||
| 81 | self.autosave.save! | ||
| 82 | self.autosave | ||
| 83 | end | ||
| 84 | |||
| 85 | # Promotes the current autosave into the draft (creating the draft if | ||
| 86 | # none exists yet) and destroys the autosave afterward. This is what | ||
| 87 | # the explicit "Save" action does; it never creates a new revision -- | ||
| 88 | # same as any other in-place draft edit. The new draft is created via | ||
| 89 | # self.pages.create! rather than by repointing the autosave's own | ||
| 90 | # node_id, because acts_as_list assigns the revision number at create | ||
| 91 | # time, scoped to node_id -- a page created with node_id nil and | ||
| 92 | # reassigned afterward would carry a wrong or missing revision number. | ||
| 93 | def save_draft! current_user | ||
| 94 | assert_locked_by! current_user | ||
| 95 | return unless self.autosave | ||
| 96 | |||
| 97 | if self.draft | ||
| 98 | self.draft.clone_attributes_from self.autosave | ||
| 99 | self.draft.user_id = self.autosave.user_id if self.autosave.user_id | ||
| 100 | self.draft.editor = current_user | ||
| 101 | self.draft.save! | ||
| 102 | else | ||
| 103 | empty_page = self.pages.create! | ||
| 104 | empty_page.user = self.autosave.user_id ? self.autosave.user : (self.head ? self.head.user : current_user) | ||
| 105 | empty_page.editor = current_user | ||
| 106 | empty_page.clone_attributes_from self.autosave | ||
| 107 | empty_page.save! | ||
| 108 | self.draft = empty_page | ||
| 109 | self.save! | ||
| 110 | end | ||
| 111 | |||
| 112 | self.autosave.destroy | ||
| 113 | self.autosave_id = nil | ||
| 114 | self.save! | ||
| 115 | self.draft.reload | ||
| 116 | end | ||
| 117 | |||
| 52 | def find_or_create_draft current_user | 118 | def find_or_create_draft current_user |
| 53 | self.wipe_draft! | 119 | self.wipe_draft! |
| 54 | if draft && self.lock_owner == current_user | 120 | if draft && self.lock_owner == current_user |
| @@ -129,8 +195,13 @@ class Node < ApplicationRecord | |||
| 129 | # removes a draft and the lock if it is older than a day and still | 195 | # removes a draft and the lock if it is older than a day and still |
| 130 | # identical to head | 196 | # identical to head |
| 131 | def wipe_draft! | 197 | def wipe_draft! |
| 198 | return if self.autosave && self.autosave.updated_at > 1.day.ago | ||
| 199 | |||
| 132 | unless self.draft | 200 | unless self.draft |
| 201 | self.autosave&.destroy | ||
| 202 | self.autosave_id = nil | ||
| 133 | self.unlock! | 203 | self.unlock! |
| 204 | self.save! | ||
| 134 | return | 205 | return |
| 135 | end | 206 | end |
| 136 | return unless self.head | 207 | return unless self.head |
| @@ -224,6 +295,15 @@ class Node < ApplicationRecord | |||
| 224 | self.save | 295 | self.save |
| 225 | end | 296 | end |
| 226 | 297 | ||
| 298 | def assert_locked_by! current_user | ||
| 299 | return if self.lock_owner == current_user | ||
| 300 | raise( | ||
| 301 | LockedByAnotherUser, | ||
| 302 | "Page is locked by another user who is working on it! " \ | ||
| 303 | "Last modification: #{(self.autosave || self.draft || self.head).updated_at.to_fs(:db)}" | ||
| 304 | ) | ||
| 305 | end | ||
| 306 | |||
| 227 | # Creates an empty page and associates it to the given node. This means | 307 | # Creates an empty page and associates it to the given node. This means |
| 228 | # freshly created node has an empty draft. A user can create nodes as he | 308 | # freshly created node has an empty draft. A user can create nodes as he |
| 229 | # wants to which will not appear on the public page until the author edits | 309 | # wants to which will not appear on the public page until the author edits |
