From 95bfe3f7f9720d1597860bfbd0e11d4ae22f0049 Mon Sep 17 00:00:00 2001 From: Cyril Mougel Date: Wed, 21 Aug 2013 11:47:58 +0200 Subject: [PATCH] Extract NoticeObserver to Notice model --- app/models/notice.rb | 19 +++++++++++++++++++ app/models/notice_observer.rb | 13 ------------- config/application.rb | 2 +- spec/models/notice_observer_spec.rb | 2 +- 4 files changed, 21 insertions(+), 15 deletions(-) delete mode 100644 app/models/notice_observer.rb diff --git a/app/models/notice.rb b/app/models/notice.rb index cd76d01..64db1f6 100644 --- a/app/models/notice.rb +++ b/app/models/notice.rb @@ -17,6 +17,7 @@ class Notice belongs_to :err belongs_to :backtrace, :index => true index :created_at + index( [ [ :err_id, Mongo::ASCENDING ], @@ -26,6 +27,8 @@ class Notice ) after_create :cache_attributes_on_problem, :unresolve_problem + after_create :email_notification + after_create :services_notification before_save :sanitize before_destroy :decrease_counter_cache, :remove_cached_attributes_from_problem @@ -160,5 +163,21 @@ class Notice end end + private + + ## + # Send email notification if needed + def email_notification + return true unless should_email? + Mailer.err_notification(self).deliver + end + + ## + # Launch all notification define on the app associate to this notice + def services_notification + return true unless app.notification_service_configured? and should_notify? + app.notification_service.create_notification(problem) + end + end diff --git a/app/models/notice_observer.rb b/app/models/notice_observer.rb deleted file mode 100644 index 80d57fe..0000000 --- a/app/models/notice_observer.rb +++ /dev/null @@ -1,13 +0,0 @@ -class NoticeObserver < Mongoid::Observer - observe :notice - - def after_create notice - # if the app has a notification service, fire it off - if notice.app.notification_service_configured? and notice.should_notify? - notice.app.notification_service.create_notification(notice.problem) - end - - Mailer.err_notification(notice).deliver if notice.should_email? - end - -end diff --git a/config/application.rb b/config/application.rb index 2cef85d..bd4b482 100644 --- a/config/application.rb +++ b/config/application.rb @@ -55,7 +55,7 @@ module Errbit config.mongoid.preload_models = true # Set up observers - config.mongoid.observers = :deploy_observer, :notice_observer, :comment_observer + config.mongoid.observers = :deploy_observer, :comment_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 index 13fb5e0..3a99793 100644 --- a/spec/models/notice_observer_spec.rb +++ b/spec/models/notice_observer_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe NoticeObserver do +describe "Callback on Notice" do describe "email notifications (configured individually for each app)" do custom_thresholds = [2, 4, 8, 16, 32, 64] -- libgit2 0.21.2