Commit 95bfe3f7f9720d1597860bfbd0e11d4ae22f0049

Authored by Cyril Mougel
1 parent 94f860ac
Exists in master and in 1 other branch production

Extract NoticeObserver to Notice model

I think the Observer Pattern in Model is too ackward and need to be
avoid. This kind of stuff is delete on Rails 4 for example.
app/models/notice.rb
@@ -17,6 +17,7 @@ class Notice @@ -17,6 +17,7 @@ class Notice
17 belongs_to :err 17 belongs_to :err
18 belongs_to :backtrace, :index => true 18 belongs_to :backtrace, :index => true
19 index :created_at 19 index :created_at
  20 +
20 index( 21 index(
21 [ 22 [
22 [ :err_id, Mongo::ASCENDING ], 23 [ :err_id, Mongo::ASCENDING ],
@@ -26,6 +27,8 @@ class Notice @@ -26,6 +27,8 @@ class Notice
26 ) 27 )
27 28
28 after_create :cache_attributes_on_problem, :unresolve_problem 29 after_create :cache_attributes_on_problem, :unresolve_problem
  30 + after_create :email_notification
  31 + after_create :services_notification
29 before_save :sanitize 32 before_save :sanitize
30 before_destroy :decrease_counter_cache, :remove_cached_attributes_from_problem 33 before_destroy :decrease_counter_cache, :remove_cached_attributes_from_problem
31 34
@@ -160,5 +163,21 @@ class Notice @@ -160,5 +163,21 @@ class Notice
160 end 163 end
161 end 164 end
162 165
  166 + private
  167 +
  168 + ##
  169 + # Send email notification if needed
  170 + def email_notification
  171 + return true unless should_email?
  172 + Mailer.err_notification(self).deliver
  173 + end
  174 +
  175 + ##
  176 + # Launch all notification define on the app associate to this notice
  177 + def services_notification
  178 + return true unless app.notification_service_configured? and should_notify?
  179 + app.notification_service.create_notification(problem)
  180 + end
  181 +
163 end 182 end
164 183
app/models/notice_observer.rb
@@ -1,13 +0,0 @@ @@ -1,13 +0,0 @@
1 -class NoticeObserver < Mongoid::Observer  
2 - observe :notice  
3 -  
4 - def after_create notice  
5 - # if the app has a notification service, fire it off  
6 - if notice.app.notification_service_configured? and notice.should_notify?  
7 - notice.app.notification_service.create_notification(notice.problem)  
8 - end  
9 -  
10 - Mailer.err_notification(notice).deliver if notice.should_email?  
11 - end  
12 -  
13 -end  
config/application.rb
@@ -55,7 +55,7 @@ module Errbit @@ -55,7 +55,7 @@ module Errbit
55 config.mongoid.preload_models = true 55 config.mongoid.preload_models = true
56 56
57 # Set up observers 57 # Set up observers
58 - config.mongoid.observers = :deploy_observer, :notice_observer, :comment_observer 58 + config.mongoid.observers = :deploy_observer, :comment_observer
59 59
60 # Configure the default encoding used in templates for Ruby 1.9. 60 # Configure the default encoding used in templates for Ruby 1.9.
61 config.encoding = "utf-8" 61 config.encoding = "utf-8"
spec/models/notice_observer_spec.rb
1 require 'spec_helper' 1 require 'spec_helper'
2 2
3 -describe NoticeObserver do 3 +describe "Callback on Notice" do
4 describe "email notifications (configured individually for each app)" do 4 describe "email notifications (configured individually for each app)" do
5 custom_thresholds = [2, 4, 8, 16, 32, 64] 5 custom_thresholds = [2, 4, 8, 16, 32, 64]
6 6