Extract observation of User to a UserObserver

This commit is contained in:
Robb Kidd 2012-06-20 12:29:10 -04:00
parent 97ca4f5dda
commit 88964132ed
5 changed files with 37 additions and 10 deletions

View file

@ -1,9 +1,8 @@
class MailerObserver < ActiveRecord::Observer
observe :issue, :user, :note, :merge_request
observe :issue, :note, :merge_request
cattr_accessor :current_user
def after_create(model)
new_user(model) if model.kind_of?(User)
new_note(model) if model.kind_of?(Note)
new_merge_request(model) if model.kind_of?(MergeRequest)
end
@ -14,10 +13,6 @@ class MailerObserver < ActiveRecord::Observer
protected
def new_user(user)
Notify.new_user_email(user.id, user.password).deliver
end
def new_note(note)
if note.notify
# Notify whole team except author of note

View file

@ -0,0 +1,5 @@
class UserObserver < ActiveRecord::Observer
def after_create(user)
Notify.new_user_email(user.id, user.password).deliver
end
end

View file

@ -23,7 +23,7 @@ module Gitlab
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer, :issue_observer
config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer, :issue_observer, :user_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.

View file

@ -0,0 +1,26 @@
require 'spec_helper'
describe UserObserver do
subject { UserObserver.instance }
it 'calls #after_create when new users are created' do
new_user = Factory.new(:user)
subject.should_receive(:after_create).with(new_user)
User.observers.enable :user_observer do
new_user.save
end
end
context 'when a new user is created' do
let(:user) { double(:user, id: 42, password: 'P@ssword!') }
let(:notification) { double :notification }
it 'sends an email' do
notification.should_receive(:deliver)
Notify.should_receive(:new_user_email).with(user.id, user.password).and_return(notification)
subject.after_create(user)
end
end
end

View file

@ -40,14 +40,15 @@ describe "Admin::Users" do
end
it "should call send mail" do
User.observers.enable :mailer_observer do
Notify.should_receive(:new_user_email).and_return(stub(:deliver => true))
User.observers.enable :user_observer do
click_button "Save"
end
end
it "should send valid email to user with email & password" do
User.observers.enable :mailer_observer do
User.observers.enable :user_observer do
with_resque do
click_button "Save"
end