diff options
| -rw-r--r-- | app/models/node.rb | 80 | ||||
| -rw-r--r-- | db/migrate/20260708095943_add_autosave_id_to_nodes.rb | 5 | ||||
| -rw-r--r-- | test/models/node_test.rb | 96 |
3 files changed, 181 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 |
diff --git a/db/migrate/20260708095943_add_autosave_id_to_nodes.rb b/db/migrate/20260708095943_add_autosave_id_to_nodes.rb new file mode 100644 index 0000000..eedcc00 --- /dev/null +++ b/db/migrate/20260708095943_add_autosave_id_to_nodes.rb | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | class AddAutosaveIdToNodes < ActiveRecord::Migration[8.1] | ||
| 2 | def change | ||
| 3 | add_column :nodes, :autosave_id, :integer | ||
| 4 | end | ||
| 5 | end | ||
diff --git a/test/models/node_test.rb b/test/models/node_test.rb index 514ba3f..2953f8f 100644 --- a/test/models/node_test.rb +++ b/test/models/node_test.rb | |||
| @@ -98,6 +98,7 @@ class NodeTest < ActiveSupport::TestCase | |||
| 98 | assert_not_nil node.draft | 98 | assert_not_nil node.draft |
| 99 | assert_nil node.draft.user | 99 | assert_nil node.draft.user |
| 100 | assert_nil node.head | 100 | assert_nil node.head |
| 101 | assert_nil node.autosave | ||
| 101 | end | 102 | end |
| 102 | 103 | ||
| 103 | def test_create_new_draft_of_published_page | 104 | def test_create_new_draft_of_published_page |
| @@ -280,6 +281,101 @@ class NodeTest < ActiveSupport::TestCase | |||
| 280 | node = Node.root.children.create( :slug => "wow" ) | 281 | node = Node.root.children.create( :slug => "wow" ) |
| 281 | assert_nil node.draft.published_at | 282 | assert_nil node.draft.published_at |
| 282 | end | 283 | end |
| 284 | |||
| 285 | test "lock_for_editing! acquires the lock without creating a draft or autosave" do | ||
| 286 | node = create_node_with_published_page | ||
| 287 | |||
| 288 | node.lock_for_editing!(@user1) | ||
| 289 | |||
| 290 | assert_equal @user1, node.lock_owner | ||
| 291 | assert_nil node.draft | ||
| 292 | assert_nil node.autosave | ||
| 293 | end | ||
| 294 | |||
| 295 | test "autosave! creates a buffer that never appears among a node's pages, leaving the draft untouched" do | ||
| 296 | node = create_node_with_draft | ||
| 297 | node.lock_for_editing!(@user1) | ||
| 298 | page_count_before = node.pages.count | ||
| 299 | |||
| 300 | node.autosave!({ :title => "in progress" }, @user1) | ||
| 301 | node.reload | ||
| 302 | |||
| 303 | assert_not_nil node.autosave | ||
| 304 | assert_nil node.autosave.node_id | ||
| 305 | assert_equal page_count_before, node.pages.count | ||
| 306 | assert_not_equal "in progress", node.draft.title | ||
| 307 | end | ||
| 308 | |||
| 309 | test "save_draft! promotes an autosave into an existing draft without creating a new revision" do | ||
| 310 | node = create_node_with_draft | ||
| 311 | node.lock_for_editing!(@user1) | ||
| 312 | node.autosave!({ :title => "in progress" }, @user1) | ||
| 313 | page_count_before = node.pages.count | ||
| 314 | |||
| 315 | node.save_draft!(@user1) | ||
| 316 | node.reload | ||
| 317 | |||
| 318 | assert_nil node.autosave | ||
| 319 | assert_equal "in progress", node.draft.title | ||
| 320 | assert_equal page_count_before, node.pages.count | ||
| 321 | end | ||
| 322 | |||
| 323 | test "save_draft! promotes an autosave into a brand new, correctly-revisioned draft when none exists" do | ||
| 324 | node = create_node_with_published_page | ||
| 325 | head_revision = node.head.revision | ||
| 326 | |||
| 327 | node.lock_for_editing!(@user1) | ||
| 328 | node.autosave!({ :title => "updated version" }, @user1) | ||
| 329 | node.reload | ||
| 330 | |||
| 331 | assert_nil node.draft | ||
| 332 | assert_nil node.autosave.node_id | ||
| 333 | |||
| 334 | node.save_draft!(@user1) | ||
| 335 | node.reload | ||
| 336 | |||
| 337 | assert_not_nil node.draft | ||
| 338 | assert_equal head_revision + 1, node.draft.revision | ||
| 339 | assert_equal head_revision, node.head.revision | ||
| 340 | assert_nil node.autosave | ||
| 341 | assert_equal 2, node.pages.count | ||
| 342 | end | ||
| 343 | |||
| 344 | test "autosave!, save_draft!, and lock_for_editing! raise LockedByAnotherUser for a second user" do | ||
| 345 | node = create_node_with_published_page | ||
| 346 | node.lock_for_editing!(@user1) | ||
| 347 | |||
| 348 | assert_raise(LockedByAnotherUser) { node.autosave!({ :title => "x" }, @user2) } | ||
| 349 | assert_raise(LockedByAnotherUser) { node.save_draft!(@user2) } | ||
| 350 | assert_raise(LockedByAnotherUser) { node.lock_for_editing!(@user2) } | ||
| 351 | |||
| 352 | assert_equal @user1, node.reload.lock_owner | ||
| 353 | end | ||
| 354 | |||
| 355 | test "wipe_draft! leaves a fresh autosave and its lock untouched" do | ||
| 356 | node = create_node_with_published_page | ||
| 357 | node.lock_for_editing!(@user1) | ||
| 358 | node.autosave!({ :title => "still typing" }, @user1) | ||
| 359 | |||
| 360 | node.wipe_draft! | ||
| 361 | node.reload | ||
| 362 | |||
| 363 | assert_not_nil node.autosave | ||
| 364 | assert_equal @user1, node.lock_owner | ||
| 365 | end | ||
| 366 | |||
| 367 | test "wipe_draft! destroys a stale, orphaned autosave and releases its lock" do | ||
| 368 | node = create_node_with_published_page | ||
| 369 | node.lock_for_editing!(@user1) | ||
| 370 | node.autosave!({ :title => "abandoned mid-session" }, @user1) | ||
| 371 | node.autosave.update_column(:updated_at, 2.days.ago) | ||
| 372 | |||
| 373 | node.wipe_draft! | ||
| 374 | node.reload | ||
| 375 | |||
| 376 | assert_nil node.autosave | ||
| 377 | assert_nil node.lock_owner | ||
| 378 | end | ||
| 283 | 379 | ||
| 284 | def create_revisions node, count | 380 | def create_revisions node, count |
| 285 | count.times do | 381 | count.times do |
