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 ++++++ 13 files changed, 92 insertions(+), 1 deletion(-) 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 (limited to 'app') 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. -- cgit 1.4.1