Commit 2d875568 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Remove rails observers from application

parent a90a201d
...@@ -11,7 +11,6 @@ end ...@@ -11,7 +11,6 @@ end
gem "rails", "~> 4.1.0" gem "rails", "~> 4.1.0"
gem "protected_attributes" gem "protected_attributes"
gem 'rails-observers'
# Make links from text # Make links from text
gem 'rails_autolink', '~> 1.1' gem 'rails_autolink', '~> 1.1'
......
...@@ -365,8 +365,6 @@ GEM ...@@ -365,8 +365,6 @@ GEM
bundler (>= 1.3.0, < 2.0) bundler (>= 1.3.0, < 2.0)
railties (= 4.1.1) railties (= 4.1.1)
sprockets-rails (~> 2.0) sprockets-rails (~> 2.0)
rails-observers (0.1.2)
activemodel (~> 4.0)
rails_autolink (1.1.6) rails_autolink (1.1.6)
rails (> 3.1) rails (> 3.1)
rails_best_practices (1.14.4) rails_best_practices (1.14.4)
...@@ -644,7 +642,6 @@ DEPENDENCIES ...@@ -644,7 +642,6 @@ DEPENDENCIES
rack-cors rack-cors
rack-mini-profiler rack-mini-profiler
rails (~> 4.1.0) rails (~> 4.1.0)
rails-observers
rails_autolink (~> 1.1) rails_autolink (~> 1.1)
rails_best_practices rails_best_practices
raphael-rails (~> 2.1.2) raphael-rails (~> 2.1.2)
......
...@@ -37,6 +37,10 @@ class UsersProject < ActiveRecord::Base ...@@ -37,6 +37,10 @@ class UsersProject < ActiveRecord::Base
scope :in_projects, ->(projects) { where(project_id: projects.map { |p| p.id }) } scope :in_projects, ->(projects) { where(project_id: projects.map { |p| p.id }) }
scope :with_user, ->(user) { where(user_id: user.id) } scope :with_user, ->(user) { where(user_id: user.id) }
after_create :post_create_hook
after_update :post_update_hook
after_destroy :post_destroy_hook
class << self class << self
# Add users to project teams with passed access option # Add users to project teams with passed access option
...@@ -114,4 +118,37 @@ class UsersProject < ActiveRecord::Base ...@@ -114,4 +118,37 @@ class UsersProject < ActiveRecord::Base
def owner? def owner?
project.owner == user project.owner == user
end end
def post_create_hook
Event.create(
project_id: self.project.id,
action: Event::JOINED,
author_id: self.user.id
)
notification_service.new_team_member(self)
system_hook_service.execute_hooks_for(self, :create)
end
def post_update_hook
notification_service.update_team_member(self) if self.project_access_changed?
end
def post_destroy_hook
Event.create(
project_id: self.project.id,
action: Event::LEFT,
author_id: self.user.id
)
system_hook_service.execute_hooks_for(self, :destroy)
end
def notification_service
NotificationService.new
end
def system_hook_service
SystemHooksService.new
end
end end
class BaseObserver < ActiveRecord::Observer
def notification
NotificationService.new
end
def event_service
EventCreateService.new
end
def log_info message
Gitlab::AppLogger.info message
end
def system_hook_service
SystemHooksService.new
end
end
class UsersProjectObserver < BaseObserver
def after_create(users_project)
Event.create(
project_id: users_project.project.id,
action: Event::JOINED,
author_id: users_project.user.id
)
notification.new_team_member(users_project)
system_hook_service.execute_hooks_for(users_project, :create)
end
def after_update(users_project)
notification.update_team_member(users_project) if users_project.project_access_changed?
end
def after_destroy(users_project)
Event.create(
project_id: users_project.project.id,
action: Event::LEFT,
author_id: users_project.user.id
)
system_hook_service.execute_hooks_for(users_project, :destroy)
end
end
...@@ -18,9 +18,6 @@ module Gitlab ...@@ -18,9 +18,6 @@ module Gitlab
# :all can be used as a placeholder for all plugins not explicitly named. # :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ] # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
config.active_record.observers = :users_project_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # 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. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)' # config.time_zone = 'Central Time (US & Canada)'
......
...@@ -47,13 +47,10 @@ describe "Admin::Users", feature: true do ...@@ -47,13 +47,10 @@ describe "Admin::Users", feature: true do
it "should call send mail" do it "should call send mail" do
Notify.should_receive(:new_user_email) Notify.should_receive(:new_user_email)
User.observers.enable :user_observer do
click_button "Create user" click_button "Create user"
end end
end
it "should send valid email to user with email & password" do it "should send valid email to user with email & password" do
User.observers.enable :user_observer do
click_button "Create user" click_button "Create user"
user = User.last user = User.last
email = ActionMailer::Base.deliveries.last email = ActionMailer::Base.deliveries.last
...@@ -62,7 +59,6 @@ describe "Admin::Users", feature: true do ...@@ -62,7 +59,6 @@ describe "Admin::Users", feature: true do
email.text_part.body.should have_content('password') email.text_part.body.should have_content('password')
end end
end end
end
describe "GET /admin/users/:id" do describe "GET /admin/users/:id" do
before do before do
......
require 'spec_helper' require 'spec_helper'
describe "Profile account page", feature: true do describe "Profile account page", feature: true do
before(:each) { enable_observers }
after(:each) {disable_observers}
let(:user) { create(:user) } let(:user) { create(:user) }
before do before do
......
require 'spec_helper' require 'spec_helper'
describe "Projects", feature: true do describe "Projects", feature: true do
before(:each) { enable_observers }
after(:each) {disable_observers}
before { login_as :user } before { login_as :user }
describe "DELETE /projects/:id" do describe "DELETE /projects/:id" do
......
...@@ -2,7 +2,6 @@ require 'spec_helper' ...@@ -2,7 +2,6 @@ require 'spec_helper'
describe "Search", feature: true do describe "Search", feature: true do
before do before do
ActiveRecord::Base.observers.enable(:user_observer)
login_as :user login_as :user
@project = create(:project, namespace: @user.namespace) @project = create(:project, namespace: @user.namespace)
@project.team << [@user, :reporter] @project.team << [@user, :reporter]
......
...@@ -27,9 +27,6 @@ ...@@ -27,9 +27,6 @@
require 'spec_helper' require 'spec_helper'
describe Project do describe Project do
before { enable_observers }
after { disable_observers }
describe "Associations" do describe "Associations" do
it { should belong_to(:group) } it { should belong_to(:group) }
it { should belong_to(:namespace) } it { should belong_to(:namespace) }
......
...@@ -19,8 +19,6 @@ require "spec_helper" ...@@ -19,8 +19,6 @@ require "spec_helper"
describe SystemHook do describe SystemHook do
describe "execute" do describe "execute" do
before(:each) { ActiveRecord::Base.observers.enable(:all) }
before(:each) do before(:each) do
@system_hook = create(:system_hook) @system_hook = create(:system_hook)
WebMock.stub_request(:post, @system_hook.url) WebMock.stub_request(:post, @system_hook.url)
......
...@@ -145,7 +145,6 @@ describe User do ...@@ -145,7 +145,6 @@ describe User do
describe 'projects' do describe 'projects' do
before do before do
ActiveRecord::Base.observers.enable(:user_observer)
@user = create :user @user = create :user
@project = create :project, namespace: @user.namespace @project = create :project, namespace: @user.namespace
@project_2 = create :project, group: create(:group) # Grant MASTER access to the user @project_2 = create :project, group: create(:group) # Grant MASTER access to the user
...@@ -168,7 +167,6 @@ describe User do ...@@ -168,7 +167,6 @@ describe User do
describe 'groups' do describe 'groups' do
before do before do
ActiveRecord::Base.observers.enable(:user_observer)
@user = create :user @user = create :user
@group = create :group @group = create :group
@group.add_owner(@user) @group.add_owner(@user)
...@@ -181,7 +179,6 @@ describe User do ...@@ -181,7 +179,6 @@ describe User do
describe 'group multiple owners' do describe 'group multiple owners' do
before do before do
ActiveRecord::Base.observers.enable(:user_observer)
@user = create :user @user = create :user
@user2 = create :user @user2 = create :user
@group = create :group @group = create :group
...@@ -195,7 +192,6 @@ describe User do ...@@ -195,7 +192,6 @@ describe User do
describe 'namespaced' do describe 'namespaced' do
before do before do
ActiveRecord::Base.observers.enable(:user_observer)
@user = create :user @user = create :user
@project = create :project, namespace: @user.namespace @project = create :project, namespace: @user.namespace
end end
......
require 'spec_helper'
describe UsersProjectObserver do
before(:each) { enable_observers }
after(:each) { disable_observers }
let(:user) { create(:user) }
let(:project) { create(:project) }
subject { UsersProjectObserver.instance }
before { subject.stub(notification: double('NotificationService').as_null_object) }
describe "#after_update" do
before do
@users_project = create :users_project
end
it "should called when UsersProject updated" do
subject.should_receive(:after_update)
@users_project.update_attribute(:project_access, UsersProject::MASTER)
end
it "should send email to user" do
subject.should_receive(:notification)
@users_project.update_attribute(:project_access, UsersProject::OWNER)
end
it "should not called after UsersProject destroyed" do
subject.should_not_receive(:after_update)
@users_project.destroy
end
end
describe "#after_destroy" do
before do
@users_project = create :users_project
end
it "should called when UsersProject destroyed" do
subject.should_receive(:after_destroy)
@users_project.destroy
end
it "should create new event" do
Event.should_receive(:create)
@users_project.destroy
end
end
describe "#after_create" do
it "should send email to user" do
subject.should_receive(:notification)
Event.stub(create: true)
create(:users_project)
end
it "should create new event" do
Event.should_receive(:create)
create(:users_project)
end
end
end
...@@ -3,8 +3,6 @@ require 'mime/types' ...@@ -3,8 +3,6 @@ require 'mime/types'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) {disable_observers}
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
......
...@@ -3,9 +3,6 @@ require 'mime/types' ...@@ -3,9 +3,6 @@ require 'mime/types'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) {disable_observers}
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
let!(:project) { create(:project, creator_id: user.id) } let!(:project) { create(:project, creator_id: user.id) }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) { create(:project, namespace: user.namespace ) } let!(:project) { create(:project, namespace: user.namespace ) }
before { project.team << [user, :developer] } before { project.team << [user, :developer] }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:key) { create(:key, user: user) } let(:key) { create(:key, user: user) }
let(:project) { create(:project) } let(:project) { create(:project) }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) { create(:project, namespace: user.namespace ) } let!(:project) { create(:project, namespace: user.namespace ) }
let!(:issue) { create(:issue, author: user, assignee: user, project: project) } let!(:issue) { create(:issue, author: user, assignee: user, project: project) }
......
...@@ -2,8 +2,6 @@ require "spec_helper" ...@@ -2,8 +2,6 @@ require "spec_helper"
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) {create(:project, creator_id: user.id, namespace: user.namespace) } let!(:project) {create(:project, creator_id: user.id, namespace: user.namespace) }
let!(:merge_request) { create(:merge_request, :simple, author: user, assignee: user, source_project: project, target_project: project, title: "Test") } let!(:merge_request) { create(:merge_request, :simple, author: user, assignee: user, source_project: project, target_project: project, title: "Test") }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) {disable_observers}
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) { create(:project, namespace: user.namespace ) } let!(:project) { create(:project, namespace: user.namespace ) }
let!(:milestone) { create(:milestone, project: project) } let!(:milestone) { create(:milestone, project: project) }
...@@ -92,9 +89,6 @@ describe API::API, api: true do ...@@ -92,9 +89,6 @@ describe API::API, api: true do
end end
describe "PUT /projects/:id/milestones/:milestone_id to test observer on close" do describe "PUT /projects/:id/milestones/:milestone_id to test observer on close" do
before { enable_observers }
after { disable_observers }
it "should create an activity event when an milestone is closed" do it "should create an activity event when an milestone is closed" do
Event.should_receive(:create) Event.should_receive(:create)
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
let!(:group1) { create(:group) } let!(:group1) { create(:group) }
let!(:group2) { create(:group) } let!(:group2) { create(:group) }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) { create(:project, namespace: user.namespace ) } let!(:project) { create(:project, namespace: user.namespace ) }
let!(:issue) { create(:issue, project: project, author: user) } let!(:issue) { create(:issue, project: project, author: user) }
...@@ -128,14 +125,10 @@ describe API::API, api: true do ...@@ -128,14 +125,10 @@ describe API::API, api: true do
end end
describe "POST /projects/:id/noteable/:noteable_id/notes to test observer on create" do describe "POST /projects/:id/noteable/:noteable_id/notes to test observer on create" do
before { enable_observers }
after { disable_observers }
it "should create an activity event when an issue note is created" do it "should create an activity event when an issue note is created" do
Event.should_receive(:create) Event.should_receive(:create)
post api("/projects/#{project.id}/issues/#{issue.id}/notes", user), body: 'hi!' post api("/projects/#{project.id}/issues/#{issue.id}/notes", user), body: 'hi!'
end end
end end
end end
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, 'ProjectHooks', api: true do describe API::API, 'ProjectHooks', api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) { disable_observers }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user3) { create(:user) } let(:user3) { create(:user) }
let!(:project) { create(:project, creator_id: user.id, namespace: user.namespace) } let!(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) { disable_observers }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
let(:user3) { create(:user) } let(:user3) { create(:user) }
......
...@@ -2,9 +2,6 @@ require 'spec_helper' ...@@ -2,9 +2,6 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) { disable_observers }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
let(:user3) { create(:user) } let(:user3) { create(:user) }
......
...@@ -3,9 +3,6 @@ require 'mime/types' ...@@ -3,9 +3,6 @@ require 'mime/types'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { enable_observers }
after(:each) {disable_observers}
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
let!(:project) { create(:project, creator_id: user.id) } let!(:project) { create(:project, creator_id: user.id) }
......
...@@ -2,9 +2,6 @@ require "spec_helper" ...@@ -2,9 +2,6 @@ require "spec_helper"
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) {create(:project, creator_id: user.id, namespace: user.namespace) } let(:project) {create(:project, creator_id: user.id, namespace: user.namespace) }
......
require 'spec_helper' require 'spec_helper'
describe Issues::BulkUpdateService do describe Issues::BulkUpdateService do
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
let(:issue) { let(:issue) {
create(:issue, project: @project) create(:issue, project: @project)
} }
......
require 'spec_helper' require 'spec_helper'
describe Projects::CreateService do describe Projects::CreateService do
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
describe :create_by_user do describe :create_by_user do
before do before do
@user = create :user @user = create :user
......
require 'spec_helper' require 'spec_helper'
describe Projects::ImageService do describe Projects::ImageService do
before(:each) { enable_observers }
after(:each) { disable_observers }
describe 'Image service' do describe 'Image service' do
before do before do
@user = create :user @user = create :user
......
require 'spec_helper' require 'spec_helper'
describe Projects::TransferService do describe Projects::TransferService do
before(:each) { enable_observers }
after(:each) {disable_observers}
let(:user) { create(:user) } let(:user) { create(:user) }
let(:group) { create(:group) } let(:group) { create(:group) }
let(:group2) { create(:group) } let(:group2) { create(:group) }
......
require 'spec_helper' require 'spec_helper'
describe Projects::UpdateService do describe Projects::UpdateService do
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
describe :update_by_user do describe :update_by_user do
before do before do
@user = create :user @user = create :user
......
...@@ -43,7 +43,7 @@ RSpec.configure do |config| ...@@ -43,7 +43,7 @@ RSpec.configure do |config|
# instead of true. # instead of true.
config.before(:suite) do config.before(:suite) do
TestEnv.init(observers: false, init_repos: true, repos: false) TestEnv.init(init_repos: true, repos: false)
end end
config.before(:each) do config.before(:each) do
TestEnv.setup_stubs TestEnv.setup_stubs
......
...@@ -3,9 +3,7 @@ module LoginHelpers ...@@ -3,9 +3,7 @@ module LoginHelpers
# #
# role - User role (e.g., :admin, :user) # role - User role (e.g., :admin, :user)
def login_as(role) def login_as(role)
ActiveRecord::Base.observers.enable(:user_observer) do
@user = create(role) @user = create(role)
end
login_with(@user) login_with(@user)
end end
......
...@@ -17,14 +17,6 @@ module TestEnv ...@@ -17,14 +17,6 @@ module TestEnv
def init(opts = {}) def init(opts = {})
RSpec::Mocks::setup(self) RSpec::Mocks::setup(self)
# Disable observers to improve test speed
#
# You can enable it in whole test case where needed by next string:
#
# before(:each) { enable_observers }
#
disable_observers if opts[:observers] == false
# Disable mailer for spinach tests # Disable mailer for spinach tests
disable_mailer if opts[:mailer] == false disable_mailer if opts[:mailer] == false
setup_stubs setup_stubs
...@@ -33,14 +25,6 @@ module TestEnv ...@@ -33,14 +25,6 @@ module TestEnv
setup_test_repos(opts) if opts[:repos] == true setup_test_repos(opts) if opts[:repos] == true
end end
def enable_observers
ActiveRecord::Base.observers.enable(:all)
end
def disable_observers
ActiveRecord::Base.observers.disable(:all)
end
def disable_mailer def disable_mailer
NotificationService.any_instance.stub(mailer: double.as_null_object) NotificationService.any_instance.stub(mailer: double.as_null_object)
end end
...@@ -89,10 +73,6 @@ module TestEnv ...@@ -89,10 +73,6 @@ module TestEnv
Repository.any_instance.stub( Repository.any_instance.stub(
size: 12.45 size: 12.45
) )
BaseObserver.any_instance.stub(
current_user: double("current_user", id: 1)
)
end end
def clear_repo_dir(namespace, name) def clear_repo_dir(namespace, name)
......
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