Commit 88964132edaec721e256449579c032ca731781bc
1 parent
97ca4f5d
Exists in
master
and in
4 other branches
Extract observation of User to a UserObserver
Showing
5 changed files
with
37 additions
and
10 deletions
Show diff stats
app/models/mailer_observer.rb
1 | class MailerObserver < ActiveRecord::Observer | 1 | class MailerObserver < ActiveRecord::Observer |
2 | - observe :issue, :user, :note, :merge_request | 2 | + observe :issue, :note, :merge_request |
3 | cattr_accessor :current_user | 3 | cattr_accessor :current_user |
4 | 4 | ||
5 | def after_create(model) | 5 | def after_create(model) |
6 | - new_user(model) if model.kind_of?(User) | ||
7 | new_note(model) if model.kind_of?(Note) | 6 | new_note(model) if model.kind_of?(Note) |
8 | new_merge_request(model) if model.kind_of?(MergeRequest) | 7 | new_merge_request(model) if model.kind_of?(MergeRequest) |
9 | end | 8 | end |
@@ -14,10 +13,6 @@ class MailerObserver < ActiveRecord::Observer | @@ -14,10 +13,6 @@ class MailerObserver < ActiveRecord::Observer | ||
14 | 13 | ||
15 | protected | 14 | protected |
16 | 15 | ||
17 | - def new_user(user) | ||
18 | - Notify.new_user_email(user.id, user.password).deliver | ||
19 | - end | ||
20 | - | ||
21 | def new_note(note) | 16 | def new_note(note) |
22 | if note.notify | 17 | if note.notify |
23 | # Notify whole team except author of note | 18 | # Notify whole team except author of note |
config/application.rb
@@ -23,7 +23,7 @@ module Gitlab | @@ -23,7 +23,7 @@ module Gitlab | ||
23 | # config.plugins = [ :exception_notification, :ssl_requirement, :all ] | 23 | # config.plugins = [ :exception_notification, :ssl_requirement, :all ] |
24 | 24 | ||
25 | # Activate observers that should always be running. | 25 | # Activate observers that should always be running. |
26 | - config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer, :issue_observer | 26 | + config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer, :issue_observer, :user_observer |
27 | 27 | ||
28 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. | 28 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. |
29 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. | 29 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. |
@@ -0,0 +1,26 @@ | @@ -0,0 +1,26 @@ | ||
1 | +require 'spec_helper' | ||
2 | + | ||
3 | +describe UserObserver do | ||
4 | + subject { UserObserver.instance } | ||
5 | + | ||
6 | + it 'calls #after_create when new users are created' do | ||
7 | + new_user = Factory.new(:user) | ||
8 | + subject.should_receive(:after_create).with(new_user) | ||
9 | + | ||
10 | + User.observers.enable :user_observer do | ||
11 | + new_user.save | ||
12 | + end | ||
13 | + end | ||
14 | + | ||
15 | + context 'when a new user is created' do | ||
16 | + let(:user) { double(:user, id: 42, password: 'P@ssword!') } | ||
17 | + let(:notification) { double :notification } | ||
18 | + | ||
19 | + it 'sends an email' do | ||
20 | + notification.should_receive(:deliver) | ||
21 | + Notify.should_receive(:new_user_email).with(user.id, user.password).and_return(notification) | ||
22 | + | ||
23 | + subject.after_create(user) | ||
24 | + end | ||
25 | + end | ||
26 | +end |
spec/requests/admin/admin_users_spec.rb
@@ -40,14 +40,15 @@ describe "Admin::Users" do | @@ -40,14 +40,15 @@ describe "Admin::Users" do | ||
40 | end | 40 | end |
41 | 41 | ||
42 | it "should call send mail" do | 42 | it "should call send mail" do |
43 | - User.observers.enable :mailer_observer do | ||
44 | - Notify.should_receive(:new_user_email).and_return(stub(:deliver => true)) | 43 | + Notify.should_receive(:new_user_email).and_return(stub(:deliver => true)) |
44 | + | ||
45 | + User.observers.enable :user_observer do | ||
45 | click_button "Save" | 46 | click_button "Save" |
46 | end | 47 | end |
47 | end | 48 | end |
48 | 49 | ||
49 | it "should send valid email to user with email & password" do | 50 | it "should send valid email to user with email & password" do |
50 | - User.observers.enable :mailer_observer do | 51 | + User.observers.enable :user_observer do |
51 | with_resque do | 52 | with_resque do |
52 | click_button "Save" | 53 | click_button "Save" |
53 | end | 54 | end |