refactor observers. Added BaseObserver with helper methods
This commit is contained in:
parent
6abf58466f
commit
ce0945efcd
9 changed files with 26 additions and 45 deletions
11
app/observers/base_observer.rb
Normal file
11
app/observers/base_observer.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
class BaseObserver < ActiveRecord::Observer
|
||||||
|
protected
|
||||||
|
|
||||||
|
def notification
|
||||||
|
NotificationService.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def log_info message
|
||||||
|
Gitlab::AppLogger.info message
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,4 +1,4 @@
|
||||||
class IssueObserver < ActiveRecord::Observer
|
class IssueObserver < BaseObserver
|
||||||
cattr_accessor :current_user
|
cattr_accessor :current_user
|
||||||
|
|
||||||
def after_create(issue)
|
def after_create(issue)
|
||||||
|
@ -27,8 +27,4 @@ class IssueObserver < ActiveRecord::Observer
|
||||||
def create_note(issue)
|
def create_note(issue)
|
||||||
Note.create_status_change_note(issue, current_user, issue.state)
|
Note.create_status_change_note(issue, current_user, issue.state)
|
||||||
end
|
end
|
||||||
|
|
||||||
def notification
|
|
||||||
NotificationService.new
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
class KeyObserver < ActiveRecord::Observer
|
class KeyObserver < BaseObserver
|
||||||
include Gitlab::ShellAdapter
|
|
||||||
|
|
||||||
def after_save(key)
|
def after_save(key)
|
||||||
GitlabShellWorker.perform_async(
|
GitlabShellWorker.perform_async(
|
||||||
:add_key,
|
:add_key,
|
||||||
|
@ -8,8 +6,7 @@ class KeyObserver < ActiveRecord::Observer
|
||||||
key.key
|
key.key
|
||||||
)
|
)
|
||||||
|
|
||||||
# Notify about ssh key being added
|
notification.new_key(key)
|
||||||
NotificationService.new.new_key(key)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_destroy(key)
|
def after_destroy(key)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
class MergeRequestObserver < ActiveRecord::Observer
|
class MergeRequestObserver < BaseObserver
|
||||||
cattr_accessor :current_user
|
cattr_accessor :current_user
|
||||||
|
|
||||||
def after_create(merge_request)
|
def after_create(merge_request)
|
||||||
|
@ -16,10 +16,4 @@ class MergeRequestObserver < ActiveRecord::Observer
|
||||||
def after_update(merge_request)
|
def after_update(merge_request)
|
||||||
notification.reassigned_merge_request(merge_request) if merge_request.is_being_reassigned?
|
notification.reassigned_merge_request(merge_request) if merge_request.is_being_reassigned?
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
|
||||||
|
|
||||||
def notification
|
|
||||||
NotificationService.new
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,5 @@
|
||||||
class NoteObserver < ActiveRecord::Observer
|
class NoteObserver < BaseObserver
|
||||||
def after_create(note)
|
def after_create(note)
|
||||||
notification.new_note(note)
|
notification.new_note(note)
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
|
||||||
|
|
||||||
def notification
|
|
||||||
NotificationService.new
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
class ProjectObserver < ActiveRecord::Observer
|
class ProjectObserver < BaseObserver
|
||||||
def after_create(project)
|
def after_create(project)
|
||||||
GitlabShellWorker.perform_async(
|
GitlabShellWorker.perform_async(
|
||||||
:add_repository,
|
:add_repository,
|
||||||
|
@ -27,10 +27,4 @@ class ProjectObserver < ActiveRecord::Observer
|
||||||
|
|
||||||
log_info("Project \"#{project.name}\" was removed")
|
log_info("Project \"#{project.name}\" was removed")
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
|
||||||
|
|
||||||
def log_info message
|
|
||||||
Gitlab::AppLogger.info message
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
class SystemHookObserver < ActiveRecord::Observer
|
class SystemHookObserver < BaseObserver
|
||||||
observe :user, :project, :users_project
|
observe :user, :project, :users_project
|
||||||
|
|
||||||
def after_create(model)
|
def after_create(model)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
class UserObserver < ActiveRecord::Observer
|
class UserObserver < BaseObserver
|
||||||
def after_create(user)
|
def after_create(user)
|
||||||
log_info("User \"#{user.name}\" (#{user.email}) was created")
|
log_info("User \"#{user.name}\" (#{user.email}) was created")
|
||||||
|
|
||||||
|
@ -18,14 +18,4 @@ class UserObserver < ActiveRecord::Observer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
|
||||||
|
|
||||||
def log_info message
|
|
||||||
Gitlab::AppLogger.info message
|
|
||||||
end
|
|
||||||
|
|
||||||
def notification
|
|
||||||
NotificationService.new
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
class UsersProjectObserver < ActiveRecord::Observer
|
class UsersProjectObserver < BaseObserver
|
||||||
def after_commit(users_project)
|
def after_commit(users_project)
|
||||||
return if users_project.destroyed?
|
return if users_project.destroyed?
|
||||||
Notify.delay.project_access_granted_email(users_project.id)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_create(users_project)
|
def after_create(users_project)
|
||||||
|
@ -10,6 +9,12 @@ class UsersProjectObserver < ActiveRecord::Observer
|
||||||
action: Event::JOINED,
|
action: Event::JOINED,
|
||||||
author_id: users_project.user.id
|
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
|
end
|
||||||
|
|
||||||
def after_destroy(users_project)
|
def after_destroy(users_project)
|
||||||
|
|
Loading…
Add table
Reference in a new issue