From fce622b5b1959892b6f6ea94bfe3a1c4eb52d8dd Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 14 Oct 2023 11:12:05 -0400 Subject: Added comment replying --- app/assets/images/comment_add.png | Bin 0 -> 530 bytes app/assets/images/cross.png | Bin 0 -> 655 bytes app/assets/javascripts/application.js | 1 + app/assets/javascripts/main/comments.coffee | 14 ++++++++++++++ app/assets/stylesheets/main/entries.scss | 20 ++++++++++++++++++++ app/controllers/admin/comments_controller.rb | 4 ++++ app/controllers/comments_controller.rb | 6 +++++- app/mailers/comment_mailer.rb | 5 +++++ app/models/comment.rb | 3 +++ .../comment_mailer/reply_comment_email.html.haml | 14 ++++++++++++++ .../comment_mailer/reply_comment_email.text.erb | 19 +++++++++++++++++++ app/views/comments/_comment.html.haml | 1 + app/views/comments/_form.html.haml | 6 ++++++ db/migrate/20231014141657_add_reply_to_comment.rb | 5 +++++ db/schema.rb | 5 ++++- 15 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 app/assets/images/comment_add.png create mode 100644 app/assets/images/cross.png create mode 100644 app/assets/javascripts/main/comments.coffee create mode 100644 app/views/comment_mailer/reply_comment_email.html.haml create mode 100644 app/views/comment_mailer/reply_comment_email.text.erb create mode 100644 db/migrate/20231014141657_add_reply_to_comment.rb diff --git a/app/assets/images/comment_add.png b/app/assets/images/comment_add.png new file mode 100644 index 0000000..75e78de Binary files /dev/null and b/app/assets/images/comment_add.png differ diff --git a/app/assets/images/cross.png b/app/assets/images/cross.png new file mode 100644 index 0000000..1514d51 Binary files /dev/null and b/app/assets/images/cross.png differ diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index cd58a9c..d8f72b4 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -17,3 +17,4 @@ //= require jquery-ui //= require js-routes //= require_tree ./admin +//= require_tree ./main diff --git a/app/assets/javascripts/main/comments.coffee b/app/assets/javascripts/main/comments.coffee new file mode 100644 index 0000000..d33e200 --- /dev/null +++ b/app/assets/javascripts/main/comments.coffee @@ -0,0 +1,14 @@ +comments_ready = -> + $(".comment-reply-to").click -> + $("#comment_reply_to_id").val($(this).data("commentId")) + $("#comment-reply-msg .comment-reply-author").text($(this).data("commentAuthor")) + $("#comment-reply-msg").show() + return false + + $(".comment-reply-cancel").click -> + $("#comment_reply_to_id").val("") + $("#comment-reply-msg").hide() + return false + +$(document).ready(comments_ready) +$(document).on('turbolinks:load', comments_ready) diff --git a/app/assets/stylesheets/main/entries.scss b/app/assets/stylesheets/main/entries.scss index 8d5796f..dd6b248 100644 --- a/app/assets/stylesheets/main/entries.scss +++ b/app/assets/stylesheets/main/entries.scss @@ -251,9 +251,29 @@ float: right; margin-top: 1em; } + + .comment-reply-to { + float: right; + margin-right: 0.5em; + + img { + display: block; + } + } } #comment-form { + #comment-reply-msg { + display: none; + margin-top: 1em; + margin-bottom: 0; + margin-left: 0; + + .comment-reply-author { + font-weight: bold; + } + } + fieldset { border: 0; diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index ccde3e6..9958232 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -14,6 +14,10 @@ class Admin::CommentsController < Admin::AdminController @comment.status = :published @comment.save! + if @comment.reply_to and @comment.reply_to.email != @comment.blog.user.email and @comment.reply_to.email != current_user.email + CommentMailer.with(comment: @comment).reply_comment_email.deliver_later + end + flash.notice = "Comment successfully published." redirect_to pending_admin_comments_url end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index a444dba..2f3117f 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -46,6 +46,10 @@ class CommentsController < ApplicationController if @comment.status == :published CommentMailer.with(comment: @comment).new_comment_email.deliver_later + + if @comment.reply_to and @comment.reply_to.email != @comment.blog.user.email + CommentMailer.with(comment: @comment).reply_comment_email.deliver_later + end else CommentMailer.with(comment: @comment).new_pending_comment_email.deliver_later end @@ -60,6 +64,6 @@ class CommentsController < ApplicationController private def comment_params - params.require(:comment).permit(:username, :email, :website, :body) + params.require(:comment).permit(:username, :email, :website, :body, :reply_to_id) end end diff --git a/app/mailers/comment_mailer.rb b/app/mailers/comment_mailer.rb index aeed1b0..41feede 100644 --- a/app/mailers/comment_mailer.rb +++ b/app/mailers/comment_mailer.rb @@ -8,4 +8,9 @@ class CommentMailer < ApplicationMailer @comment = params[:comment] mail(to: @comment.blog.user.email, subject: "[Four Island] Pending comment on #{@comment.blog.title}") end + + def reply_comment_email + @comment = params[:comment] + mail(to: @comment.reply_to.email, subject: "[Four Island] Reply to your comment on #{@comment.blog.title}") + end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 9697100..b85f3b6 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -3,6 +3,9 @@ class Comment < ApplicationRecord belongs_to :blog + has_many :replies, class_name: "Comment", foreign_key: "reply_to_id" + belongs_to :reply_to, class_name: "Comment", optional: true + validates :body, presence: true validates :username, presence: true validates :email, presence: true, format: URI::MailTo::EMAIL_REGEXP diff --git a/app/views/comment_mailer/reply_comment_email.html.haml b/app/views/comment_mailer/reply_comment_email.html.haml new file mode 100644 index 0000000..22009ee --- /dev/null +++ b/app/views/comment_mailer/reply_comment_email.html.haml @@ -0,0 +1,14 @@ +%p + A reply has been posted to your comment on + = succeed "." do + = link_to @comment.blog.title, @comment.blog + The original comment: +%blockquote= @comment.reply_to.body +%p + = @comment.username + left the following response: +%blockquote= @comment.body +%p + Posted: + = @comment.published_at.strftime("%B #{@comment.published_at.day.ordinalize}, %Y at %-I:%M:%S%P") +%p= link_to "See the comment on the web", blog_url(@comment.blog, anchor: "comment-#{@comment.id}") diff --git a/app/views/comment_mailer/reply_comment_email.text.erb b/app/views/comment_mailer/reply_comment_email.text.erb new file mode 100644 index 0000000..bd60692 --- /dev/null +++ b/app/views/comment_mailer/reply_comment_email.text.erb @@ -0,0 +1,19 @@ +A reply has been posted to your comment on <%= @comment.blog.title %>. The original comment: + +--- + +<%= @comment.reply_to.body %> + +--- + +<%= @comment.username %> posted the following response: + +--- + +<%= @comment.body %> + +--- + +Posted: <%= @comment.published_at.strftime("%B #{@comment.published_at.day.ordinalize}, %Y at %-I:%M:%S%P") %> + +See the comment on the web: <%= blog_url(@comment.blog, anchor: "comment-#{@comment.id}") %> diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml index 3ad21e9..225b5d9 100644 --- a/app/views/comments/_comment.html.haml +++ b/app/views/comments/_comment.html.haml @@ -11,3 +11,4 @@ = link_to comment.username, comment.website on = comment.published_at.strftime("%B #{comment.published_at.day.ordinalize}, %Y at %-I:%M:%S%P") + = link_to (image_tag "comment_add.png"), "#", class: "comment-reply-to", title: "Reply to comment", data: { comment_id: comment.id, comment_author: comment.username } diff --git a/app/views/comments/_form.html.haml b/app/views/comments/_form.html.haml index f6df807..eb374c7 100644 --- a/app/views/comments/_form.html.haml +++ b/app/views/comments/_form.html.haml @@ -1,6 +1,12 @@ = form_for @comment || blog.comments.new, html: { id: "comment-form" } do |f| + = f.hidden_field :reply_to_id %fieldset#comment-body-field %blockquote.bubble.rounded.bottom + #comment-reply-msg + = link_to (image_tag "cross"), "#", class: "comment-reply-cancel" + Replying to comment by + = succeed ":" do + %span.comment-reply-author = f.label :body = f.text_area :body %cite.bubble Feel free to post a comment! You may use Markdown. diff --git a/db/migrate/20231014141657_add_reply_to_comment.rb b/db/migrate/20231014141657_add_reply_to_comment.rb new file mode 100644 index 0000000..8e7b445 --- /dev/null +++ b/db/migrate/20231014141657_add_reply_to_comment.rb @@ -0,0 +1,5 @@ +class AddReplyToComment < ActiveRecord::Migration[7.0] + def change + add_reference :comments, :reply_to, foreign_key: { to_table: :comments } + end +end diff --git a/db/schema.rb b/db/schema.rb index 56fd9bb..49c7925 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_10_14_140431) do +ActiveRecord::Schema[7.0].define(version: 2023_10_14_141657) do create_table "audits", force: :cascade do |t| t.integer "auditable_id" t.string "auditable_type" @@ -68,7 +68,9 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_14_140431) do t.datetime "published_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "reply_to_id" t.index ["blog_id"], name: "index_comments_on_blog_id" + t.index ["reply_to_id"], name: "index_comments_on_reply_to_id" end create_table "games", force: :cascade do |t| @@ -362,6 +364,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_14_140431) do add_foreign_key "blogs", "users" add_foreign_key "comments", "blogs" + add_foreign_key "comments", "comments", column: "reply_to_id" add_foreign_key "pokeviewer_items", "pokeviewer_moves", column: "move_id" add_foreign_key "pokeviewer_pokedex_entries", "pokeviewer_species", column: "species_id" add_foreign_key "pokeviewer_pokedex_entries", "pokeviewer_trainers", column: "trainer_id" -- cgit 1.4.1