summaryrefslogtreecommitdiff
path: root/app/models/node.rb
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-07-14 14:31:18 +0200
committererdgeist <erdgeist@erdgeist.org>2026-07-14 14:31:18 +0200
commitcca0e57b21d506cd341b927984bcb68f0e461783 (patch)
tree2d49cf1b678a3f5cf0fd20cd214b20524027ec50 /app/models/node.rb
parent6b302d263ac66b521a743f1e8e1cc82997b54b5d (diff)
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).
Diffstat (limited to 'app/models/node.rb')
-rw-r--r--app/models/node.rb51
1 files changed, 0 insertions, 51 deletions
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
145 pairs 145 pairs
146 end 146 end
147 147
148 def find_or_create_draft current_user
149 self.wipe_draft!
150 if draft && self.lock_owner == current_user
151 draft
152 elsif draft && self.lock_owner.nil?
153 lock_for! current_user
154 draft.user = current_user if draft.user.nil?
155 draft.editor = current_user
156 draft.save
157 draft
158 elsif draft && self.lock_owner != current_user
159 raise(
160 LockedByAnotherUser,
161 "Page is locked by another user who is working on it! " \
162 "Last modification: #{draft.updated_at.to_fs(:db)}"
163 )
164 else
165 lock_for! current_user
166 create_new_draft current_user
167 end
168 end
169
170 def create_new_draft user 148 def create_new_draft user
171 empty_page = self.pages.create! 149 empty_page = self.pages.create!
172 empty_page.user = (self.head ? self.head.user : user) 150 empty_page.user = (self.head ? self.head.user : user)
@@ -249,35 +227,6 @@ class Node < ApplicationRecord
249 self 227 self
250 end 228 end
251 229
252 # Releases whatever's stale and abandoned; never anything actively in
253 # use. Three independent cases share one rule -- nothing is touched
254 # unless it's been sitting untouched for over a day:
255 # - a lock held with no draft or autosave at all (the editor opened
256 # the page and never actually wrote anything)
257 # - a fresh autosave older than a day, never promoted to a draft
258 # - a draft older than a day that's still identical to head
259 def wipe_draft!
260 return if self.autosave && self.autosave.updated_at > 1.day.ago
261
262 unless self.draft
263 return if self.autosave.nil? && self.locked? && self.updated_at > 1.day.ago
264 self.autosave&.destroy
265 self.autosave_id = nil
266 self.unlock!
267 self.save!
268 return
269 end
270 return unless self.head
271 return unless self.draft.updated_at < 1.day.ago
272 return if self.head.has_changes_to? self.draft
273
274 self.draft.destroy
275 self.draft_id = nil
276 self.unlock!
277 self.save!
278 self.reload
279 end
280
281 def restore_revision! revision 230 def restore_revision! revision
282 if page = self.pages.find_by_revision(revision) 231 if page = self.pages.find_by_revision(revision)
283 self.head = page 232 self.head = page