From ce0945efcd36cc89d1b633500e4bdecf373fc304 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 26 Mar 2013 18:11:38 +0200 Subject: [PATCH] refactor observers. Added BaseObserver with helper methods --- app/observers/base_observer.rb | 11 +++++++++++ app/observers/issue_observer.rb | 6 +----- app/observers/key_observer.rb | 7 ++----- app/observers/merge_request_observer.rb | 8 +------- app/observers/note_observer.rb | 8 +------- app/observers/project_observer.rb | 8 +------- app/observers/system_hook_observer.rb | 2 +- app/observers/user_observer.rb | 12 +----------- app/observers/users_project_observer.rb | 9 +++++++-- 9 files changed, 26 insertions(+), 45 deletions(-) create mode 100644 app/observers/base_observer.rb diff --git a/app/observers/base_observer.rb b/app/observers/base_observer.rb new file mode 100644 index 00000000..b4641bf8 --- /dev/null +++ b/app/observers/base_observer.rb @@ -0,0 +1,11 @@ +class BaseObserver < ActiveRecord::Observer + protected + + def notification + NotificationService.new + end + + def log_info message + Gitlab::AppLogger.info message + end +end diff --git a/app/observers/issue_observer.rb b/app/observers/issue_observer.rb index 7e8e9ccd..03ce4b95 100644 --- a/app/observers/issue_observer.rb +++ b/app/observers/issue_observer.rb @@ -1,4 +1,4 @@ -class IssueObserver < ActiveRecord::Observer +class IssueObserver < BaseObserver cattr_accessor :current_user def after_create(issue) @@ -27,8 +27,4 @@ class IssueObserver < ActiveRecord::Observer def create_note(issue) Note.create_status_change_note(issue, current_user, issue.state) end - - def notification - NotificationService.new - end end diff --git a/app/observers/key_observer.rb b/app/observers/key_observer.rb index 2a69b876..28fef55a 100644 --- a/app/observers/key_observer.rb +++ b/app/observers/key_observer.rb @@ -1,6 +1,4 @@ -class KeyObserver < ActiveRecord::Observer - include Gitlab::ShellAdapter - +class KeyObserver < BaseObserver def after_save(key) GitlabShellWorker.perform_async( :add_key, @@ -8,8 +6,7 @@ class KeyObserver < ActiveRecord::Observer key.key ) - # Notify about ssh key being added - NotificationService.new.new_key(key) + notification.new_key(key) end def after_destroy(key) diff --git a/app/observers/merge_request_observer.rb b/app/observers/merge_request_observer.rb index 64585570..15214016 100644 --- a/app/observers/merge_request_observer.rb +++ b/app/observers/merge_request_observer.rb @@ -1,4 +1,4 @@ -class MergeRequestObserver < ActiveRecord::Observer +class MergeRequestObserver < BaseObserver cattr_accessor :current_user def after_create(merge_request) @@ -16,10 +16,4 @@ class MergeRequestObserver < ActiveRecord::Observer def after_update(merge_request) notification.reassigned_merge_request(merge_request) if merge_request.is_being_reassigned? end - - protected - - def notification - NotificationService.new - end end diff --git a/app/observers/note_observer.rb b/app/observers/note_observer.rb index 944c6839..7b79161c 100644 --- a/app/observers/note_observer.rb +++ b/app/observers/note_observer.rb @@ -1,11 +1,5 @@ -class NoteObserver < ActiveRecord::Observer +class NoteObserver < BaseObserver def after_create(note) notification.new_note(note) end - - protected - - def notification - NotificationService.new - end end diff --git a/app/observers/project_observer.rb b/app/observers/project_observer.rb index 89dc97ac..7d7ecdd3 100644 --- a/app/observers/project_observer.rb +++ b/app/observers/project_observer.rb @@ -1,4 +1,4 @@ -class ProjectObserver < ActiveRecord::Observer +class ProjectObserver < BaseObserver def after_create(project) GitlabShellWorker.perform_async( :add_repository, @@ -27,10 +27,4 @@ class ProjectObserver < ActiveRecord::Observer log_info("Project \"#{project.name}\" was removed") end - - protected - - def log_info message - Gitlab::AppLogger.info message - end end diff --git a/app/observers/system_hook_observer.rb b/app/observers/system_hook_observer.rb index be2594b4..3a649fd5 100644 --- a/app/observers/system_hook_observer.rb +++ b/app/observers/system_hook_observer.rb @@ -1,4 +1,4 @@ -class SystemHookObserver < ActiveRecord::Observer +class SystemHookObserver < BaseObserver observe :user, :project, :users_project def after_create(model) diff --git a/app/observers/user_observer.rb b/app/observers/user_observer.rb index 7ce3be0c..6bb3c471 100644 --- a/app/observers/user_observer.rb +++ b/app/observers/user_observer.rb @@ -1,4 +1,4 @@ -class UserObserver < ActiveRecord::Observer +class UserObserver < BaseObserver def after_create(user) log_info("User \"#{user.name}\" (#{user.email}) was created") @@ -18,14 +18,4 @@ class UserObserver < ActiveRecord::Observer end end end - - protected - - def log_info message - Gitlab::AppLogger.info message - end - - def notification - NotificationService.new - end end diff --git a/app/observers/users_project_observer.rb b/app/observers/users_project_observer.rb index 66b42175..ca9649c7 100644 --- a/app/observers/users_project_observer.rb +++ b/app/observers/users_project_observer.rb @@ -1,7 +1,6 @@ -class UsersProjectObserver < ActiveRecord::Observer +class UsersProjectObserver < BaseObserver def after_commit(users_project) return if users_project.destroyed? - Notify.delay.project_access_granted_email(users_project.id) end def after_create(users_project) @@ -10,6 +9,12 @@ class UsersProjectObserver < ActiveRecord::Observer action: Event::JOINED, author_id: users_project.user.id ) + + notification.new_team_member(users_project) + end + + def after_update(users_project) + notification.update_team_member(users_project) end def after_destroy(users_project)