From 88964132edaec721e256449579c032ca731781bc Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Wed, 20 Jun 2012 12:29:10 -0400 Subject: [PATCH] Extract observation of User to a UserObserver --- app/models/mailer_observer.rb | 7 +------ app/models/user_observer.rb | 5 +++++ config/application.rb | 2 +- spec/models/user_observer_spec.rb | 26 +++++++++++++++++++++++++ spec/requests/admin/admin_users_spec.rb | 7 ++++--- 5 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 app/models/user_observer.rb create mode 100644 spec/models/user_observer_spec.rb diff --git a/app/models/mailer_observer.rb b/app/models/mailer_observer.rb index 80ce8b365e2..b562d05a428 100644 --- a/app/models/mailer_observer.rb +++ b/app/models/mailer_observer.rb @@ -1,9 +1,8 @@ class MailerObserver < ActiveRecord::Observer - observe :issue, :user, :note, :merge_request + observe :issue, :note, :merge_request cattr_accessor :current_user def after_create(model) - new_user(model) if model.kind_of?(User) new_note(model) if model.kind_of?(Note) new_merge_request(model) if model.kind_of?(MergeRequest) end @@ -14,10 +13,6 @@ class MailerObserver < ActiveRecord::Observer protected - def new_user(user) - Notify.new_user_email(user.id, user.password).deliver - end - def new_note(note) if note.notify # Notify whole team except author of note diff --git a/app/models/user_observer.rb b/app/models/user_observer.rb new file mode 100644 index 00000000000..d12bcc99c83 --- /dev/null +++ b/app/models/user_observer.rb @@ -0,0 +1,5 @@ +class UserObserver < ActiveRecord::Observer + def after_create(user) + Notify.new_user_email(user.id, user.password).deliver + end +end diff --git a/config/application.rb b/config/application.rb index 4531b5ead6c..6242b655e49 100644 --- a/config/application.rb +++ b/config/application.rb @@ -23,7 +23,7 @@ module Gitlab # 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 + 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. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. diff --git a/spec/models/user_observer_spec.rb b/spec/models/user_observer_spec.rb new file mode 100644 index 00000000000..23dac98bb74 --- /dev/null +++ b/spec/models/user_observer_spec.rb @@ -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 diff --git a/spec/requests/admin/admin_users_spec.rb b/spec/requests/admin/admin_users_spec.rb index d9c3472d7e3..ba6831e3d8b 100644 --- a/spec/requests/admin/admin_users_spec.rb +++ b/spec/requests/admin/admin_users_spec.rb @@ -40,14 +40,15 @@ describe "Admin::Users" do end 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" end end 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 click_button "Save" end