Commit 2e601cb575ca97f1a1097f12d0edfae241a70263
1 parent
4c86d517
Exists in
master
and in
1 other branch
Deliver notification emails when notices threshold is met
Showing
6 changed files
with
54 additions
and
1 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,15 @@ |
| 1 | +class Mailer < ActionMailer::Base | |
| 2 | + default :from => App.email_from | |
| 3 | + default_url_options[:host] = App.host | |
| 4 | + | |
| 5 | + def error_notification(notice) | |
| 6 | + @notice = notice | |
| 7 | + @project = notice.err.project | |
| 8 | + | |
| 9 | + mail({ | |
| 10 | + :to => @project.watchers.map(&:email), | |
| 11 | + :subject => "[#{@project.name}] #{@notice.err.message}" | |
| 12 | + }) | |
| 13 | + end | |
| 14 | + | |
| 15 | +end | |
| 0 | 16 | \ No newline at end of file | ... | ... |
app/models/notice.rb
| ... | ... | @@ -11,6 +11,8 @@ class Notice |
| 11 | 11 | |
| 12 | 12 | embedded_in :err, :inverse_of => :notices |
| 13 | 13 | |
| 14 | + after_create :deliver_notification, :if => :should_notify? | |
| 15 | + | |
| 14 | 16 | validates_presence_of :backtrace, :server_environment, :notifier |
| 15 | 17 | |
| 16 | 18 | def self.from_xml(hoptoad_xml) |
| ... | ... | @@ -34,4 +36,14 @@ class Notice |
| 34 | 36 | }) |
| 35 | 37 | end |
| 36 | 38 | |
| 39 | + def deliver_notification | |
| 40 | + Mailer.error_notification(self).deliver | |
| 41 | + end | |
| 42 | + | |
| 43 | + protected | |
| 44 | + | |
| 45 | + def should_notify? | |
| 46 | + App.email_at_notices.include?(err.notices.count) && err.project.watchers.any? | |
| 47 | + end | |
| 48 | + | |
| 37 | 49 | end |
| 38 | 50 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,8 @@ |
| 1 | +An error has just occurred in <%= @notice.err.environment %>: <%= @notice.err.message %> | |
| 2 | + | |
| 3 | +This error has occurred <%= pluralize @notice.err.notices.count, 'time' %>. You should really look into it here: | |
| 4 | + | |
| 5 | + <%= error_notice_url(@notice.err, @notice) %> | |
| 6 | + | |
| 7 | +Your loyal servant, | |
| 8 | +Hypnotoad | |
| 0 | 9 | \ No newline at end of file | ... | ... |
config/config.yml
config/routes.rb
spec/models/notice_spec.rb
| ... | ... | @@ -79,4 +79,16 @@ describe Notice do |
| 79 | 79 | end |
| 80 | 80 | end |
| 81 | 81 | |
| 82 | + describe "email notifications" do | |
| 83 | + App.email_at_notices.each do |threshold| | |
| 84 | + it "sends an email notification after #{threshold} notice(s)" do | |
| 85 | + error = Factory(:err) | |
| 86 | + error.notices.stub(:count).and_return(threshold) | |
| 87 | + Mailer.should_receive(:error_notification). | |
| 88 | + and_return(mock('email', :deliver => true)) | |
| 89 | + Factory(:notice, :err => error) | |
| 90 | + end | |
| 91 | + end | |
| 92 | + end | |
| 93 | + | |
| 82 | 94 | end |
| 83 | 95 | \ No newline at end of file | ... | ... |