Remove user observer

This commit is contained in:
Dmitriy Zaporozhets 2014-06-17 22:32:35 +03:00
parent a7d5052cd4
commit a90a201d0e
4 changed files with 37 additions and 50 deletions

View file

@ -132,6 +132,10 @@ class User < ActiveRecord::Base
before_validation :sanitize_attrs
before_save :ensure_authentication_token
after_save :ensure_namespace_correct
after_create :post_create_hook
after_destroy :post_destroy_hook
alias_attribute :private_token, :authentication_token
@ -490,4 +494,36 @@ class User < ActiveRecord::Base
GravatarService.new.execute(email, size)
end
end
def ensure_namespace_correct
# Ensure user has namespace
self.create_namespace!(path: self.username, name: self.username) unless self.namespace
if self.username_changed?
self.namespace.update_attributes(path: self.username, name: self.username)
end
end
def post_create_hook
log_info("User \"#{self.name}\" (#{self.email}) was created")
notification.new_user(self)
system_hook_service.execute_hooks_for(self, :create)
end
def post_destroy_hook
log_info("User \"#{self.name}\" (#{self.email}) was removed")
system_hook_service.execute_hooks_for(self, :destroy)
end
def notification
NotificationService.new
end
def log_info message
Gitlab::AppLogger.info message
end
def system_hook_service
SystemHooksService.new
end
end

View file

@ -1,21 +0,0 @@
class UserObserver < BaseObserver
def after_create(user)
log_info("User \"#{user.name}\" (#{user.email}) was created")
notification.new_user(user)
system_hook_service.execute_hooks_for(user, :create)
end
def after_destroy user
log_info("User \"#{user.name}\" (#{user.email}) was removed")
system_hook_service.execute_hooks_for(user, :destroy)
end
def after_save user
# Ensure user has namespace
user.create_namespace!(path: user.username, name: user.username) unless user.namespace
if user.username_changed?
user.namespace.update_attributes(path: user.username, name: user.username)
end
end
end

View file

@ -19,8 +19,7 @@ module Gitlab
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
config.active_record.observers = :user_observer,
:users_project_observer
config.active_record.observers = :users_project_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

@ -1,27 +0,0 @@
require 'spec_helper'
describe UserObserver do
before(:each) { enable_observers }
after(:each) {disable_observers}
subject { UserObserver.instance }
before { subject.stub(notification: double('NotificationService').as_null_object) }
it 'calls #after_create when new users are created' do
new_user = build(:user)
subject.should_receive(:after_create).with(new_user)
new_user.save
end
context 'when a new user is created' do
it 'sends an email' do
subject.should_receive(:notification)
create(:user)
end
it 'trigger logger' do
user = double(:user, id: 42, password: 'P@ssword!', name: 'John', email: 'u@mail.local', extern_uid?: false)
Gitlab::AppLogger.should_receive(:info)
create(:user)
end
end
end