diff --git a/app/models/deploy.rb b/app/models/deploy.rb index 45b45f5..3828ecc 100644 --- a/app/models/deploy.rb +++ b/app/models/deploy.rb @@ -12,16 +12,11 @@ class Deploy embedded_in :app, :inverse_of => :deploys - after_create :deliver_notification, :if => :should_notify? after_create :resolve_app_errs, :if => :should_resolve_app_errs? after_create :store_cached_attributes_on_problems validates_presence_of :username, :environment - def deliver_notification - Mailer.deploy_notification(self).deliver - end - def resolve_app_errs app.problems.unresolved.in_env(environment).each {|problem| problem.resolve!} end @@ -32,10 +27,6 @@ class Deploy protected - def should_notify? - app.notify_on_deploys? && app.notification_recipients.any? - end - def should_resolve_app_errs? app.resolve_errs_on_deploy? end diff --git a/app/models/deploy_observer.rb b/app/models/deploy_observer.rb new file mode 100644 index 0000000..5431f2e --- /dev/null +++ b/app/models/deploy_observer.rb @@ -0,0 +1,9 @@ +class DeployObserver < Mongoid::Observer + observe :deploy + + def after_create deploy + return unless deploy.app.notify_on_deploys? && deploy.app.notification_recipients.any? + + Mailer.deploy_notification(deploy).deliver + end +end diff --git a/config/application.rb b/config/application.rb index 481978d..95c04e3 100644 --- a/config/application.rb +++ b/config/application.rb @@ -51,6 +51,9 @@ module Errbit # IssueTracker subclasses use inheritance, so preloading models provides querying consistency in dev mode. config.mongoid.preload_models = true + # Set up observers + config.mongoid.observers = :deploy_observer + # Configure the default encoding used in templates for Ruby 1.9. config.encoding = "utf-8" diff --git a/spec/models/deploy_observer_spec.rb b/spec/models/deploy_observer_spec.rb new file mode 100644 index 0000000..cd7201b --- /dev/null +++ b/spec/models/deploy_observer_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe DeployObserver do + context 'when a Deploy is saved' do + context 'and the app should notify on deploys' do + it 'should send an email notification' do + Mailer.should_receive(:deploy_notification). + and_return(mock('email', :deliver => true)) + Fabricate(:deploy, :app => Fabricate(:app_with_watcher, :notify_on_deploys => true)) + end + end + + context 'and the app is not set to notify on deploys' do + it 'should not send an email notification' do + Mailer.should_not_receive(:deploy_notification) + Fabricate(:deploy, :app => Fabricate(:app_with_watcher, :notify_on_deploys => false)) + end + end + end +end diff --git a/spec/models/deploy_spec.rb b/spec/models/deploy_spec.rb index 7ef93a6..d730894 100644 --- a/spec/models/deploy_spec.rb +++ b/spec/models/deploy_spec.rb @@ -17,12 +17,6 @@ describe Deploy do end context 'being created' do - it 'should send an email notification' do - Mailer.should_receive(:deploy_notification). - and_return(mock('email', :deliver => true)) - Fabricate(:deploy, :app => Fabricate(:app_with_watcher, :notify_on_deploys => true)) - end - context 'when the app has resolve_errs_on_deploy set to false' do it 'should not resolve the apps errs' do app = Fabricate(:app, :resolve_errs_on_deploy => false) @@ -43,12 +37,6 @@ describe Deploy do end end - context 'when the app has deploy notifications set to false' do - it 'should not send an email notification' do - Mailer.should_not_receive(:deploy_notification) - Fabricate(:deploy, :app => Fabricate(:app_with_watcher, :notify_on_deploys => false)) - end - end end it "should produce a shortened revision with 7 characters" do -- libgit2 0.21.2