diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index fbe4991..c8e31de 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -40,9 +40,12 @@ class UsersController < ApplicationController end def destroy - user.destroy - - flash[:success] = "That's sad. #{user.name} is no longer part of your team." + if user == current_user + flash[:error] = I18n.t('controllers.users.flash.destroy.error') + else + UserDestroy.new(user).destroy + flash[:success] = "That's sad. #{user.name} is no longer part of your team." + end redirect_to users_path end diff --git a/app/interactors/user_destroy.rb b/app/interactors/user_destroy.rb new file mode 100644 index 0000000..8453962 --- /dev/null +++ b/app/interactors/user_destroy.rb @@ -0,0 +1,11 @@ +class UserDestroy + def initialize(user) + @user = user + end + + def destroy + @user.destroy + @user.watchers.each(&:destroy) + end + +end diff --git a/app/models/user.rb b/app/models/user.rb index 3f8c5dc..10cc11b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,7 +13,6 @@ class User field :per_page, :type => Fixnum, :default => PER_PAGE field :time_zone, :default => "UTC" - after_destroy :destroy_watchers before_save :ensure_authentication_token validates_presence_of :name @@ -57,10 +56,5 @@ class User self[:github_login] = login end - protected - - def destroy_watchers - watchers.each(&:destroy) - end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 7378a40..12d010f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -14,3 +14,11 @@ en: n_errs_have: one: "%{count} err has" other: "%{count} errs have" + + + controllers: + users: + flash: + destroy: + success: "That's sad. %{name} is no longer part of your team." + error: "You can't delete yourself" diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 7c4f251..5d4ebdc 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -212,19 +212,30 @@ describe UsersController do context "DELETE /users/:id" do - it "destroys the user" do - delete :destroy, :id => user.id - User.where(:id => user.id).first.should be_nil - end + context "with a destroy success" do + let(:user_destroy) { mock(:destroy => true) } + + before { + UserDestroy.should_receive(:new).with(user).and_return(user_destroy) + delete :destroy, :id => user.id + } - it "redirects to the users index page" do - delete :destroy, :id => user.id - response.should redirect_to(users_path) + it 'should destroy user' do + expect(request.flash[:success]).to eq I18n.t('controllers.users.flash.destroy.success', :name => user.name) + response.should redirect_to(users_path) + end end - it "sets a message to display" do - delete :destroy, :id => user.id - request.flash[:success].should include('no longer part of your team') + context "with trying destroy himself" do + before { + UserDestroy.should_not_receive(:new) + delete :destroy, :id => admin.id + } + + it 'should not destroy user' do + response.should redirect_to(users_path) + expect(request.flash[:error]).to eq I18n.t('controllers.users.flash.destroy.error') + end end end end diff --git a/spec/fabricators/app_fabricator.rb b/spec/fabricators/app_fabricator.rb index 2fa26dd..59acba8 100644 --- a/spec/fabricators/app_fabricator.rb +++ b/spec/fabricators/app_fabricator.rb @@ -4,7 +4,9 @@ Fabricator(:app) do end Fabricator(:app_with_watcher, :from => :app) do - watchers(:count => 1) { |parent, i| Fabricate.build(:watcher, :app => parent) } + watchers(:count => 1) { |parent, i| + Fabricate.build(:watcher, :app => parent) + } end Fabricator(:watcher) do diff --git a/spec/interactors/user_destroy_spec.rb b/spec/interactors/user_destroy_spec.rb new file mode 100644 index 0000000..3b10c8c --- /dev/null +++ b/spec/interactors/user_destroy_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe UserDestroy do + let(:app) { Fabricate( + :app, + :watchers => [ + Fabricate.build(:user_watcher, :user => user) + ]) + } + + describe "#destroy" do + let!(:user) { Fabricate(:user) } + it 'should delete user' do + expect { + UserDestroy.new(user).destroy + }.to change(User, :count) + end + + it 'should delete watcher' do + expect { + UserDestroy.new(user).destroy + }.to change{ + app.reload.watchers.where(:user_id => user.id).count + }.from(1).to(0) + end + end +end -- libgit2 0.21.2