From 4b5d3a0d63783c2067a1ccb8d2c515fc9278214f Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Wed, 6 Mar 2013 21:57:05 +1300 Subject: [PATCH] Added email notifications for comments --- app/mailers/mailer.rb | 12 +++++++++++- app/models/comment.rb | 1 + app/models/comment_observer.rb | 8 ++++++++ app/views/mailer/comment_notification.html.haml | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ app/views/mailer/comment_notification.text.erb | 36 ++++++++++++++++++++++++++++++++++++ config/application.rb | 2 +- 6 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 app/models/comment_observer.rb create mode 100644 app/views/mailer/comment_notification.html.haml create mode 100644 app/views/mailer/comment_notification.text.erb diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index 040c416..5390cce 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -23,5 +23,15 @@ class Mailer < ActionMailer::Base mail :to => @app.notification_recipients, :subject => "[#{@app.name}] Deployed to #{@deploy.environment} by #{@deploy.username}" end -end + def comment_notification(comment) + @comment = comment + @user = comment.user + @problem = comment.err + @notice = @problem.notices.first + @app = @problem.app + + mail :to => @app.notification_recipients, + :subject => "#{@user.name} commented on [#{@app.name}][#{@notice.environment_name}] #{@notice.message.truncate(50)}" + end +end diff --git a/app/models/comment.rb b/app/models/comment.rb index e9dd5a6..09d4af9 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -10,6 +10,7 @@ class Comment belongs_to :err, :class_name => "Problem" belongs_to :user + delegate :app, :to => :err validates_presence_of :body diff --git a/app/models/comment_observer.rb b/app/models/comment_observer.rb new file mode 100644 index 0000000..4e59ba7 --- /dev/null +++ b/app/models/comment_observer.rb @@ -0,0 +1,8 @@ +class CommentObserver < Mongoid::Observer + observe :comment + + def after_create(comment) + Mailer.comment_notification(comment).deliver if comment.app.notifiable? + end + +end diff --git a/app/views/mailer/comment_notification.html.haml b/app/views/mailer/comment_notification.html.haml new file mode 100644 index 0000000..f034749 --- /dev/null +++ b/app/views/mailer/comment_notification.html.haml @@ -0,0 +1,50 @@ +%tr + %td.section + %table(cellpadding="0" cellspacing="0" border="0" align="left") + %tbody + %tr + %td.content(valign="top") + %div + %p + = @user.name + has just commented on an error that occurred in + = link_to(@app.name, app_url(@app), :class => "bold") << "," + on the + %span.bold= @problem.environment + environment. + %br + This err has occurred #{pluralize @problem.notices_count, 'time'}. + %p + = link_to("Click here to view the error and add a comment on Errbit", app_problem_url(@app, @problem), :class => "bold") << "." + +%tr + %td.section + %table(cellpadding="0" cellspacing="0" border="0" align="left") + %tbody + %tr + %td.content(valign="top") + %div + %p.heading COMMENT: + %br + %p= @comment.body + +%tr + %td.section + %table(cellpadding="0" cellspacing="0" border="0" align="left") + %tbody + %tr + %td.content(valign="top") + %div + %p.heading ERROR MESSAGE: + %p= @problem.message + %p.heading WHERE: + %p.monospace + = @problem.where + %p.heading URL: + %p.monospace + - if @notice.request['url'].present? + = link_to @notice.request['url'], @notice.request['url'] + %p.heading BROWSER: + %p.monospace + = user_agent_graph(@problem) + %br diff --git a/app/views/mailer/comment_notification.text.erb b/app/views/mailer/comment_notification.text.erb new file mode 100644 index 0000000..01629f9 --- /dev/null +++ b/app/views/mailer/comment_notification.text.erb @@ -0,0 +1,36 @@ +<%= @user.name %> has just commented on an error that occurred in <%= @notice.environment_name %>: <%= raw(@notice.message) %> + +This err has occurred <%= pluralize @notice.problem.notices_count, 'time' %>. You should really look into it here: + + <%= app_problem_url(@app, @notice.problem) %> + + +COMMENT: + +<%= @comment.body %> + + +----------------------------------------------- + +ERROR MESSAGE: + +<%= raw(@notice.message) %> + + +WHERE: + +<%= @notice.where %> + +<% @notice.in_app_backtrace_lines.each do |line| %> + <%= line %> +<% end %> + + +URL: + +<%= @notice.request['url'] %> + + +BROWSER: + +<%= @notice.user_agent_string %> diff --git a/config/application.rb b/config/application.rb index 9e6ebdd..e17218a 100644 --- a/config/application.rb +++ b/config/application.rb @@ -52,7 +52,7 @@ module Errbit config.mongoid.preload_models = true # Set up observers - config.mongoid.observers = :deploy_observer, :notice_observer + config.mongoid.observers = :deploy_observer, :notice_observer, :comment_observer # Configure the default encoding used in templates for Ruby 1.9. config.encoding = "utf-8" -- libgit2 0.21.2