Manage comments with streams

We replacing the full list of comments within a turbo frame, which didn't feel great when you had several comments.
This commit is contained in:
Jorge Manrubia
2025-11-13 17:00:19 +01:00
parent bddbc0c0ac
commit 17876b30d4
9 changed files with 17 additions and 9 deletions
+1 -2
View File
@@ -5,7 +5,7 @@ class Cards::CommentsController < ApplicationController
before_action :ensure_creatorship, only: %i[ edit update destroy ]
def create
@card.comments.create!(comment_params)
@comment = @card.comments.create!(comment_params)
end
def show
@@ -20,7 +20,6 @@ class Cards::CommentsController < ApplicationController
def destroy
@comment.destroy
redirect_to @card
end
private
+1 -1
View File
@@ -1,4 +1,4 @@
<div class="comment comment--new flex-inline align-start full-width">
<div id="<%= dom_id(card, :new_comment) %>" class="comment comment--new flex-inline align-start full-width">
<figure class="comment__avatar flex-item-no-shrink" aria-hidden="true">
<%= avatar_tag Current.user, hidden_for_screen_reader: true %>
</figure>
-1
View File
@@ -1 +0,0 @@
<%= render "cards/messages", card: @card %>
@@ -0,0 +1,7 @@
<%= turbo_stream.before dom_id(@card, :new_comment) do %>
<%= render "cards/comments/comment", comment: @comment %>
<% end %>
<%= turbo_stream.update dom_id(@card, :new_comment) do %>
<%= render "cards/comments/new", card: @card %>
<% end %>
@@ -0,0 +1 @@
<%= turbo_stream.remove [ @comment, :container ] %>
+1 -1
View File
@@ -1,5 +1,5 @@
<%= turbo_frame_tag @comment, :container do %>
<div class="comment flex align-start full-width">
<div id="<%= dom_id(@comment) %>" class="comment flex align-start full-width">
<figure class="comment__avatar flex-item-no-shrink" aria-hidden="true">
<%= avatar_tag @comment.creator, hidden_for_screen_reader: true %>
</figure>
-1
View File
@@ -1 +0,0 @@
<%= render "cards/comments/comment", comment: @comment %>
@@ -0,0 +1,3 @@
<%= turbo_stream.replace [ @comment, :container ] do %>
<%= render "cards/comments/comment", comment: @comment %>
<% end %>
@@ -7,14 +7,14 @@ class Cards::CommentsControllerTest < ActionDispatch::IntegrationTest
test "create" do
assert_difference -> { cards(:logo).comments.count }, +1 do
post card_comments_path(cards(:logo), params: { comment: { body: "Agreed." } })
post card_comments_path(cards(:logo)), params: { comment: { body: "Agreed." } }, as: :turbo_stream
end
assert_response :success
end
test "update" do
put card_comment_path(cards(:logo), comments(:logo_agreement_kevin)), params: { comment: { body: "I've changed my mind" } }
put card_comment_path(cards(:logo), comments(:logo_agreement_kevin)), params: { comment: { body: "I've changed my mind" } }, as: :turbo_stream
assert_response :success
assert_action_text "I've changed my mind", comments(:logo_agreement_kevin).reload.body
@@ -22,7 +22,7 @@ class Cards::CommentsControllerTest < ActionDispatch::IntegrationTest
test "update another user's comment" do
assert_no_changes -> { comments(:logo_agreement_jz).reload.body.to_s } do
put card_comment_path(cards(:logo), comments(:logo_agreement_jz)), params: { comment: { body: "I've changed my mind" } }
put card_comment_path(cards(:logo), comments(:logo_agreement_jz)), params: { comment: { body: "I've changed my mind" } }, as: :turbo_stream
end
assert_response :forbidden