Commit dc508e26687f8aafe5d78317b3e00df790ddccf8
1 parent
2be92c1e
Exists in
master
and in
1 other branch
Avoid delete himself
* Extract in UserDestroy the user destroy system with callback * Avoid delete himself * little refactor on rspec to be more rspec compiliant
Showing
7 changed files
with
76 additions
and
20 deletions
Show diff stats
app/controllers/users_controller.rb
@@ -40,9 +40,12 @@ class UsersController < ApplicationController | @@ -40,9 +40,12 @@ class UsersController < ApplicationController | ||
40 | end | 40 | end |
41 | 41 | ||
42 | def destroy | 42 | def destroy |
43 | - user.destroy | ||
44 | - | ||
45 | - flash[:success] = "That's sad. #{user.name} is no longer part of your team." | 43 | + if user == current_user |
44 | + flash[:error] = I18n.t('controllers.users.flash.destroy.error') | ||
45 | + else | ||
46 | + UserDestroy.new(user).destroy | ||
47 | + flash[:success] = "That's sad. #{user.name} is no longer part of your team." | ||
48 | + end | ||
46 | redirect_to users_path | 49 | redirect_to users_path |
47 | end | 50 | end |
48 | 51 |
app/models/user.rb
@@ -13,7 +13,6 @@ class User | @@ -13,7 +13,6 @@ class User | ||
13 | field :per_page, :type => Fixnum, :default => PER_PAGE | 13 | field :per_page, :type => Fixnum, :default => PER_PAGE |
14 | field :time_zone, :default => "UTC" | 14 | field :time_zone, :default => "UTC" |
15 | 15 | ||
16 | - after_destroy :destroy_watchers | ||
17 | before_save :ensure_authentication_token | 16 | before_save :ensure_authentication_token |
18 | 17 | ||
19 | validates_presence_of :name | 18 | validates_presence_of :name |
@@ -57,10 +56,5 @@ class User | @@ -57,10 +56,5 @@ class User | ||
57 | self[:github_login] = login | 56 | self[:github_login] = login |
58 | end | 57 | end |
59 | 58 | ||
60 | - protected | ||
61 | - | ||
62 | - def destroy_watchers | ||
63 | - watchers.each(&:destroy) | ||
64 | - end | ||
65 | end | 59 | end |
66 | 60 |
config/locales/en.yml
@@ -14,3 +14,11 @@ en: | @@ -14,3 +14,11 @@ en: | ||
14 | n_errs_have: | 14 | n_errs_have: |
15 | one: "%{count} err has" | 15 | one: "%{count} err has" |
16 | other: "%{count} errs have" | 16 | other: "%{count} errs have" |
17 | + | ||
18 | + | ||
19 | + controllers: | ||
20 | + users: | ||
21 | + flash: | ||
22 | + destroy: | ||
23 | + success: "That's sad. %{name} is no longer part of your team." | ||
24 | + error: "You can't delete yourself" |
spec/controllers/users_controller_spec.rb
@@ -212,19 +212,30 @@ describe UsersController do | @@ -212,19 +212,30 @@ describe UsersController do | ||
212 | 212 | ||
213 | context "DELETE /users/:id" do | 213 | context "DELETE /users/:id" do |
214 | 214 | ||
215 | - it "destroys the user" do | ||
216 | - delete :destroy, :id => user.id | ||
217 | - User.where(:id => user.id).first.should be_nil | ||
218 | - end | 215 | + context "with a destroy success" do |
216 | + let(:user_destroy) { mock(:destroy => true) } | ||
217 | + | ||
218 | + before { | ||
219 | + UserDestroy.should_receive(:new).with(user).and_return(user_destroy) | ||
220 | + delete :destroy, :id => user.id | ||
221 | + } | ||
219 | 222 | ||
220 | - it "redirects to the users index page" do | ||
221 | - delete :destroy, :id => user.id | ||
222 | - response.should redirect_to(users_path) | 223 | + it 'should destroy user' do |
224 | + expect(request.flash[:success]).to eq I18n.t('controllers.users.flash.destroy.success', :name => user.name) | ||
225 | + response.should redirect_to(users_path) | ||
226 | + end | ||
223 | end | 227 | end |
224 | 228 | ||
225 | - it "sets a message to display" do | ||
226 | - delete :destroy, :id => user.id | ||
227 | - request.flash[:success].should include('no longer part of your team') | 229 | + context "with trying destroy himself" do |
230 | + before { | ||
231 | + UserDestroy.should_not_receive(:new) | ||
232 | + delete :destroy, :id => admin.id | ||
233 | + } | ||
234 | + | ||
235 | + it 'should not destroy user' do | ||
236 | + response.should redirect_to(users_path) | ||
237 | + expect(request.flash[:error]).to eq I18n.t('controllers.users.flash.destroy.error') | ||
238 | + end | ||
228 | end | 239 | end |
229 | end | 240 | end |
230 | end | 241 | end |
spec/fabricators/app_fabricator.rb
@@ -4,7 +4,9 @@ Fabricator(:app) do | @@ -4,7 +4,9 @@ Fabricator(:app) do | ||
4 | end | 4 | end |
5 | 5 | ||
6 | Fabricator(:app_with_watcher, :from => :app) do | 6 | Fabricator(:app_with_watcher, :from => :app) do |
7 | - watchers(:count => 1) { |parent, i| Fabricate.build(:watcher, :app => parent) } | 7 | + watchers(:count => 1) { |parent, i| |
8 | + Fabricate.build(:watcher, :app => parent) | ||
9 | + } | ||
8 | end | 10 | end |
9 | 11 | ||
10 | Fabricator(:watcher) do | 12 | Fabricator(:watcher) do |
@@ -0,0 +1,27 @@ | @@ -0,0 +1,27 @@ | ||
1 | +require 'spec_helper' | ||
2 | + | ||
3 | +describe UserDestroy do | ||
4 | + let(:app) { Fabricate( | ||
5 | + :app, | ||
6 | + :watchers => [ | ||
7 | + Fabricate.build(:user_watcher, :user => user) | ||
8 | + ]) | ||
9 | + } | ||
10 | + | ||
11 | + describe "#destroy" do | ||
12 | + let!(:user) { Fabricate(:user) } | ||
13 | + it 'should delete user' do | ||
14 | + expect { | ||
15 | + UserDestroy.new(user).destroy | ||
16 | + }.to change(User, :count) | ||
17 | + end | ||
18 | + | ||
19 | + it 'should delete watcher' do | ||
20 | + expect { | ||
21 | + UserDestroy.new(user).destroy | ||
22 | + }.to change{ | ||
23 | + app.reload.watchers.where(:user_id => user.id).count | ||
24 | + }.from(1).to(0) | ||
25 | + end | ||
26 | + end | ||
27 | +end |