From f3000a02c5411bf2c9225a534ea8a0d4ac5ead46 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 18 Apr 2025 15:20:38 +0200 Subject: [PATCH] Follow render_card_replacement pattern And clean up the rest of the implementation a tad. --- app/controllers/cards/pins_controller.rb | 24 ++++++++++++------- app/views/cards/pins/create.turbo_stream.erb | 1 - app/views/cards/pins/destroy.turbo_stream.erb | 1 - 3 files changed, 16 insertions(+), 10 deletions(-) delete mode 100644 app/views/cards/pins/create.turbo_stream.erb delete 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 235393178..a4b49433f 100644 --- a/app/controllers/cards/pins_controller.rb +++ b/app/controllers/cards/pins_controller.rb @@ -5,21 +5,29 @@ class Cards::PinsController < ApplicationController end def create - pin = @card.pin_by Current.user - broadcast_add_to_tray pin + @pin = @card.pin_by Current.user + + broadcast_add_pin_to_tray + render_pin_button_replacement end def destroy - pin = @card.unpin_by Current.user - broadcast_remove_from_tray pin + @pin = @card.unpin_by Current.user + + broadcast_remove_pin_from_tray + render_pin_button_replacement end private - def broadcast_add_to_tray(pin) - pin.broadcast_prepend_to [ Current.user, :pins_tray ], target: "pins", partial: "my/pins/pin" + def broadcast_add_pin_to_tray + @pin.broadcast_prepend_to [ Current.user, :pins_tray ], target: "pins", partial: "my/pins/pin" end - def broadcast_remove_from_tray(pin) - pin.broadcast_remove_to [ Current.user, :pins_tray ] + def broadcast_remove_pin_from_tray + @pin.broadcast_remove_to [ Current.user, :pins_tray ] + end + + def render_pin_button_replacement + turbo_stream.replace [ @card, :pin_button ], partial: "cards/pins/pin_button", locals: { card: @card } end end diff --git a/app/views/cards/pins/create.turbo_stream.erb b/app/views/cards/pins/create.turbo_stream.erb deleted file mode 100644 index b1dd787e9..000000000 --- a/app/views/cards/pins/create.turbo_stream.erb +++ /dev/null @@ -1 +0,0 @@ -<%= turbo_stream.replace [ @card, :pin_button ], partial: "cards/pins/pin_button", locals: { card: @card } %> diff --git a/app/views/cards/pins/destroy.turbo_stream.erb b/app/views/cards/pins/destroy.turbo_stream.erb deleted file mode 100644 index d54dea5f1..000000000 --- a/app/views/cards/pins/destroy.turbo_stream.erb +++ /dev/null @@ -1 +0,0 @@ -<%= render template: "cards/pins/create" %>