System Hooks: move callback to observer

This commit is contained in:
Valeriy Sizov 2012-07-20 01:01:29 +03:00
parent 86bd11cbd8
commit d9cd6269e9
5 changed files with 69 additions and 74 deletions

View file

@ -107,32 +107,6 @@ class Project < ActiveRecord::Base
validate :check_limit
validate :repo_name
after_create :create_hooks
after_destroy :destroy_hooks
def create_hooks
SystemHook.all_hooks_fire({
event_name: "project_create",
name: self.name,
path: self.path,
project_id: self.id,
owner_name: self.owner.name,
owner_email: self.owner.email,
created_at: self.created_at
})
end
def destroy_hooks
SystemHook.all_hooks_fire({
event_name: "project_destroy",
name: self.name,
path: self.path,
project_id: self.id,
owner_name: self.owner.name,
owner_email: self.owner.email,
})
end
def check_limit
unless owner.can_create_project?
errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it")

View file

@ -1,4 +1,5 @@
class User < ActiveRecord::Base
include Account
devise :database_authenticatable, :token_authenticatable, :lockable,
@ -57,25 +58,6 @@ class User < ActiveRecord::Base
scope :active, where(:blocked => false)
before_validation :generate_password, :on => :create
after_create :create_hooks
after_destroy :destroy_hooks
def create_hooks
SystemHook.all_hooks_fire({
event_name: "user_create",
name: self.name,
email: self.email,
created_at: self.created_at
})
end
def destroy_hooks
SystemHook.all_hooks_fire({
event_name: "user_destroy",
name: self.name,
email: self.email
})
end
def generate_password
if self.force_random_password

View file

@ -11,9 +11,6 @@ class UsersProject < ActiveRecord::Base
after_save :update_repository
after_destroy :update_repository
after_create :add_to_team_hooks
after_destroy :remove_from_team_hooks
validates_uniqueness_of :user_id, :scope => [:project_id]
validates_presence_of :user_id
@ -21,31 +18,6 @@ class UsersProject < ActiveRecord::Base
delegate :name, :email, :to => :user, :prefix => true
def add_to_team_hooks
SystemHook.all_hooks_fire({
event_name: "user_add_to_team",
project_name: self.project.name,
project_path: self.project.path,
project_id: self.project_id,
user_name: self.user.name,
user_email: self.user.email,
project_access: self.repo_access_human,
created_at: self.created_at
})
end
def remove_from_team_hooks
SystemHook.all_hooks_fire({
event_name: "user_remove_from_team",
project_name: self.project.name,
project_path: self.project.path,
project_id: self.project_id,
user_name: self.user.name,
user_email: self.user.email,
project_access: self.repo_access_human
})
end
def self.bulk_import(project, user_ids, project_access)
UsersProject.transaction do
user_ids.each do |user_id|

View file

@ -0,0 +1,67 @@
class SystemHookObserver < ActiveRecord::Observer
observe :user, :project, :users_project
def after_create(model)
if model.kind_of? Project
SystemHook.all_hooks_fire({
event_name: "project_create",
name: model.name,
path: model.path,
project_id: model.id,
owner_name: model.owner.name,
owner_email: model.owner.email,
created_at: model.created_at
})
elsif model.kind_of? User
SystemHook.all_hooks_fire({
event_name: "user_create",
name: model.name,
email: model.email,
created_at: model.created_at
})
elsif model.kind_of? UsersProject
SystemHook.all_hooks_fire({
event_name: "user_add_to_team",
project_name: model.project.name,
project_path: model.project.path,
project_id: model.project_id,
user_name: model.user.name,
user_email: model.user.email,
project_access: model.repo_access_human,
created_at: model.created_at
})
end
end
def after_destroy(model)
if model.kind_of? Project
SystemHook.all_hooks_fire({
event_name: "project_destroy",
name: model.name,
path: model.path,
project_id: model.id,
owner_name: model.owner.name,
owner_email: model.owner.email,
})
elsif model.kind_of? User
SystemHook.all_hooks_fire({
event_name: "user_destroy",
name: model.name,
email: model.email
})
elsif model.kind_of? UsersProject
SystemHook.all_hooks_fire({
event_name: "user_remove_from_team",
project_name: model.project.name,
project_path: model.project.path,
project_id: model.project_id,
user_name: model.user.name,
user_email: model.user.email,
project_access: model.repo_access_human
})
end
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, :user_observer
config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer, :issue_observer, :user_observer, :system_hook_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.