summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-06-17 22:32:35 +0300
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-06-17 22:32:35 +0300
commita90a201d0ee3c9e33cc04075ac8a2509c1d2acc9 (patch)
treef188438bd919e4ae20f4f48725cb7b77927970d9
parenta7d5052cd4e117768d9c32f582ed72d2cc98e5e6 (diff)
downloadgitlab-ce-a90a201d0ee3c9e33cc04075ac8a2509c1d2acc9.tar.gz
Remove user observer
-rw-r--r--app/models/user.rb36
-rw-r--r--app/observers/user_observer.rb21
-rw-r--r--config/application.rb3
-rw-r--r--spec/observers/user_observer_spec.rb27
4 files changed, 37 insertions, 50 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index 2352f8c050b..5e371e2c60b 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -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
diff --git a/app/observers/user_observer.rb b/app/observers/user_observer.rb
deleted file mode 100644
index a7b1a857e29..00000000000
--- a/app/observers/user_observer.rb
+++ /dev/null
@@ -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
diff --git a/config/application.rb b/config/application.rb
index b55729be6ba..b68a4ffe347 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -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.
diff --git a/spec/observers/user_observer_spec.rb b/spec/observers/user_observer_spec.rb
deleted file mode 100644
index 9aeade535f9..00000000000
--- a/spec/observers/user_observer_spec.rb
+++ /dev/null
@@ -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