Commit 50bc6a38e425576d75232daede21769a7e742254

Authored by Cyril Mougel
2 parents f53baa7c 1c41a8fd
Exists in master and in 1 other branch production

Merge pull request #582 from arthurnn/remove_observer

Remove Observers
app/models/comment.rb
... ... @@ -5,6 +5,8 @@ class Comment
5 5 after_create :increase_counter_cache
6 6 before_destroy :decrease_counter_cache
7 7  
  8 + after_create :deliver_email, :if => :emailable?
  9 +
8 10 field :body, :type => String
9 11 index(:user_id => 1)
10 12  
... ... @@ -14,6 +16,10 @@ class Comment
14 16  
15 17 validates_presence_of :body
16 18  
  19 + def deliver_email
  20 + Mailer.comment_notification(self).deliver
  21 + end
  22 +
17 23 def notification_recipients
18 24 app.notification_recipients - [user.email]
19 25 end
... ...
app/models/comment_observer.rb
... ... @@ -1,8 +0,0 @@
1   -class CommentObserver < Mongoid::Observer
2   - observe :comment
3   -
4   - def after_create(comment)
5   - Mailer.comment_notification(comment).deliver if comment.emailable?
6   - end
7   -
8   -end
app/models/deploy.rb
... ... @@ -14,6 +14,7 @@ class Deploy
14 14  
15 15 after_create :resolve_app_errs, :if => :should_resolve_app_errs?
16 16 after_create :store_cached_attributes_on_problems
  17 + after_create :deliver_email
17 18  
18 19 validates_presence_of :username, :environment
19 20  
... ... @@ -34,5 +35,12 @@ class Deploy
34 35 def store_cached_attributes_on_problems
35 36 Problem.where(:app_id => app.id).each(&:cache_app_attributes)
36 37 end
  38 +
  39 + def deliver_email
  40 + if app.notify_on_deploys? && app.notification_recipients.any?
  41 + Mailer.deploy_notification(self).deliver
  42 + end
  43 + end
  44 +
37 45 end
38 46  
... ...
app/models/deploy_observer.rb
... ... @@ -1,9 +0,0 @@
1   -class DeployObserver < Mongoid::Observer
2   - observe :deploy
3   -
4   - def after_create deploy
5   - return unless deploy.app.notify_on_deploys? && deploy.app.notification_recipients.any?
6   -
7   - Mailer.deploy_notification(deploy).deliver
8   - end
9   -end
config/application.rb
... ... @@ -29,9 +29,6 @@ module Errbit
29 29 # :all can be used as a placeholder for all plugins not explicitly named.
30 30 # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
31 31  
32   - # Activate observers that should always be running.
33   - # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
34   -
35 32 # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
36 33 # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
37 34 # config.time_zone = 'Central Time (US & Canada)'
... ... @@ -54,9 +51,6 @@ module Errbit
54 51 # IssueTracker subclasses use inheritance, so preloading models provides querying consistency in dev mode.
55 52 config.mongoid.preload_models = true
56 53  
57   - # Set up observers
58   - config.mongoid.observers = :deploy_observer, :comment_observer
59   -
60 54 # Configure the default encoding used in templates for Ruby 1.9.
61 55 config.encoding = "utf-8"
62 56  
... ...
spec/models/comment_observer_spec.rb
1 1 require 'spec_helper'
2 2  
3   -describe CommentObserver do
  3 +describe "Callback on Comment" do
4 4 context 'when a Comment is saved' do
5 5 let(:comment) { Fabricate.build(:comment) }
6 6  
... ...
spec/models/deploy_observer_spec.rb
1 1 require 'spec_helper'
2 2  
3   -describe DeployObserver do
  3 +describe "Callback on Deploy" do
4 4 context 'when a Deploy is saved' do
5 5 context 'and the app should notify on deploys' do
6 6 it 'should send an email notification' do
... ...