From 7adbd8d6348e1885bb12a9db2eada25c98df1c9a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 17 Apr 2025 15:20:27 +0200 Subject: [PATCH] Use card rerendering instead of redirects --- app/controllers/cards/assignments_controller.rb | 1 + app/controllers/cards/closures_controller.rb | 4 ++-- app/controllers/cards/engagements_controller.rb | 4 ++-- app/controllers/cards/goldnesses_controller.rb | 5 ----- app/controllers/cards/stagings_controller.rb | 2 ++ app/controllers/cards/taggings_controller.rb | 1 + app/controllers/concerns/card_scoped.rb | 4 ++++ app/views/cards/assignments/create.turbo_stream.erb | 1 - app/views/cards/stagings/create.turbo_stream.erb | 8 -------- app/views/cards/taggings/create.turbo_stream.erb | 1 - test/controllers/cards/assignments_controller_test.rb | 4 ++-- test/controllers/cards/closures_controller_test.rb | 6 ++---- test/controllers/cards/engagements_controller_test.rb | 11 +++++++---- test/controllers/cards/goldnesses_controller_test.rb | 4 ++-- test/controllers/cards/taggings_controller_test.rb | 4 ++-- test/test_helper.rb | 2 +- test/test_helpers/card_test_helper.rb | 5 +++++ 17 files changed, 33 insertions(+), 34 deletions(-) delete mode 100644 app/views/cards/assignments/create.turbo_stream.erb delete mode 100644 app/views/cards/stagings/create.turbo_stream.erb delete mode 100644 app/views/cards/taggings/create.turbo_stream.erb create mode 100644 test/test_helpers/card_test_helper.rb diff --git a/app/controllers/cards/assignments_controller.rb b/app/controllers/cards/assignments_controller.rb index 63dce613c..bba888bf4 100644 --- a/app/controllers/cards/assignments_controller.rb +++ b/app/controllers/cards/assignments_controller.rb @@ -6,5 +6,6 @@ class Cards::AssignmentsController < ApplicationController def create @card.toggle_assignment @collection.users.active.find(params[:assignee_id]) + rerender_card_container end end diff --git a/app/controllers/cards/closures_controller.rb b/app/controllers/cards/closures_controller.rb index 3682552ee..f79608036 100644 --- a/app/controllers/cards/closures_controller.rb +++ b/app/controllers/cards/closures_controller.rb @@ -3,11 +3,11 @@ class Cards::ClosuresController < ApplicationController def create @card.close(user: Current.user, reason: params[:reason]) - redirect_to @card + rerender_card_container end def destroy @card.reopen - redirect_to @card + rerender_card_container end end diff --git a/app/controllers/cards/engagements_controller.rb b/app/controllers/cards/engagements_controller.rb index 72025d16a..e1178f1b3 100644 --- a/app/controllers/cards/engagements_controller.rb +++ b/app/controllers/cards/engagements_controller.rb @@ -3,11 +3,11 @@ class Cards::EngagementsController < ApplicationController def create @card.engage - redirect_to @card + rerender_card_container end def destroy @card.reconsider - redirect_to @card + rerender_card_container end end diff --git a/app/controllers/cards/goldnesses_controller.rb b/app/controllers/cards/goldnesses_controller.rb index 2c37ad80a..fb6076eec 100644 --- a/app/controllers/cards/goldnesses_controller.rb +++ b/app/controllers/cards/goldnesses_controller.rb @@ -10,9 +10,4 @@ class Cards::GoldnessesController < ApplicationController @card.ungild rerender_card_container end - - private - def rerender_card_container - render turbo_stream: turbo_stream.replace([ @card, :card_container ], partial: "cards/container", locals: { card: @card.reload }) - end end diff --git a/app/controllers/cards/stagings_controller.rb b/app/controllers/cards/stagings_controller.rb index 071d19f5a..728cad8a5 100644 --- a/app/controllers/cards/stagings_controller.rb +++ b/app/controllers/cards/stagings_controller.rb @@ -9,6 +9,8 @@ class Cards::StagingsController < ApplicationController else @card.update!(stage: nil) end + + rerender_card_container end private diff --git a/app/controllers/cards/taggings_controller.rb b/app/controllers/cards/taggings_controller.rb index 710b59fdd..a44b8a4df 100644 --- a/app/controllers/cards/taggings_controller.rb +++ b/app/controllers/cards/taggings_controller.rb @@ -7,6 +7,7 @@ class Cards::TaggingsController < ApplicationController def create @card.toggle_tag_with sanitized_tag_title_param + rerender_card_container end private diff --git a/app/controllers/concerns/card_scoped.rb b/app/controllers/concerns/card_scoped.rb index ef40d56e3..e89bf7f4d 100644 --- a/app/controllers/concerns/card_scoped.rb +++ b/app/controllers/concerns/card_scoped.rb @@ -13,4 +13,8 @@ module CardScoped def set_collection @collection = @card.collection end + + def rerender_card_container + render turbo_stream: turbo_stream.replace([ @card, :card_container ], partial: "cards/container", locals: { card: @card.reload }) + end end diff --git a/app/views/cards/assignments/create.turbo_stream.erb b/app/views/cards/assignments/create.turbo_stream.erb deleted file mode 100644 index f05f15d8d..000000000 --- a/app/views/cards/assignments/create.turbo_stream.erb +++ /dev/null @@ -1 +0,0 @@ -<%= turbo_stream.replace([ @card, :meta ], partial: "cards/display/perma/meta", locals: { card: @card }) %> diff --git a/app/views/cards/stagings/create.turbo_stream.erb b/app/views/cards/stagings/create.turbo_stream.erb deleted file mode 100644 index ac3942d6a..000000000 --- a/app/views/cards/stagings/create.turbo_stream.erb +++ /dev/null @@ -1,8 +0,0 @@ -<%= turbo_stream.replace dom_id(@card, "stages") do %> - <%= render partial: "cards/stagings/stages", locals: { card: @card } %> -<% end %> - -<%= turbo_stream.set_css_variable dom_id(@card, :card_closure_toggle), name: "--card-color", value: @card.color %> -<%= turbo_stream.set_css_variable dom_id(@card, :card_container), name: "--card-color", value: @card.color %> -<%= turbo_stream.set_css_variable dom_id(@card, :card_engagement_toggle), name: "--card-color", value: @card.color %> -<%= turbo_stream.set_css_variable dom_id(@card, :ticket), name: "--card-color", value: @card.color %> diff --git a/app/views/cards/taggings/create.turbo_stream.erb b/app/views/cards/taggings/create.turbo_stream.erb deleted file mode 100644 index f1775e2f8..000000000 --- a/app/views/cards/taggings/create.turbo_stream.erb +++ /dev/null @@ -1 +0,0 @@ -<%= turbo_stream.replace([ @card, :tags ], partial: "cards/display/perma/tags", locals: { card: @card }) %> diff --git a/test/controllers/cards/assignments_controller_test.rb b/test/controllers/cards/assignments_controller_test.rb index c92f57627..e0629aee4 100644 --- a/test/controllers/cards/assignments_controller_test.rb +++ b/test/controllers/cards/assignments_controller_test.rb @@ -13,12 +13,12 @@ class Cards::AssignmentsControllerTest < ActionDispatch::IntegrationTest test "create" do assert_changes "cards(:logo).assigned_to?(users(:david))", from: false, to: true do post card_assignments_path(cards(:logo)), params: { assignee_id: users(:david).id }, as: :turbo_stream + assert_card_container_rerendered(cards(:logo)) end - assert_response :success assert_changes "cards(:logo).assigned_to?(users(:david))", from: true, to: false do post card_assignments_path(cards(:logo)), params: { assignee_id: users(:david).id }, as: :turbo_stream + assert_card_container_rerendered(cards(:logo)) end - assert_response :success end end diff --git a/test/controllers/cards/closures_controller_test.rb b/test/controllers/cards/closures_controller_test.rb index 1729010a9..1e8f85871 100644 --- a/test/controllers/cards/closures_controller_test.rb +++ b/test/controllers/cards/closures_controller_test.rb @@ -10,11 +10,10 @@ class Cards::ClosuresControllerTest < ActionDispatch::IntegrationTest assert_changes -> { card.reload.closed? }, from: false, to: true do post card_closure_path(card, reason: "Scope too big") + assert_card_container_rerendered(card) end assert_equal "Scope too big", card.closure.reason - - assert_redirected_to collection_card_path(card.collection, card) end test "destroy" do @@ -22,8 +21,7 @@ class Cards::ClosuresControllerTest < ActionDispatch::IntegrationTest assert_changes -> { card.reload.closed? }, from: true, to: false do delete card_closure_path(card) + assert_card_container_rerendered(card) end - - assert_redirected_to collection_card_path(card.collection, card) end end diff --git a/test/controllers/cards/engagements_controller_test.rb b/test/controllers/cards/engagements_controller_test.rb index 1e966108c..6b4265b86 100644 --- a/test/controllers/cards/engagements_controller_test.rb +++ b/test/controllers/cards/engagements_controller_test.rb @@ -10,9 +10,8 @@ class Cards::EngagementsControllerTest < ActionDispatch::IntegrationTest assert_changes -> { card.reload.doing? }, from: false, to: true do post card_engagement_path(card) + assert_card_container_rerendered(card) end - - assert_redirected_to collection_card_path(card.collection, card) end test "destroy" do @@ -20,8 +19,12 @@ class Cards::EngagementsControllerTest < ActionDispatch::IntegrationTest assert_changes -> { card.reload.doing? }, from: true, to: false do delete card_engagement_path(card) + assert_card_container_rerendered(card) end - - assert_redirected_to collection_card_path(card.collection, card) end + + private + def assert_card_container_rerendered(card) + assert_turbo_stream action: :replace, target: dom_id(card, :card_container) + end end diff --git a/test/controllers/cards/goldnesses_controller_test.rb b/test/controllers/cards/goldnesses_controller_test.rb index 50fd411b0..447aa2b71 100644 --- a/test/controllers/cards/goldnesses_controller_test.rb +++ b/test/controllers/cards/goldnesses_controller_test.rb @@ -8,14 +8,14 @@ class Cards::GoldnessesControllerTest < ActionDispatch::IntegrationTest test "create" do assert_changes -> { cards(:text).reload.golden? }, from: false, to: true do post card_goldness_path(cards(:text)), as: :turbo_stream - assert_response :success + assert_card_container_rerendered(cards(:text)) end end test "destroy" do assert_changes -> { cards(:logo).reload.golden? }, from: true, to: false do delete card_goldness_path(cards(:logo)), as: :turbo_stream - assert_response :success + assert_card_container_rerendered(cards(:logo)) end end end diff --git a/test/controllers/cards/taggings_controller_test.rb b/test/controllers/cards/taggings_controller_test.rb index ad5a8fa91..60f4cf743 100644 --- a/test/controllers/cards/taggings_controller_test.rb +++ b/test/controllers/cards/taggings_controller_test.rb @@ -13,14 +13,14 @@ class Cards::TaggingsControllerTest < ActionDispatch::IntegrationTest test "toggle tag on" do assert_changes "cards(:logo).tagged_with?(tags(:mobile))", from: false, to: true do post card_taggings_path(cards(:logo)), params: { tag_title: tags(:mobile).title }, as: :turbo_stream + assert_card_container_rerendered(cards(:logo)) end - assert_response :success end test "toggle tag off" do assert_changes "cards(:logo).tagged_with?(tags(:web))", from: true, to: false do post card_taggings_path(cards(:logo)), params: { tag_title: tags(:web).title }, as: :turbo_stream + assert_card_container_rerendered(cards(:logo)) end - assert_response :success end end diff --git a/test/test_helper.rb b/test/test_helper.rb index f7cc1e088..1447430b1 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,6 +10,6 @@ module ActiveSupport # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all - include ChangeTestHelper, SessionTestHelper + include CardTestHelper, ChangeTestHelper, SessionTestHelper end end diff --git a/test/test_helpers/card_test_helper.rb b/test/test_helpers/card_test_helper.rb new file mode 100644 index 000000000..6de139c86 --- /dev/null +++ b/test/test_helpers/card_test_helper.rb @@ -0,0 +1,5 @@ +module CardTestHelper + def assert_card_container_rerendered(card) + assert_turbo_stream action: :replace, target: dom_id(card, :card_container) + end +end