From cca0e57b21d506cd341b927984bcb68f0e461783 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Tue, 14 Jul 2026 14:31:18 +0200 Subject: Retire wipe_draft! and find_or_create_draft from production code Both had already lost their reason to exist as production API: wipe_draft!'s one remaining callsite (nodes#show) was removed two sessions ago, and find_or_create_draft had zero production callers left at all -- confirmed by a fresh grep, not assumed -- every one of its ~65 call sites was test setup, unrelated to what those tests actually cover. wipe_draft! is deleted outright, along with its two tests -- the lock/draft/autosave cleanup it silently performed already has explicit, always-visible manual equivalents (Unlock, Discard Autosave, Destroy Draft), so nothing real is lost. find_or_create_draft moves to test_helper.rb as a plain method on ActiveSupport::TestCase, alongside the create_node_with_draft/ create_node_with_published_page helpers already living there -- extending the framework's own designated test-extension point rather than reopening the Node model from test code. Its three tests of real dispatch behavior (idempotency on repeat calls, and raising when a second user contends for the lock) are kept, since ~65 other tests depend on this helper actually working correctly; only the call syntax changed, from node.find_or_create_draft(user) to find_or_create_draft(node, user). --- app/models/node.rb | 51 --------------------------------------------------- 1 file changed, 51 deletions(-) (limited to 'app/models') diff --git a/app/models/node.rb b/app/models/node.rb index 73eefd1..311c5c0 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -145,28 +145,6 @@ class Node < ApplicationRecord pairs end - def find_or_create_draft current_user - self.wipe_draft! - if draft && self.lock_owner == current_user - draft - elsif draft && self.lock_owner.nil? - lock_for! current_user - draft.user = current_user if draft.user.nil? - draft.editor = current_user - draft.save - draft - elsif draft && self.lock_owner != current_user - raise( - LockedByAnotherUser, - "Page is locked by another user who is working on it! " \ - "Last modification: #{draft.updated_at.to_fs(:db)}" - ) - else - lock_for! current_user - create_new_draft current_user - end - end - def create_new_draft user empty_page = self.pages.create! empty_page.user = (self.head ? self.head.user : user) @@ -249,35 +227,6 @@ class Node < ApplicationRecord self end - # Releases whatever's stale and abandoned; never anything actively in - # use. Three independent cases share one rule -- nothing is touched - # unless it's been sitting untouched for over a day: - # - a lock held with no draft or autosave at all (the editor opened - # the page and never actually wrote anything) - # - a fresh autosave older than a day, never promoted to a draft - # - a draft older than a day that's still identical to head - def wipe_draft! - return if self.autosave && self.autosave.updated_at > 1.day.ago - - unless self.draft - return if self.autosave.nil? && self.locked? && self.updated_at > 1.day.ago - self.autosave&.destroy - self.autosave_id = nil - self.unlock! - self.save! - return - end - return unless self.head - return unless self.draft.updated_at < 1.day.ago - return if self.head.has_changes_to? self.draft - - self.draft.destroy - self.draft_id = nil - self.unlock! - self.save! - self.reload - end - def restore_revision! revision if page = self.pages.find_by_revision(revision) self.head = page -- cgit v1.3