From d005c47ec4d42e60e02f64c1187d920a991b448c Mon Sep 17 00:00:00 2001 From: Matt Gauger Date: Wed, 7 Mar 2012 16:10:20 -0600 Subject: [PATCH] Add Notice Observer --- app/models/notice.rb | 9 --------- app/models/notice_observer.rb | 19 +++++++++++++++++++ config/application.rb | 2 +- spec/models/notice_observer_spec.rb | 3 +++ spec/models/notice_spec.rb | 44 -------------------------------------------- 5 files changed, 23 insertions(+), 54 deletions(-) create mode 100644 app/models/notice_observer.rb create mode 100644 spec/models/notice_observer_spec.rb diff --git a/app/models/notice.rb b/app/models/notice.rb index 0d284c3..e425a80 100644 --- a/app/models/notice.rb +++ b/app/models/notice.rb @@ -23,7 +23,6 @@ class Notice ) after_create :increase_counter_cache, :cache_attributes_on_problem, :unresolve_problem - after_create :deliver_notification, :if => :should_notify? before_save :sanitize before_destroy :decrease_counter_cache, :remove_cached_attributes_from_problem @@ -92,10 +91,6 @@ class Notice request['session'] || {} end - def deliver_notification - Mailer.err_notification(self).deliver - end - # Backtrace containing only files from the app itself (ignore gems) def app_backtrace backtrace.select { |l| l && l['file'] && l['file'].include?("[PROJECT_ROOT]") } @@ -103,10 +98,6 @@ class Notice protected - def should_notify? - app.notify_on_errs? && (Errbit::Config.per_app_email_at_notices && app.email_at_notices || Errbit::Config.email_at_notices).include?(problem.notices_count) && app.notification_recipients.any? - end - def increase_counter_cache problem.inc(:notices_count, 1) end diff --git a/app/models/notice_observer.rb b/app/models/notice_observer.rb new file mode 100644 index 0000000..c0169c0 --- /dev/null +++ b/app/models/notice_observer.rb @@ -0,0 +1,19 @@ +class NoticeObserver < Mongoid::Observer + observe :notice + + def after_create notice + return unless should_notify? notice + + Mailer.err_notification(notice).deliver + end + + private + + def should_notify? notice + app = notice.app + app.notify_on_errs? && + (Errbit::Config.per_app_email_at_notices && app.email_at_notices || Errbit::Config.email_at_notices).include?(notice.problem.notices_count) && + app.notification_recipients.any? + end + +end diff --git a/config/application.rb b/config/application.rb index 95c04e3..24f5d43 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 + config.mongoid.observers = :deploy_observer, :notice_observer # Configure the default encoding used in templates for Ruby 1.9. config.encoding = "utf-8" diff --git a/spec/models/notice_observer_spec.rb b/spec/models/notice_observer_spec.rb new file mode 100644 index 0000000..efdc9e3 --- /dev/null +++ b/spec/models/notice_observer_spec.rb @@ -0,0 +1,3 @@ +require 'spec_helper' + + diff --git a/spec/models/notice_spec.rb b/spec/models/notice_spec.rb index fa38a1e..64531b0 100644 --- a/spec/models/notice_spec.rb +++ b/spec/models/notice_spec.rb @@ -112,48 +112,4 @@ describe Notice do end - - describe "email notifications (configured individually for each app)" do - custom_thresholds = [2, 4, 8, 16, 32, 64] - - before do - Errbit::Config.per_app_email_at_notices = true - @app = Fabricate(:app_with_watcher, :email_at_notices => custom_thresholds) - @err = Fabricate(:err, :problem => Fabricate(:problem, :app => @app)) - end - - after do - Errbit::Config.per_app_email_at_notices = false - end - - custom_thresholds.each do |threshold| - it "sends an email notification after #{threshold} notice(s)" do - @err.problem.stub(:notices_count).and_return(threshold) - Mailer.should_receive(:err_notification). - and_return(mock('email', :deliver => true)) - Fabricate(:notice, :err => @err) - end - end - end - - describe "email notifications for a resolved issue" do - - before do - Errbit::Config.per_app_email_at_notices = true - @app = Fabricate(:app_with_watcher, :email_at_notices => [1]) - @err = Fabricate(:err, :problem => Fabricate(:problem, :app => @app, :notices_count => 100)) - end - - after do - Errbit::Config.per_app_email_at_notices = false - end - - it "should send email notification after 1 notice since an error has been resolved" do - @err.problem.resolve! - Mailer.should_receive(:err_notification). - and_return(mock('email', :deliver => true)) - Fabricate(:notice, :err => @err) - end - end end - -- libgit2 0.21.2