moved #notify_users method from observer to model, updated specs

This commit is contained in:
Vokhmin Alexey V 2012-09-19 17:55:17 +04:00
parent c77fc28b21
commit 426eb72d5f
4 changed files with 139 additions and 114 deletions

View File

@ -127,6 +127,11 @@ class BuildList < ActiveRecord::Base
after_transition :on => :published, :do => [:set_version_and_tag, :actualize_packages]
after_transition :on => [:published, :fail_publish],
:do => :notify_users, :if => lambda { |build_list| build_list.auto_publish? }
after_transition :on => [:published, :fail_publish, :build_success, :build_error],
:do => :notify_users, :if => lambda { |build_list| !build_list.auto_publish? }
event :place_build do
transition :waiting_for_response => :build_pending, :if => lambda { |build_list|
build_list.add_to_queue == BuildServer::SUCCESS
@ -290,6 +295,22 @@ class BuildList < ActiveRecord::Base
protected
def notify_users
unless mass_build_id
users = []
if project # find associated users
users = project.all_members.
select{ |user| user.notifier.can_notify? && user.notifier.new_associated_build? }
end
if user.notifier.can_notify? && user.notifier.new_build?
users = users | [user]
end
users.each do |user|
UserMailer.build_list_notification(self, user).deliver
end
end
end # notify_users
def delete_container
if can_cancel?
BuildServer.delete_build_list bs_id

View File

@ -1,7 +1,4 @@
class BuildListObserver < ActiveRecord::Observer
PUBLICATION_STATUSES = [BuildList::BUILD_PUBLISHED, BuildList::FAILED_PUBLISH]
STATUSES = [BuildServer::BUILD_ERROR, BuildServer::SUCCESS] + PUBLICATION_STATUSES
observe :build_list
def before_update(record)
@ -18,28 +15,7 @@ class BuildListObserver < ActiveRecord::Observer
record.project.update_attributes({ :average_build_time => new_av_time, :build_count => build_count + 1 }, :without_protection => true)
end
end
BuildListObserver.notify_users(record)
end
end # before_update
private
def self.notify_users(build_list)
if !build_list.mass_build_id &&
( (build_list.auto_publish? && PUBLICATION_STATUSES.include?(build_list.status)) ||
(!build_list.auto_publish? && STATUSES.include?(build_list.status)) )
users = []
if build_list.project # find associated users
users = build_list.project.all_members.
select{ |user| user.notifier.can_notify? && user.notifier.new_associated_build? }
end
if build_list.user.notifier.can_notify? && build_list.user.notifier.new_build?
users = users | [build_list.user]
end
users.each do |user|
UserMailer.build_list_notification(build_list, user).deliver
end
end
end # notify_users
end

View File

@ -1,94 +1,5 @@
require 'spec_helper'
describe BuildListObserver do
context "notify users" do
let!(:user) { FactoryGirl.create(:user) }
let!(:build_list) { FactoryGirl.create(:build_list, :user => user, :auto_publish => false) }
before(:all) { ActionMailer::Base.deliveries = [] }
before { build_list.update_attribute(:status, BuildServer::BUILD_STARTED) }
after { ActionMailer::Base.deliveries = [] }
shared_examples_for 'build list notifications by email' do
it "gets notification by email when status - Build complete" do
build_list.update_attribute(:status, BuildServer::SUCCESS)
should have(1).item
end
it "gets notification by email when status - Build published" do
build_list.update_attribute(:status, BuildList::BUILD_PUBLISHED)
should have(1).item
end
it "gets notification by email when auto_publish and status - Build published" do
build_list.update_attributes(:auto_publish => true, :status => BuildList::BUILD_PUBLISHED)
should have(1).item
end
it "doesn't get notification by email when auto_publish and status - Build complete" do
build_list.update_attributes(:auto_publish => true, :status => BuildServer::SUCCESS)
should have(:no).items
end
it "doesn't get notification by email when mass build" do
build_list.update_attributes(:mass_build_id => 1, :status => BuildList::BUILD_PUBLISHED)
should have(:no).items
end
it "doesn't get notification by email when notification by email has been disabled" do
notifier.update_attribute(:can_notify, false)
build_list.update_attribute(:status, BuildServer::SUCCESS)
should have(:no).items
end
end
subject { ActionMailer::Base.deliveries }
context "user created build task" do
let!(:notifier) { user.notifier }
before do
notifier.update_attribute(:new_associated_build, false)
build_list.project.owner.notifier.update_attribute(:can_notify, false)
end
it_should_behave_like 'build list notifications by email'
it "doesn't get notification by email when 'build list' notifications has been disabled" do
notifier.update_attribute(:new_build, false)
build_list.update_attribute(:status, BuildServer::SUCCESS)
should have(:no).items
end
it "doesn't get notification by email when 'build list' notifications - enabled, email notifications - disabled" do
notifier.update_attributes(:can_notify => false, :new_build => true)
build_list.update_attribute(:status, BuildServer::SUCCESS)
should have(:no).items
end
end
context "build task has been created and associated user" do
let!(:notifier) { build_list.project.owner.notifier }
before do
notifier.update_attribute(:new_build, false)
user.notifier.update_attribute(:can_notify, false)
end
it_should_behave_like 'build list notifications by email'
it "doesn't get notification by email when 'associated build list' notifications has been disabled" do
notifier.update_attribute(:new_associated_build, false)
build_list.update_attribute(:status, BuildServer::SUCCESS)
should have(:no).items
end
it "doesn't get notification by email when 'associated build list' notifications - enabled, email notifications - disabled" do
notifier.update_attributes(:can_notify => false, :new_associated_build => true)
build_list.update_attribute(:status, BuildServer::SUCCESS)
should have(:no).items
end
end
end # notify users
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -0,0 +1,117 @@
# -*- encoding : utf-8 -*-
require 'spec_helper'
describe BuildList do
context "#notify_users" do
before { stub_symlink_methods }
let!(:user) { FactoryGirl.create(:user) }
let!(:build_list) { FactoryGirl.create(:build_list_core,
:user => user,
:auto_publish => false) }
let!(:build_list_package) { FactoryGirl.create(:build_list_package,
:build_list => build_list,
:project => build_list.project) }
before(:all) { ActionMailer::Base.deliveries = [] }
before { build_list.update_attribute(:status, BuildServer::BUILD_STARTED) }
after { ActionMailer::Base.deliveries = [] }
shared_examples_for 'build list notifications by email' do
it "gets notification by email when status - Build complete" do
build_list.build_success
should have(1).item
end
it "gets notification by email when status - Build published" do
build_list.update_attributes(:status => BuildList::BUILD_PUBLISH)
build_list.published
should have(1).item
end
it "gets notification by email when auto_publish and status - Build published" do
build_list.update_attributes(:auto_publish => true, :status => BuildList::BUILD_PUBLISH)
build_list.published
should have(1).item
end
it "doesn't get notification by email when auto_publish and status - Build complete" do
build_list.update_attributes(:auto_publish => true)
build_list.build_success
should have(:no).items
end
it "doesn't get notification by email when mass build" do
build_list.update_attributes(:mass_build_id => 1, :status => BuildList::BUILD_PUBLISH)
build_list.published
should have(:no).items
end
it "doesn't get notification by email when notification by email has been disabled" do
notifier.update_attributes(:can_notify => false)
build_list.build_success
should have(:no).items
end
end
subject { ActionMailer::Base.deliveries }
context "user created build task" do
let!(:notifier) { user.notifier }
before do
notifier.update_attributes(:new_associated_build => false)
build_list.project.owner.notifier.update_attributes(:can_notify => false)
end
it_should_behave_like 'build list notifications by email'
it "doesn't get notification by email when 'build list' notifications has been disabled" do
notifier.update_attributes(:new_build => false)
build_list.build_success
should have(:no).items
end
it "doesn't get notification by email when 'build list' notifications - enabled, email notifications - disabled" do
notifier.update_attributes(:can_notify => false, :new_build => true)
build_list.build_success
should have(:no).items
end
end
context "build task has been created and associated user" do
let!(:notifier) { build_list.project.owner.notifier }
before do
notifier.update_attributes(:new_build => false)
user.notifier.update_attributes(:can_notify => false)
end
it_should_behave_like 'build list notifications by email'
it "doesn't get notification by email when 'associated build list' notifications has been disabled" do
notifier.update_attributes(:new_associated_build => false)
build_list.build_success
should have(:no).items
end
it "doesn't get notification by email when 'associated build list' notifications - enabled, email notifications - disabled" do
notifier.update_attributes(:can_notify => false, :new_associated_build => true)
build_list.build_success
should have(:no).items
end
end
it "doesn't get 2 notification by email when user associated to project and created task" do
bl = FactoryGirl.create(:build_list_core,
:user => user,
:auto_publish => true,
:project => FactoryGirl.create(:project, :owner => user))
FactoryGirl.create(:build_list_package, :build_list => bl, :project => bl.project)
bl.update_attributes(:status => BuildList::BUILD_PUBLISH)
bl.published
should have(1).item
end
end # notify_users
end