Extract observation of User to a UserObserver
This commit is contained in:
parent
97ca4f5dda
commit
88964132ed
5 changed files with 37 additions and 10 deletions
|
@ -1,9 +1,8 @@
|
||||||
class MailerObserver < ActiveRecord::Observer
|
class MailerObserver < ActiveRecord::Observer
|
||||||
observe :issue, :user, :note, :merge_request
|
observe :issue, :note, :merge_request
|
||||||
cattr_accessor :current_user
|
cattr_accessor :current_user
|
||||||
|
|
||||||
def after_create(model)
|
def after_create(model)
|
||||||
new_user(model) if model.kind_of?(User)
|
|
||||||
new_note(model) if model.kind_of?(Note)
|
new_note(model) if model.kind_of?(Note)
|
||||||
new_merge_request(model) if model.kind_of?(MergeRequest)
|
new_merge_request(model) if model.kind_of?(MergeRequest)
|
||||||
end
|
end
|
||||||
|
@ -14,10 +13,6 @@ class MailerObserver < ActiveRecord::Observer
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def new_user(user)
|
|
||||||
Notify.new_user_email(user.id, user.password).deliver
|
|
||||||
end
|
|
||||||
|
|
||||||
def new_note(note)
|
def new_note(note)
|
||||||
if note.notify
|
if note.notify
|
||||||
# Notify whole team except author of note
|
# Notify whole team except author of note
|
||||||
|
|
5
app/models/user_observer.rb
Normal file
5
app/models/user_observer.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class UserObserver < ActiveRecord::Observer
|
||||||
|
def after_create(user)
|
||||||
|
Notify.new_user_email(user.id, user.password).deliver
|
||||||
|
end
|
||||||
|
end
|
|
@ -23,7 +23,7 @@ module Gitlab
|
||||||
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
||||||
|
|
||||||
# Activate observers that should always be running.
|
# Activate observers that should always be running.
|
||||||
config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer, :issue_observer
|
config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer, :issue_observer, :user_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.
|
||||||
|
|
26
spec/models/user_observer_spec.rb
Normal file
26
spec/models/user_observer_spec.rb
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe UserObserver do
|
||||||
|
subject { UserObserver.instance }
|
||||||
|
|
||||||
|
it 'calls #after_create when new users are created' do
|
||||||
|
new_user = Factory.new(:user)
|
||||||
|
subject.should_receive(:after_create).with(new_user)
|
||||||
|
|
||||||
|
User.observers.enable :user_observer do
|
||||||
|
new_user.save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when a new user is created' do
|
||||||
|
let(:user) { double(:user, id: 42, password: 'P@ssword!') }
|
||||||
|
let(:notification) { double :notification }
|
||||||
|
|
||||||
|
it 'sends an email' do
|
||||||
|
notification.should_receive(:deliver)
|
||||||
|
Notify.should_receive(:new_user_email).with(user.id, user.password).and_return(notification)
|
||||||
|
|
||||||
|
subject.after_create(user)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -40,14 +40,15 @@ describe "Admin::Users" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should call send mail" do
|
it "should call send mail" do
|
||||||
User.observers.enable :mailer_observer do
|
Notify.should_receive(:new_user_email).and_return(stub(:deliver => true))
|
||||||
Notify.should_receive(:new_user_email).and_return(stub(:deliver => true))
|
|
||||||
|
User.observers.enable :user_observer do
|
||||||
click_button "Save"
|
click_button "Save"
|
||||||
end
|
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 :mailer_observer do
|
User.observers.enable :user_observer do
|
||||||
with_resque do
|
with_resque do
|
||||||
click_button "Save"
|
click_button "Save"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue