Remove UsersGroup observer

This commit is contained in:
Dmitriy Zaporozhets 2014-06-17 21:36:40 +03:00
parent b3a90aba41
commit 9e3094b232
5 changed files with 45 additions and 42 deletions

View File

@ -33,6 +33,9 @@ class UsersGroup < ActiveRecord::Base
scope :with_group, ->(group) { where(group_id: group.id) }
scope :with_user, ->(user) { where(user_id: user.id) }
after_create :notify_create
after_update :notify_update
validates :group_access, inclusion: { in: UsersGroup.group_access_roles.values }, presence: true
validates :user_id, presence: true
validates :group_id, presence: true
@ -43,4 +46,18 @@ class UsersGroup < ActiveRecord::Base
def access_field
group_access
end
def notify_create
notification_service.new_group_member(self)
end
def notify_update
if group_access_changed?
notification_service.update_group_member(self)
end
end
def notification_service
NotificationService.new
end
end

View File

@ -1,9 +0,0 @@
class UsersGroupObserver < BaseObserver
def after_create(membership)
notification.new_group_member(membership)
end
def after_update(membership)
notification.update_group_member(membership) if membership.group_access_changed?
end
end

View File

@ -23,7 +23,6 @@ module Gitlab
:project_observer,
:system_hook_observer,
:user_observer,
:users_group_observer,
:users_project_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.

View File

@ -37,4 +37,32 @@ describe UsersGroup do
it { should respond_to(:user_name) }
it { should respond_to(:user_email) }
end
context 'notification' do
describe "#after_create" do
it "should send email to user" do
membership = build(:users_group)
membership.stub(notification_service: double('NotificationService').as_null_object)
membership.should_receive(:notification_service)
membership.save
end
end
describe "#after_update" do
before do
@membership = create :users_group
@membership.stub(notification_service: double('NotificationService').as_null_object)
end
it "should send email to user" do
@membership.should_receive(:notification_service)
@membership.update_attribute(:group_access, UsersGroup::MASTER)
end
it "does not send an email when the access level has not changed" do
@membership.should_not_receive(:notification_service)
@membership.update_attribute(:group_access, UsersGroup::OWNER)
end
end
end
end

View File

@ -1,32 +0,0 @@
require 'spec_helper'
describe UsersGroupObserver do
before(:each) { enable_observers }
after(:each) { disable_observers }
subject { UsersGroupObserver.instance }
before { subject.stub(notification: double('NotificationService').as_null_object) }
describe "#after_create" do
it "should send email to user" do
subject.should_receive(:notification)
create(:users_group)
end
end
describe "#after_update" do
before do
@membership = create :users_group
end
it "should send email to user" do
subject.should_receive(:notification)
@membership.update_attribute(:group_access, UsersGroup::MASTER)
end
it "does not send an email when the access level has not changed" do
subject.should_not_receive(:notification)
@membership.update_attribute(:group_access, UsersGroup::OWNER)
end
end
end