summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-07-08 16:23:01 +0200
committererdgeist <erdgeist@erdgeist.org>2026-07-08 16:23:01 +0200
commitc6bf63a82007c275d13e9e9e0857434b3b7890c0 (patch)
tree21d71937172d756f9af4211e1359647f63a004c5
parenta006440f59bf99380e179cc2963cb98d4d894d3a (diff)
Fix tests for the new autosave routing
Route Save and autosave through the new Node methods update now promotes the current autosave into the draft via save_draft! rather than writing submitted params directly; autosave gets its own PUT member route so PATCH update can mean "deliberate save" specifically, matching every other custom action on this resource. Both now require the lock to already be held rather than acquiring it themselves, which is a deliberate narrowing: through the real UI this is always true, since neither can fire before edit has loaded. Two existing tests called update directly with no prior edit, which the old code tolerated by acquiring the lock as a side effect; updated to call edit first instead of loosening the guarantee back open. NodesController#edit itself is unchanged and still calls the old find_or_create_draft, so drafts are still created eagerly on entering edit for now -- switching that over is the next step, not this one.
-rw-r--r--app/controllers/nodes_controller.rb39
-rw-r--r--test/controllers/nodes_controller_test.rb2
2 files changed, 31 insertions, 10 deletions
diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb
index a72be68..042963b 100644
--- a/app/controllers/nodes_controller.rb
+++ b/app/controllers/nodes_controller.rb
@@ -11,7 +11,8 @@ class NodesController < ApplicationController
11 :update, 11 :update,
12 :destroy, 12 :destroy,
13 :publish, 13 :publish,
14 :unlock 14 :unlock,
15 :autosave
15 ] 16 ]
16 17
17 def index 18 def index
@@ -72,17 +73,35 @@ class NodesController < ApplicationController
72 73
73 def update 74 def update
74 @node.update(node_params) 75 @node.update(node_params)
75 @draft = @node.find_or_create_draft current_user 76 @node.autosave!( page_params.merge(:tag_list => params[:tag_list]), current_user )
76 @draft.tag_list = params[:tag_list] 77 @node.save_draft!(current_user)
77 if @draft.update( page_params ) 78
78 flash[:notice] = "Draft has been saved: #{Time.now}" 79 flash[:notice] = "Draft has been saved: #{Time.now}"
79 respond_to do |format| 80
80 format.html { redirect_to edit_node_path(@node) } 81 if params[:commit] == "Save + Unlock + Exit"
81 format.js 82 @node.unlock!
82 end 83 redirect_to node_path(@node)
83 else 84 else
84 render :action => :edit 85 redirect_to edit_node_path(@node)
85 end 86 end
87 rescue LockedByAnotherUser => e
88 flash[:error] = e.message
89 redirect_to node_path(@node)
90 rescue ActiveRecord::RecordInvalid
91 @page = @node.autosave || @node.draft || @node.head
92 render :action => :edit
93 end
94
95 def autosave
96 @node.update(node_params)
97 @node.autosave!( page_params.merge(:tag_list => params[:tag_list]), current_user )
98 head :ok
99 rescue LockedByAnotherUser => e
100 render plain: e.message, status: :locked
101 rescue ActiveRecord::RecordInvalid => e
102 render plain: e.message, status: :unprocessable_entity
103 rescue StandardError => e
104 render plain: "Autosave failed", status: :internal_server_error
86 end 105 end
87 106
88 def destroy 107 def destroy
diff --git a/test/controllers/nodes_controller_test.rb b/test/controllers/nodes_controller_test.rb
index 61f7db4..f0be8c9 100644
--- a/test/controllers/nodes_controller_test.rb
+++ b/test/controllers/nodes_controller_test.rb
@@ -123,6 +123,7 @@ class NodesControllerTest < ActionController::TestCase
123 def test_update_a_draft 123 def test_update_a_draft
124 test_node = Node.root.children.create! :slug => "test_node" 124 test_node = Node.root.children.create! :slug => "test_node"
125 login_as :quentin 125 login_as :quentin
126 get :edit, params: { :id => test_node.id }
126 put :update, params: { :id => test_node.id, :page => {:title => "Hello", :body => "There"} } 127 put :update, params: { :id => test_node.id, :page => {:title => "Hello", :body => "There"} }
127 test_node.reload 128 test_node.reload
128 assert_equal "Hello", test_node.draft.title 129 assert_equal "Hello", test_node.draft.title
@@ -133,6 +134,7 @@ class NodesControllerTest < ActionController::TestCase
133 test_node = Node.root.children.create! :slug => "test_node" 134 test_node = Node.root.children.create! :slug => "test_node"
134 135
135 login_as :quentin 136 login_as :quentin
137 get :edit, params: { :id => test_node.id }
136 put :update, params: { 138 put :update, params: {
137 :id => test_node.id, 139 :id => test_node.id,
138 :page => { 140 :page => {