Commit e17ea8256c7f1c049535a81f2b774fec5e1ce954

Authored by Cyril Mougel
1 parent bf8726a2
Exists in master and in 1 other branch production

Fix the watcher destroy controller because not work before

app/controllers/watchers_controller.rb
1 class WatchersController < ApplicationController 1 class WatchersController < ApplicationController
2 respond_to :html 2 respond_to :html
3 3
4 - before_filter :find_watcher, :only => [:destroy] 4 + expose(:app) do
  5 + App.find(params[:app_id])
  6 + end
  7 +
  8 + expose(:watcher) do
  9 + app.watchers.where(:user_id => params[:id]).first
  10 + end
  11 +
5 before_filter :require_watcher_edit_priviledges, :only => [:destroy] 12 before_filter :require_watcher_edit_priviledges, :only => [:destroy]
6 13
7 def destroy 14 def destroy
8 - @app.watchers.delete(@watcher)  
9 - flash[:success] = "That's sad. #{@watcher.label} is no longer watcher." 15 + app.watchers.delete(watcher)
  16 + flash[:success] = "That's sad. #{watcher.label} is no longer watcher."
10 redirect_to root_path 17 redirect_to root_path
11 end 18 end
12 19
13 - protected  
14 -  
15 - def find_watcher  
16 - @app = App.find(params[:app_id])  
17 - @watcher = @app.watchers.find(params[:id])  
18 - end 20 + private
19 21
20 - def require_watcher_edit_priviledges  
21 - can_edit = current_user == @watcher.user || current_user.admin?  
22 - redirect_to(root_path) unless can_edit  
23 - end 22 + def require_watcher_edit_priviledges
  23 + redirect_to(root_path) unless current_user == watcher.user || current_user.admin?
  24 + end
24 25
25 end 26 end
26 27
spec/controllers/watchers_controller_spec.rb
1 require 'spec_helper' 1 require 'spec_helper'
2 2
3 describe WatchersController do 3 describe WatchersController do
4 - let(:app) { Fabricate(:app_with_watcher) } 4 + let(:app) do
  5 + a = Fabricate(:app)
  6 + Fabricate(:user_watcher, :app => a)
  7 + a
  8 + end
5 9
6 describe "DELETE /apps/:app_id/watchers/:id/destroy" do 10 describe "DELETE /apps/:app_id/watchers/:id/destroy" do
7 - render_views  
8 -  
9 - before(:each) do  
10 - sign_in Fabricate(:admin)  
11 - end  
12 -  
13 - context "successful watcher deletion" do  
14 - let(:problem) { Fabricate(:problem_with_comments) }  
15 - let(:watcher) { app.watchers.first }  
16 11
  12 + context "with admin user" do
17 before(:each) do 13 before(:each) do
18 - delete :destroy, :app_id => app.id, :id => watcher.id.to_s  
19 - problem.reload 14 + sign_in Fabricate(:admin)
20 end 15 end
21 16
22 - it "should delete the watcher" do  
23 - app.watchers.detect{|w| w.id.to_s == watcher.id }.should == nil  
24 - end 17 + context "successful watcher deletion" do
  18 + let(:problem) { Fabricate(:problem_with_comments) }
  19 + let(:watcher) { app.watchers.first }
  20 +
  21 + before(:each) do
  22 + delete :destroy, :app_id => app.id, :id => watcher.user.id.to_s
  23 + problem.reload
  24 + end
  25 +
  26 + it "should delete the watcher" do
  27 + app.watchers.detect{|w| w.id.to_s == watcher.id }.should == nil
  28 + end
25 29
26 - it "should redirect to index page" do  
27 - response.should redirect_to(root_path) 30 + it "should redirect to index page" do
  31 + response.should redirect_to(root_path)
  32 + end
28 end 33 end
29 end 34 end
30 end 35 end