diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index dd13d2f..63c8d43 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -1,26 +1,27 @@ class WatchersController < ApplicationController respond_to :html - before_filter :find_watcher, :only => [:destroy] + expose(:app) do + App.find(params[:app_id]) + end + + expose(:watcher) do + app.watchers.where(:user_id => params[:id]).first + end + before_filter :require_watcher_edit_priviledges, :only => [:destroy] def destroy - @app.watchers.delete(@watcher) - flash[:success] = "That's sad. #{@watcher.label} is no longer watcher." + app.watchers.delete(watcher) + flash[:success] = "That's sad. #{watcher.label} is no longer watcher." redirect_to root_path end - protected - - def find_watcher - @app = App.find(params[:app_id]) - @watcher = @app.watchers.find(params[:id]) - end + private - def require_watcher_edit_priviledges - can_edit = current_user == @watcher.user || current_user.admin? - redirect_to(root_path) unless can_edit - end + def require_watcher_edit_priviledges + redirect_to(root_path) unless current_user == watcher.user || current_user.admin? + end end diff --git a/spec/controllers/watchers_controller_spec.rb b/spec/controllers/watchers_controller_spec.rb index 4e34d6a..07f6f0d 100644 --- a/spec/controllers/watchers_controller_spec.rb +++ b/spec/controllers/watchers_controller_spec.rb @@ -1,30 +1,35 @@ require 'spec_helper' describe WatchersController do - let(:app) { Fabricate(:app_with_watcher) } + let(:app) do + a = Fabricate(:app) + Fabricate(:user_watcher, :app => a) + a + end describe "DELETE /apps/:app_id/watchers/:id/destroy" do - render_views - - before(:each) do - sign_in Fabricate(:admin) - end - - context "successful watcher deletion" do - let(:problem) { Fabricate(:problem_with_comments) } - let(:watcher) { app.watchers.first } + context "with admin user" do before(:each) do - delete :destroy, :app_id => app.id, :id => watcher.id.to_s - problem.reload + sign_in Fabricate(:admin) end - it "should delete the watcher" do - app.watchers.detect{|w| w.id.to_s == watcher.id }.should == nil - end + context "successful watcher deletion" do + let(:problem) { Fabricate(:problem_with_comments) } + let(:watcher) { app.watchers.first } + + before(:each) do + delete :destroy, :app_id => app.id, :id => watcher.user.id.to_s + problem.reload + end + + it "should delete the watcher" do + app.watchers.detect{|w| w.id.to_s == watcher.id }.should == nil + end - it "should redirect to index page" do - response.should redirect_to(root_path) + it "should redirect to index page" do + response.should redirect_to(root_path) + end end end end -- libgit2 0.21.2