Skip to content

Commit

Permalink
Fix the watcher destroy controller because not work before
Browse files Browse the repository at this point in the history
  • Loading branch information
shingara committed Sep 17, 2013
1 parent bf8726a commit e17ea82
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
27 changes: 14 additions & 13 deletions 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

39 changes: 22 additions & 17 deletions 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
Expand Down

0 comments on commit e17ea82

Please sign in to comment.