Commit d9cd6269 authored by Valeriy Sizov's avatar Valeriy Sizov
Browse files

System Hooks: move callback to observer

parent dd14e117f698
......@@ -107,32 +107,6 @@ def saved?
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")
......
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
......
......@@ -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|
......
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
......@@ -23,7 +23,7 @@ class Application < Rails::Application
# 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.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment