From f4ce57e30880df7dac6969d21baad26aac7a53e9 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 11 Apr 2025 16:21:44 +0200 Subject: [PATCH] Move comments under cards --- .../comments/reactions_controller.rb | 6 ++-- .../{ => cards}/comments_controller.rb | 2 +- app/models/comment.rb | 4 +++ app/views/cards/_messages.html.erb | 2 +- app/views/{ => cards}/comments/_body.html.erb | 6 ++-- .../{ => cards}/comments/_comment.html.erb | 2 +- app/views/{ => cards}/comments/_new.html.erb | 2 +- .../{ => cards}/comments/create.html.erb | 0 app/views/{ => cards}/comments/edit.html.erb | 6 ++-- .../comments/reactions/_reaction.html.erb | 2 +- .../comments/reactions/_reactions.html.erb | 4 +-- .../cards/comments/reactions/index.html.erb | 1 + .../comments/reactions/new.html.erb | 4 +-- app/views/cards/comments/show.html.erb | 1 + app/views/cards/comments/update.html.erb | 1 + app/views/comments/reactions/index.html.erb | 1 - app/views/comments/show.html.erb | 1 - app/views/comments/update.html.erb | 1 - app/views/messages/_message.html.erb | 2 +- config/routes.rb | 10 +++---- .../comments/reactions_controller_test.rb | 26 ++++++++++++++++ .../cards/comments_controller_test.rb | 30 +++++++++++++++++++ test/controllers/comments_controller_test.rb | 30 ------------------- test/controllers/reactions_controller_test.rb | 26 ---------------- 24 files changed, 87 insertions(+), 83 deletions(-) rename app/controllers/{ => cards}/comments/reactions_controller.rb (72%) rename app/controllers/{ => cards}/comments_controller.rb (93%) rename app/views/{ => cards}/comments/_body.html.erb (73%) rename app/views/{ => cards}/comments/_comment.html.erb (77%) rename app/views/{ => cards}/comments/_new.html.erb (89%) rename app/views/{ => cards}/comments/create.html.erb (100%) rename app/views/{ => cards}/comments/edit.html.erb (73%) rename app/views/{ => cards}/comments/reactions/_reaction.html.erb (92%) rename app/views/{ => cards}/comments/reactions/_reactions.html.erb (70%) create mode 100644 app/views/cards/comments/reactions/index.html.erb rename app/views/{ => cards}/comments/reactions/new.html.erb (84%) create mode 100644 app/views/cards/comments/show.html.erb create mode 100644 app/views/cards/comments/update.html.erb delete mode 100644 app/views/comments/reactions/index.html.erb delete mode 100644 app/views/comments/show.html.erb delete mode 100644 app/views/comments/update.html.erb create mode 100644 test/controllers/cards/comments/reactions_controller_test.rb create mode 100644 test/controllers/cards/comments_controller_test.rb delete mode 100644 test/controllers/comments_controller_test.rb delete mode 100644 test/controllers/reactions_controller_test.rb diff --git a/app/controllers/comments/reactions_controller.rb b/app/controllers/cards/comments/reactions_controller.rb similarity index 72% rename from app/controllers/comments/reactions_controller.rb rename to app/controllers/cards/comments/reactions_controller.rb index 17da85753..00f0ca8a9 100644 --- a/app/controllers/comments/reactions_controller.rb +++ b/app/controllers/cards/comments/reactions_controller.rb @@ -1,4 +1,4 @@ -class Comments::ReactionsController < ApplicationController +class Cards::Comments::ReactionsController < ApplicationController before_action :set_comment def index @@ -12,7 +12,7 @@ class Comments::ReactionsController < ApplicationController @reaction = @comment.reactions.create!(reaction_params) broadcast_create - redirect_to collection_card_comment_reactions_url(@comment.card.collection, @comment.card, @comment) + redirect_to card_comment_reactions_url(@comment.card, @comment) end def destroy @@ -33,7 +33,7 @@ class Comments::ReactionsController < ApplicationController def broadcast_create @reaction.broadcast_append_to @reaction.comment, :comments, - target: "reactions_comment_#{@comment.id}", partial: "comments/reactions/reaction", locals: { comment: @comment } + target: "reactions_comment_#{@comment.id}", partial: "cards/comments/reactions/reaction", locals: { comment: @comment } end def broadcast_remove diff --git a/app/controllers/comments_controller.rb b/app/controllers/cards/comments_controller.rb similarity index 93% rename from app/controllers/comments_controller.rb rename to app/controllers/cards/comments_controller.rb index 6d9540662..4f1745602 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/cards/comments_controller.rb @@ -1,4 +1,4 @@ -class CommentsController < ApplicationController +class Cards::CommentsController < ApplicationController include CardScoped before_action :set_comment, only: [ :show, :edit, :update, :destroy ] before_action :require_own_comment, only: [ :edit, :update, :destroy ] diff --git a/app/models/comment.rb b/app/models/comment.rb index 1f394aacd..519fc9a72 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -18,6 +18,10 @@ class Comment < ApplicationRecord card_comments.many? && card_comments_prior.last&.creator != creator end + def to_partial_path + "cards/#{super}" + end + private def cleanup_events # Delete events that reference through event_summary diff --git a/app/views/cards/_messages.html.erb b/app/views/cards/_messages.html.erb index 5b7ed1ef5..ea6cbf0df 100644 --- a/app/views/cards/_messages.html.erb +++ b/app/views/cards/_messages.html.erb @@ -1,6 +1,6 @@ <%= messages_tag(card) do %> <%= render partial: "messages/message", collection: card.messages, cached: true %> - <%= render "comments/new", card: card, cached: true %> + <%= render "cards/comments/new", card: card, cached: true %>
Subscribers diff --git a/app/views/comments/_body.html.erb b/app/views/cards/comments/_body.html.erb similarity index 73% rename from app/views/comments/_body.html.erb rename to app/views/cards/comments/_body.html.erb index 4c58dfa51..a8f800f1a 100644 --- a/app/views/comments/_body.html.erb +++ b/app/views/cards/comments/_body.html.erb @@ -6,11 +6,11 @@ <%= link_to comment.creator.name, user_path(comment.creator), class: "txt-ink btn btn--plain fill-transparent", data: { turbo_frame: "_top" } %> - <%= link_to collection_card_path(comment.card.collection, comment.card, anchor: "comment_#{comment.id}"), class: "txt-undecorated txt-uppercase" do %> + <%= link_to card_path(comment.card, anchor: "comment_#{comment.id}"), class: "txt-undecorated txt-uppercase" do %> <%= local_datetime_tag comment.created_at, style: :shortdate, class: "txt-ink translucent" %> <% end %> - <%= link_to edit_collection_card_comment_path(comment.card.collection, comment.card, comment), + <%= link_to edit_card_comment_path(comment.card, comment), class: "comment__edit btn btn--plain txt-xx-small fill-transparent translucent flex-item-justify-end" do %> <%= icon_tag "menu-dots-horizontal" %> Edit this comment @@ -20,7 +20,7 @@ <%= sanitize comment.body_html %>
- <%= render "comments/reactions/reactions", comment: comment %> + <%= render "cards/comments/reactions/reactions", comment: comment %> <% end %> <% end %> diff --git a/app/views/comments/_comment.html.erb b/app/views/cards/comments/_comment.html.erb similarity index 77% rename from app/views/comments/_comment.html.erb rename to app/views/cards/comments/_comment.html.erb index f90ed8ee6..c866f30f0 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/cards/comments/_comment.html.erb @@ -3,5 +3,5 @@ <%= avatar_tag comment.creator, hidden_for_screen_reader: true %> - <%= render "comments/body", comment: comment %> + <%= render "cards/comments/body", comment: comment %> <% end %> diff --git a/app/views/comments/_new.html.erb b/app/views/cards/comments/_new.html.erb similarity index 89% rename from app/views/comments/_new.html.erb rename to app/views/cards/comments/_new.html.erb index 8c75edcf1..2352d1797 100644 --- a/app/views/comments/_new.html.erb +++ b/app/views/cards/comments/_new.html.erb @@ -5,7 +5,7 @@
- <%= form_with model: Comment.new, url: collection_card_comments_path(card.collection, card), class: "flex flex-column gap full-width", + <%= form_with model: Comment.new, url: card_comments_path(card), class: "flex flex-column gap full-width", data: { controller: "form paste local-save", local_save_key_value: "comment-#{card.id}", action: "turbo:submit-end->local-save#submit keydown.ctrl+enter->form#submit:prevent keydown.meta+enter->form#submit:prevent keydown.esc->form#cancel:stop paste->paste#pasteFiles" } do |form| %> diff --git a/app/views/comments/create.html.erb b/app/views/cards/comments/create.html.erb similarity index 100% rename from app/views/comments/create.html.erb rename to app/views/cards/comments/create.html.erb diff --git a/app/views/comments/edit.html.erb b/app/views/cards/comments/edit.html.erb similarity index 73% rename from app/views/comments/edit.html.erb rename to app/views/cards/comments/edit.html.erb index 795a79d71..e3a2e783d 100644 --- a/app/views/comments/edit.html.erb +++ b/app/views/cards/comments/edit.html.erb @@ -1,14 +1,14 @@ <%= turbo_frame_tag dom_id(@comment) do %>
- <%= form_with model: [@card.collection, @card, @comment], class: "flex flex-column gap full-width", + <%= form_with model: [ @card, @comment ], class: "flex flex-column gap full-width", data: { controller: "form paste", action: "keydown.ctrl+enter->form#submit:prevent keydown.meta+enter->form#submit:prevent keydown.esc->form#cancel:stop paste->paste#pasteFiles" } do |form| %> <%= form.markdown_area :body, class: "input comment__input", required: true, autofocus: true, placeholder: new_comment_placeholder(@card) %>
<%= form.button class: "btn btn--reversed", type: :submit do %> Save changes <% end %> - <%= link_to collection_card_comment_path(@card.collection, @card, @comment), class: "btn", data: { form_target: "cancel" } do %> + <%= link_to card_comment_path(@card, @comment), class: "btn", data: { form_target: "cancel" } do %> Cancel <% end %> @@ -20,7 +20,7 @@
<% end %> - <%= form_with url: collection_card_comment_path(@card.collection, @card, @comment), method: :delete, id: dom_id(@comment, :delete_form), data: { turbo_frame: "_top" } %> + <%= form_with url: card_comment_path(@card, @comment), method: :delete, id: dom_id(@comment, :delete_form), data: { turbo_frame: "_top" } %>
<% end %> diff --git a/app/views/comments/reactions/_reaction.html.erb b/app/views/cards/comments/reactions/_reaction.html.erb similarity index 92% rename from app/views/comments/reactions/_reaction.html.erb rename to app/views/cards/comments/reactions/_reaction.html.erb index c5790204d..d88ed4fae 100644 --- a/app/views/comments/reactions/_reaction.html.erb +++ b/app/views/cards/comments/reactions/_reaction.html.erb @@ -13,7 +13,7 @@ class: [ "txt-small", { "txt-medium": reaction.all_emoji? } ], data: { action: "click->reaction-delete#reveal keydown.enter->reaction-delete#reveal:prevent", reaction_delete_target: "content" } %> - <%= button_to collection_card_comment_reaction_path(comment.card.collection, comment.card, comment, reaction), + <%= button_to card_comment_reaction_path(comment.card, comment, reaction), method: :delete, class: "btn btn--negative flex-item-justify-end reaction__delete", data: { action: "reaction-delete#perform", reaction_delete_target: "button" } do %> diff --git a/app/views/comments/reactions/_reactions.html.erb b/app/views/cards/comments/reactions/_reactions.html.erb similarity index 70% rename from app/views/comments/reactions/_reactions.html.erb rename to app/views/cards/comments/reactions/_reactions.html.erb index 0106deefe..5c9b9f846 100644 --- a/app/views/comments/reactions/_reactions.html.erb +++ b/app/views/cards/comments/reactions/_reactions.html.erb @@ -2,12 +2,12 @@ <%= turbo_stream_from comment, :comments %>
- <%= render partial: "comments/reactions/reaction", collection: comment.reactions.ordered, locals: { comment: comment }, cached: true %> + <%= render partial: "cards/comments/reactions/reaction", collection: comment.reactions.ordered, locals: { comment: comment }, cached: true %>
<%= turbo_frame_tag comment, :new_reaction do %>
- <%= link_to new_collection_card_comment_reaction_path(comment.card.collection, comment.card, comment), role: "button", + <%= link_to new_card_comment_reaction_path(comment.card, comment), role: "button", class: "btn reaction__action", action: "soft-keyboard#open" do %> <%= icon_tag "reaction" %> Add a reaction diff --git a/app/views/cards/comments/reactions/index.html.erb b/app/views/cards/comments/reactions/index.html.erb new file mode 100644 index 000000000..74863a9f1 --- /dev/null +++ b/app/views/cards/comments/reactions/index.html.erb @@ -0,0 +1 @@ +<%= render "cards/comments/reactions/reactions", comment: @comment %> diff --git a/app/views/comments/reactions/new.html.erb b/app/views/cards/comments/reactions/new.html.erb similarity index 84% rename from app/views/comments/reactions/new.html.erb rename to app/views/cards/comments/reactions/new.html.erb index fc3983949..a7945c112 100644 --- a/app/views/comments/reactions/new.html.erb +++ b/app/views/cards/comments/reactions/new.html.erb @@ -1,5 +1,5 @@ <%= turbo_frame_tag dom_id(@comment, :new_reaction) do %> - <%= form_with url: collection_card_comment_reactions_path(@comment.card.collection, @comment.card, @comment), + <%= form_with url: card_comment_reactions_path(@comment.card, @comment), class: "reaction reaction__form flex-inline postion--relative max-width align-center fill-white gap expanded", html: { aria: { label: "New reaction" } }, data: { controller: "form", turbo_frame: dom_id(@comment, :reacting), action: "keydown.esc->form#cancel" } do |form| %> @@ -17,7 +17,7 @@ Submit <% end %> - <%= link_to collection_card_comment_reactions_path(@comment.card.collection, @comment.card, @comment), role: "button", + <%= link_to card_comment_reactions_path(@comment.card, @comment), role: "button", data: { turbo_frame: dom_id(@comment, :reactions), form_target: "cancel" }, class: "btn btn--negative" do %> <%= icon_tag "minus" %> Cancel diff --git a/app/views/cards/comments/show.html.erb b/app/views/cards/comments/show.html.erb new file mode 100644 index 000000000..a17046280 --- /dev/null +++ b/app/views/cards/comments/show.html.erb @@ -0,0 +1 @@ +<%= render "cards/comments/body", comment: @comment %> diff --git a/app/views/cards/comments/update.html.erb b/app/views/cards/comments/update.html.erb new file mode 100644 index 000000000..a17046280 --- /dev/null +++ b/app/views/cards/comments/update.html.erb @@ -0,0 +1 @@ +<%= render "cards/comments/body", comment: @comment %> diff --git a/app/views/comments/reactions/index.html.erb b/app/views/comments/reactions/index.html.erb deleted file mode 100644 index 7228b8718..000000000 --- a/app/views/comments/reactions/index.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= render "comments/reactions/reactions", comment: @comment %> \ No newline at end of file diff --git a/app/views/comments/show.html.erb b/app/views/comments/show.html.erb deleted file mode 100644 index 815881610..000000000 --- a/app/views/comments/show.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= render "comments/body", comment: @comment %> diff --git a/app/views/comments/update.html.erb b/app/views/comments/update.html.erb deleted file mode 100644 index 815881610..000000000 --- a/app/views/comments/update.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= render "comments/body", comment: @comment %> diff --git a/app/views/messages/_message.html.erb b/app/views/messages/_message.html.erb index 1a3b8e401..54fa127cc 100644 --- a/app/views/messages/_message.html.erb +++ b/app/views/messages/_message.html.erb @@ -1,5 +1,5 @@ <% cache message do %> - <%# Template Dependency: comments/comment %> + <%# Template Dependency: cards/comments/comment %> <%# Template Dependency: event_summaries/event_summary %> <%= render partial: message.messageable.to_partial_path, object: message.messageable %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 70de37a31..a0a3f5ffa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -28,6 +28,10 @@ Rails.application.routes.draw do resources :boosts resources :stagings resources :taggings + + resources :comments do + resources :reactions, module: :comments + end end end @@ -52,11 +56,7 @@ Rails.application.routes.draw do resource :involvement end - resources :cards do - resources :comments do - resources :reactions, module: :comments - end - end + resources :cards end resources :events, only: :index diff --git a/test/controllers/cards/comments/reactions_controller_test.rb b/test/controllers/cards/comments/reactions_controller_test.rb new file mode 100644 index 000000000..83f556992 --- /dev/null +++ b/test/controllers/cards/comments/reactions_controller_test.rb @@ -0,0 +1,26 @@ +require "test_helper" + +class Cards::Comments::ReactionsControllerTest < ActionDispatch::IntegrationTest + setup do + sign_in_as :jz + @comment = comments(:logo_agreement_jz) + end + + test "create" do + assert_turbo_stream_broadcasts [ @comment, :comments ], count: 1 do + assert_difference -> { @comment.reactions.count }, 1 do + post card_comment_reactions_url(@comment.card, @comment, format: :turbo_stream), params: { reaction: { content: "Great work!" } } + assert_redirected_to card_comment_reactions_url(@comment.card, @comment) + end + end + end + + test "destroy" do + assert_turbo_stream_broadcasts [ @comment, :comments ], count: 1 do + assert_difference -> { @comment.reactions.count }, -1 do + delete card_comment_reaction_url(@comment.card, @comment, reactions(:kevin), format: :turbo_stream) + assert_response :success + end + end + end +end diff --git a/test/controllers/cards/comments_controller_test.rb b/test/controllers/cards/comments_controller_test.rb new file mode 100644 index 000000000..a9e45f838 --- /dev/null +++ b/test/controllers/cards/comments_controller_test.rb @@ -0,0 +1,30 @@ +require "test_helper" + +class Cards::CommentsControllerTest < ActionDispatch::IntegrationTest + setup do + sign_in_as :kevin + end + + test "create" do + assert_difference "cards(:logo).messages.comments.count", +1 do + post card_comments_url(cards(:logo), params: { comment: { body: "Agreed." } }) + end + + assert_response :success + end + + test "update" do + put card_comment_url(cards(:logo), comments(:logo_agreement_kevin)), params: { comment: { body: "I've changed my mind" } } + + assert_response :success + assert_equal "I've changed my mind", comments(:logo_agreement_kevin).reload.body.content + end + + test "update another user's comment" do + assert_no_changes "comments(:logo_agreement_jz).body.content" do + put card_comment_url(cards(:logo), comments(:logo_agreement_jz)), params: { comment: { body: "I've changed my mind" } } + end + + assert_response :forbidden + end +end diff --git a/test/controllers/comments_controller_test.rb b/test/controllers/comments_controller_test.rb deleted file mode 100644 index 78d4987ae..000000000 --- a/test/controllers/comments_controller_test.rb +++ /dev/null @@ -1,30 +0,0 @@ -require "test_helper" - -class CommentsControllerTest < ActionDispatch::IntegrationTest - setup do - sign_in_as :kevin - end - - test "create" do - assert_difference "cards(:logo).messages.comments.count", +1 do - post collection_card_comments_url(collections(:writebook), cards(:logo), params: { comment: { body: "Agreed." } }) - end - - assert_response :success - end - - test "update" do - put collection_card_comment_url(collections(:writebook), cards(:logo), comments(:logo_agreement_kevin)), params: { comment: { body: "I've changed my mind" } } - - assert_response :success - assert_equal "I've changed my mind", comments(:logo_agreement_kevin).reload.body.content - end - - test "update another user's comment" do - assert_no_changes "comments(:logo_agreement_jz).body.content" do - put collection_card_comment_url(collections(:writebook), cards(:logo), comments(:logo_agreement_jz)), params: { comment: { body: "I've changed my mind" } } - end - - assert_response :forbidden - end -end diff --git a/test/controllers/reactions_controller_test.rb b/test/controllers/reactions_controller_test.rb deleted file mode 100644 index e46c9ac30..000000000 --- a/test/controllers/reactions_controller_test.rb +++ /dev/null @@ -1,26 +0,0 @@ -require "test_helper" - -class Comments::ReactionsControllerTest < ActionDispatch::IntegrationTest - setup do - sign_in_as :jz - @comment = comments(:logo_agreement_jz) - end - - test "create" do - assert_turbo_stream_broadcasts [ @comment, :comments ], count: 1 do - assert_difference -> { @comment.reactions.count }, 1 do - post collection_card_comment_reactions_url(@comment.card.collection, @comment.card, @comment, format: :turbo_stream), params: { reaction: { content: "Great work!" } } - assert_redirected_to collection_card_comment_reactions_url(@comment.card.collection, @comment.card, @comment) - end - end - end - - test "destroy" do - assert_turbo_stream_broadcasts [ @comment, :comments ], count: 1 do - assert_difference -> { @comment.reactions.count }, -1 do - delete collection_card_comment_reaction_url(@comment.card.collection, @comment.card, @comment, reactions(:kevin), format: :turbo_stream) - assert_response :success - end - end - end -end