From fc0cf3917f016e0fe00fc7c340ed1d4dff262b7f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 17 Apr 2025 14:05:26 +0200 Subject: [PATCH] Pin without redirect Use turbo stream response to save on the redirect latency --- app/controllers/cards/pins_controller.rb | 4 ---- app/views/cards/pins/_pin.html.erb | 11 +++++++++++ app/views/cards/pins/create.turbo_stream.erb | 1 + app/views/cards/pins/destroy.turbo_stream.erb | 1 + app/views/cards/pins/show.html.erb | 12 +----------- test/controllers/cards/pins_controller_test.rb | 8 ++++---- 6 files changed, 18 insertions(+), 19 deletions(-) create mode 100644 app/views/cards/pins/_pin.html.erb create mode 100644 app/views/cards/pins/create.turbo_stream.erb create mode 100644 app/views/cards/pins/destroy.turbo_stream.erb diff --git a/app/controllers/cards/pins_controller.rb b/app/controllers/cards/pins_controller.rb index 1f6c1aaa8..c6317f095 100644 --- a/app/controllers/cards/pins_controller.rb +++ b/app/controllers/cards/pins_controller.rb @@ -6,16 +6,12 @@ class Cards::PinsController < ApplicationController def create pin = @card.pin_by Current.user - broadcast_my_new pin - redirect_to card_pin_path(@card) end def destroy pin = @card.unpin_by Current.user - broadcast_my_removed pin - redirect_to card_pin_path(@card) end private diff --git a/app/views/cards/pins/_pin.html.erb b/app/views/cards/pins/_pin.html.erb new file mode 100644 index 000000000..e08308829 --- /dev/null +++ b/app/views/cards/pins/_pin.html.erb @@ -0,0 +1,11 @@ +
+ <% if card.pinned_by? Current.user %> + <%= button_to card_pin_path(card), method: :delete, class: "btn btn--reversed" do %> + <%= icon_tag "pinned" %> Un-pin this card + <% end %> + <% else %> + <%= button_to card_pin_path(card), class: "btn" do %> + <%= icon_tag "unpinned" %> Pin this card + <% end %> + <% end %> +
diff --git a/app/views/cards/pins/create.turbo_stream.erb b/app/views/cards/pins/create.turbo_stream.erb new file mode 100644 index 000000000..752fc594d --- /dev/null +++ b/app/views/cards/pins/create.turbo_stream.erb @@ -0,0 +1 @@ +<%= turbo_stream.replace [ @card, :pin_button ], partial: "cards/pins/pin", locals: { card: @card } %> diff --git a/app/views/cards/pins/destroy.turbo_stream.erb b/app/views/cards/pins/destroy.turbo_stream.erb new file mode 100644 index 000000000..752fc594d --- /dev/null +++ b/app/views/cards/pins/destroy.turbo_stream.erb @@ -0,0 +1 @@ +<%= turbo_stream.replace [ @card, :pin_button ], partial: "cards/pins/pin", locals: { card: @card } %> diff --git a/app/views/cards/pins/show.html.erb b/app/views/cards/pins/show.html.erb index 64eb1ad5c..0343f1b68 100644 --- a/app/views/cards/pins/show.html.erb +++ b/app/views/cards/pins/show.html.erb @@ -1,13 +1,3 @@ <%= turbo_frame_tag dom_id(@card, :pin) do %> - <% if @card.pinned_by? Current.user %> - <%= button_to card_pin_path(@card), method: :delete, class: "btn btn--reversed" do %> - <%= icon_tag "pinned" %> - Un-pin this card - <% end %> - <% else %> - <%= button_to card_pin_path(@card), class: "btn" do %> - <%= icon_tag "unpinned" %> - Pin this card - <% end %> - <% end %> + <%= render "cards/pins/pin", card: @card %> <% end %> diff --git a/test/controllers/cards/pins_controller_test.rb b/test/controllers/cards/pins_controller_test.rb index 39b12a053..82f0534c5 100644 --- a/test/controllers/cards/pins_controller_test.rb +++ b/test/controllers/cards/pins_controller_test.rb @@ -9,23 +9,23 @@ class Cards::PinsControllerTest < ActionDispatch::IntegrationTest assert_changes -> { cards(:layout).pinned_by?(users(:kevin)) }, from: false, to: true do perform_enqueued_jobs do assert_turbo_stream_broadcasts([ users(:kevin), :pins ], count: 1) do - post card_pin_path(cards(:layout)) + post card_pin_path(cards(:layout)), as: :turbo_stream end end end - assert_redirected_to card_pin_path(cards(:layout)) + assert_response :success end test "destroy" do assert_changes -> { cards(:shipping).pinned_by?(users(:kevin)) }, from: true, to: false do perform_enqueued_jobs do assert_turbo_stream_broadcasts([ users(:kevin), :pins ], count: 1) do - delete card_pin_path(cards(:shipping)) + delete card_pin_path(cards(:shipping)), as: :turbo_stream end end end - assert_redirected_to card_pin_path(cards(:shipping)) + assert_response :success end end