From 9ad3bca2ea475b709c8311ab25194e4578c1a0d9 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 12 Oct 2023 21:51:02 -0400 Subject: Webmaster gets an email when a comment is posted --- app/controllers/comments_controller.rb | 4 ++++ app/mailers/application_mailer.rb | 2 +- app/mailers/comment_mailer.rb | 7 +++++++ .../comment_mailer/new_comment_email.html.haml | 22 ++++++++++++++++++++++ .../comment_mailer/new_comment_email.text.erb | 20 ++++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 app/mailers/comment_mailer.rb create mode 100644 app/views/comment_mailer/new_comment_email.html.haml create mode 100644 app/views/comment_mailer/new_comment_email.text.erb (limited to 'app') diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 60c8f6a..c66365b 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -43,6 +43,10 @@ class CommentsController < ApplicationController if @comment.save flash.notice = flash_message + + if @comment.status == :published + CommentMailer.with(comment: @comment).new_comment_email.deliver_later + end else flash.alert = "Error posting comment." end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 286b223..4862159 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,4 +1,4 @@ class ApplicationMailer < ActionMailer::Base - default from: 'from@example.com' + default from: 'no-reply@fourisland.com' layout 'mailer' end diff --git a/app/mailers/comment_mailer.rb b/app/mailers/comment_mailer.rb new file mode 100644 index 0000000..363875a --- /dev/null +++ b/app/mailers/comment_mailer.rb @@ -0,0 +1,7 @@ +class CommentMailer < ApplicationMailer + def new_comment_email + @comment = params[:comment] + @admin = User.first # this is weird + mail(to: @admin.email, subject: "[Four Island] Comment on #{@comment.blog.title}") + end +end diff --git a/app/views/comment_mailer/new_comment_email.html.haml b/app/views/comment_mailer/new_comment_email.html.haml new file mode 100644 index 0000000..bd9e91c --- /dev/null +++ b/app/views/comment_mailer/new_comment_email.html.haml @@ -0,0 +1,22 @@ +%h1 Four Island +%p + A comment has been posted on + = link_to @comment.blog.title, @comment.blog + by the following user: +%ul + %li + Name: + = @comment.username + %li + Email: + = @comment.email + - unless @comment.website.empty? + %li + Website: + = @comment.website +%p Here is the comment: +%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/new_comment_email.text.erb b/app/views/comment_mailer/new_comment_email.text.erb new file mode 100644 index 0000000..e37046f --- /dev/null +++ b/app/views/comment_mailer/new_comment_email.text.erb @@ -0,0 +1,20 @@ +Four Island +=========== + +A comment has been posted on <%= @comment.blog.title %> by the following user: + +* Name: <%= @comment.username %> +* Email: <%= @comment.email %> +<% unless @comment.website.empty? %>* Website: <%= @comment.website %> +<% end %> +Here is the comment: + +--- + +<%= @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}") %> -- cgit 1.4.1