Skip to content

Commit 2527f07

Browse files
committed
Fix "M" hotkey using stale user from fragment cache
Add a SelfAssignmentsController to resolve the current user at request time, avoiding the use of an incorrect user id baked into a cached URL. ref: #2211 originally added the "M" hotkey ref: https://app.fizzy.do/5986089/cards/3722
1 parent 8ada756 commit 2527f07

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Cards::SelfAssignmentsController < ApplicationController
2+
include CardScoped
3+
4+
def create
5+
if @card.toggle_assignment(Current.user)
6+
respond_to do |format|
7+
format.turbo_stream { render "cards/assignments/create" }
8+
format.json { head :no_content }
9+
end
10+
else
11+
respond_to do |format|
12+
format.turbo_stream { render "cards/assignments/create" }
13+
format.json { head :unprocessable_entity }
14+
end
15+
end
16+
end
17+
end

app/javascript/controllers/card_hotkeys_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export default class extends Controller {
7979
const selection = this.#selectedCard
8080
if (!selection) return
8181

82-
const url = selection.card.dataset.cardAssignToMeUrl
82+
const url = selection.card.dataset.cardAssignToSelfUrl
8383
if (url) {
8484
event.preventDefault()
8585
await post(url, { responseKind: "turbo-stream" })

app/views/cards/display/_preview.html.erb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
<% card_data[:card_not_now_url] = card_not_now_path(card) %>
1212
<% card_data[:card_closure_url] = card_closure_path(card) %>
1313
<% card_data[:action] = "mouseenter->navigable-list#hoverSelect" %>
14-
<% if Current.user %>
15-
<% card_data[:card_assign_to_me_url] = card_assignments_path(card, params: { assignee_id: Current.user.id }) %>
16-
<% end %>
14+
<% card_data[:card_assign_to_self_url] = card_self_assignment_path(card) %>
1715
<% end %>
1816

1917
<%= card_article_tag card, class: "card", draggable: draggable, data: card_data, tabindex: 0 do %>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
resource :reading
8888

8989
resources :assignments
90+
resource :self_assignment, only: :create
9091
resources :steps
9192
resources :taggings
9293

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require "test_helper"
2+
3+
class Cards::SelfAssignmentsControllerTest < ActionDispatch::IntegrationTest
4+
setup do
5+
sign_in_as :kevin
6+
end
7+
8+
test "create assigns to current user" do
9+
card = cards(:layout)
10+
11+
assert_not card.assigned_to?(users(:kevin))
12+
13+
post card_self_assignment_path(card), as: :turbo_stream
14+
assert_response :success
15+
assert_meta_replaced(card)
16+
assert card.reload.assigned_to?(users(:kevin))
17+
end
18+
19+
test "create toggles off when already assigned" do
20+
card = cards(:logo)
21+
22+
assert card.assigned_to?(users(:kevin))
23+
24+
post card_self_assignment_path(card), as: :turbo_stream
25+
assert_response :success
26+
assert_meta_replaced(card)
27+
assert_not card.reload.assigned_to?(users(:kevin))
28+
end
29+
30+
test "create as JSON" do
31+
card = cards(:layout)
32+
33+
assert_not card.assigned_to?(users(:kevin))
34+
35+
post card_self_assignment_path(card), as: :json
36+
assert_response :no_content
37+
assert card.reload.assigned_to?(users(:kevin))
38+
end
39+
40+
private
41+
def assert_meta_replaced(card)
42+
assert_turbo_stream action: :replace, target: dom_id(card, :meta)
43+
end
44+
end

0 commit comments

Comments
 (0)