From 262047fe6da39dcc007afd83c2e9d9dedb939c37 Mon Sep 17 00:00:00 2001 From: Junichi Ito Date: Sat, 3 Aug 2024 19:30:17 +0900 Subject: [PATCH 1/9] =?UTF-8?q?turbo=20stream=E3=81=A7=E3=82=B3=E3=83=A1?= =?UTF-8?q?=E3=83=B3=E3=83=88=E3=82=92=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/books/comments_controller.rb | 3 +- .../reports/comments_controller.rb | 3 +- app/views/shared/_comment_contents.html.erb | 17 +++++++++++ app/views/shared/_comment_form.html.erb | 5 ++++ app/views/shared/_comments.html.erb | 28 ++++--------------- .../shared/comment_create.turbo_stream.erb | 2 ++ config/locales/ja.yml | 4 ++- 7 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 app/views/shared/_comment_contents.html.erb create mode 100644 app/views/shared/_comment_form.html.erb create mode 100644 app/views/shared/comment_create.turbo_stream.erb diff --git a/app/controllers/books/comments_controller.rb b/app/controllers/books/comments_controller.rb index 579eb8591..ea1d41dd9 100644 --- a/app/controllers/books/comments_controller.rb +++ b/app/controllers/books/comments_controller.rb @@ -7,7 +7,8 @@ def create comment.user = current_user # NOTE: 実装を複雑にしないよう、あえて保存失敗時の処理を考慮しないことにした(失敗時はシステムエラーとする) comment.save! - redirect_to book, notice: t('controllers.common.notice_create', name: Comment.model_name.human) + @commentable = book + render 'shared/comment_create' end private diff --git a/app/controllers/reports/comments_controller.rb b/app/controllers/reports/comments_controller.rb index 992eed28e..a35a7045c 100644 --- a/app/controllers/reports/comments_controller.rb +++ b/app/controllers/reports/comments_controller.rb @@ -7,7 +7,8 @@ def create comment.user = current_user # NOTE: 実装を複雑にしないよう、あえて保存失敗時の処理を考慮しないことにした(失敗時はシステムエラーとする) comment.save! - redirect_to report, notice: t('controllers.common.notice_create', name: Comment.model_name.human) + @commentable = report + render 'shared/comment_create' end private diff --git a/app/views/shared/_comment_contents.html.erb b/app/views/shared/_comment_contents.html.erb new file mode 100644 index 000000000..81215b67e --- /dev/null +++ b/app/views/shared/_comment_contents.html.erb @@ -0,0 +1,17 @@ +<% if (comments = commentable.comments.order(:id).includes(:user).presence) %> + +<% else %> + (<%= t('.no_comments') %>) +<% end %> diff --git a/app/views/shared/_comment_form.html.erb b/app/views/shared/_comment_form.html.erb new file mode 100644 index 000000000..9bdf72bc6 --- /dev/null +++ b/app/views/shared/_comment_form.html.erb @@ -0,0 +1,5 @@ +<%= form_with model: [commentable, commentable.comments.new], local: true do |f| %> + <%# NOTE: ブラウザ側で必須チェックを行い、サーバーサイドの検証エラーを起きにくくする %> + <%= f.text_field :content, required: true %> + <%= f.submit t('.create') %> +<% end %> diff --git a/app/views/shared/_comments.html.erb b/app/views/shared/_comments.html.erb index 3ca28143a..07b4b4e2a 100644 --- a/app/views/shared/_comments.html.erb +++ b/app/views/shared/_comments.html.erb @@ -1,25 +1,9 @@
<%= Comment.model_name.human %>: - <% if (comments = commentable.comments.order(:id).includes(:user).presence) %> - - <% else %> - (<%= t('.no_comments') %>) - <% end %> - <%= form_with model: [commentable, commentable.comments.new], local: true do |f| %> - <%# NOTE: ブラウザ側で必須チェックを行い、サーバーサイドの検証エラーを起きにくくする %> - <%= f.text_field :content, required: true %> - <%= f.submit t('.create') %> - <% end %> +
+ <%= render 'shared/comment_contents', commentable: commentable %> +
+
+ <%= render 'shared/comment_form', commentable: commentable %> +
diff --git a/app/views/shared/comment_create.turbo_stream.erb b/app/views/shared/comment_create.turbo_stream.erb new file mode 100644 index 000000000..6538ace45 --- /dev/null +++ b/app/views/shared/comment_create.turbo_stream.erb @@ -0,0 +1,2 @@ +<%= turbo_stream.update "comments", partial: 'shared/comment_contents', locals: { commentable: @commentable } %> +<%= turbo_stream.update "comment_form", partial: 'shared/comment_form', locals: { commentable: @commentable } %> diff --git a/config/locales/ja.yml b/config/locales/ja.yml index 3be324acf..7d54a2920 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -213,9 +213,11 @@ ja: sign_in_as: "%{user} としてログイン中" shared: comments: - create: コメントする delete: 削除 + comment_contents: no_comments: コメントはまだありません + comment_form: + create: コメントする reports: mentions: title: この日報に言及している日報 From 1e5fae0fc345c23c750dcf00fb9444afb08655f5 Mon Sep 17 00:00:00 2001 From: Junichi Ito Date: Sat, 3 Aug 2024 19:40:49 +0900 Subject: [PATCH 2/9] =?UTF-8?q?turbo=20stream=E3=81=A7=E3=82=B3=E3=83=A1?= =?UTF-8?q?=E3=83=B3=E3=83=88=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/comments_controller.rb | 7 +++---- app/views/comments/destroy.turbo_stream.erb | 5 +++++ app/views/shared/_comment_contents.html.erb | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 app/views/comments/destroy.turbo_stream.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index dad2f4b14..d0c09b511 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -2,9 +2,8 @@ class CommentsController < ApplicationController def destroy - comment = current_user.comments.find(params[:id]) - commentable = comment.commentable - comment.destroy - redirect_to commentable, notice: t('controllers.common.notice_destroy', name: Comment.model_name.human) + @comment = current_user.comments.find(params[:id]) + @commentable = @comment.commentable + @comment.destroy end end diff --git a/app/views/comments/destroy.turbo_stream.erb b/app/views/comments/destroy.turbo_stream.erb new file mode 100644 index 000000000..63e8e461c --- /dev/null +++ b/app/views/comments/destroy.turbo_stream.erb @@ -0,0 +1,5 @@ +<% if @commentable.comments.exists? %> + <%= turbo_stream.remove @comment %> +<% else %> + <%= turbo_stream.update "comments", partial: 'shared/comment_contents', locals: { commentable: @commentable } %> +<% end %> diff --git a/app/views/shared/_comment_contents.html.erb b/app/views/shared/_comment_contents.html.erb index 81215b67e..465ff7316 100644 --- a/app/views/shared/_comment_contents.html.erb +++ b/app/views/shared/_comment_contents.html.erb @@ -1,7 +1,7 @@ <% if (comments = commentable.comments.order(:id).includes(:user).presence) %>