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
This commit is contained in:
Mike Dalessio
2026-01-16 13:41:30 -05:00
parent 8ada756f33
commit 2527f07365
5 changed files with 64 additions and 4 deletions
@@ -0,0 +1,17 @@
class Cards::SelfAssignmentsController < ApplicationController
include CardScoped
def create
if @card.toggle_assignment(Current.user)
respond_to do |format|
format.turbo_stream { render "cards/assignments/create" }
format.json { head :no_content }
end
else
respond_to do |format|
format.turbo_stream { render "cards/assignments/create" }
format.json { head :unprocessable_entity }
end
end
end
end
@@ -79,7 +79,7 @@ export default class extends Controller {
const selection = this.#selectedCard const selection = this.#selectedCard
if (!selection) return if (!selection) return
const url = selection.card.dataset.cardAssignToMeUrl const url = selection.card.dataset.cardAssignToSelfUrl
if (url) { if (url) {
event.preventDefault() event.preventDefault()
await post(url, { responseKind: "turbo-stream" }) await post(url, { responseKind: "turbo-stream" })
+1 -3
View File
@@ -11,9 +11,7 @@
<% card_data[:card_not_now_url] = card_not_now_path(card) %> <% card_data[:card_not_now_url] = card_not_now_path(card) %>
<% card_data[:card_closure_url] = card_closure_path(card) %> <% card_data[:card_closure_url] = card_closure_path(card) %>
<% card_data[:action] = "mouseenter->navigable-list#hoverSelect" %> <% card_data[:action] = "mouseenter->navigable-list#hoverSelect" %>
<% if Current.user %> <% card_data[:card_assign_to_self_url] = card_self_assignment_path(card) %>
<% card_data[:card_assign_to_me_url] = card_assignments_path(card, params: { assignee_id: Current.user.id }) %>
<% end %>
<% end %> <% end %>
<%= card_article_tag card, class: "card", draggable: draggable, data: card_data, tabindex: 0 do %> <%= card_article_tag card, class: "card", draggable: draggable, data: card_data, tabindex: 0 do %>
+1
View File
@@ -87,6 +87,7 @@ Rails.application.routes.draw do
resource :reading resource :reading
resources :assignments resources :assignments
resource :self_assignment, only: :create
resources :steps resources :steps
resources :taggings resources :taggings
@@ -0,0 +1,44 @@
require "test_helper"
class Cards::SelfAssignmentsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in_as :kevin
end
test "create assigns to current user" do
card = cards(:layout)
assert_not card.assigned_to?(users(:kevin))
post card_self_assignment_path(card), as: :turbo_stream
assert_response :success
assert_meta_replaced(card)
assert card.reload.assigned_to?(users(:kevin))
end
test "create toggles off when already assigned" do
card = cards(:logo)
assert card.assigned_to?(users(:kevin))
post card_self_assignment_path(card), as: :turbo_stream
assert_response :success
assert_meta_replaced(card)
assert_not card.reload.assigned_to?(users(:kevin))
end
test "create as JSON" do
card = cards(:layout)
assert_not card.assigned_to?(users(:kevin))
post card_self_assignment_path(card), as: :json
assert_response :no_content
assert card.reload.assigned_to?(users(:kevin))
end
private
def assert_meta_replaced(card)
assert_turbo_stream action: :replace, target: dom_id(card, :meta)
end
end