Commit 2bb61b50eb08513d1224f0140b7a2ec633d33da7

Authored by João M. M. Silva + Guilherme Rojas V. de Lima
Committed by Rafael Manzo
1 parent a801d1d1

Projects helper

Projects helper and destroy link is not shown anymore if the user is not the owner
app/helpers/projects_helper.rb 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +module ProjectsHelper
  2 + def project_owner? project_id
  3 + user_signed_in? && !current_user.project_ownerships.find_by_project_id(project_id).nil?
  4 + end
  5 +end
0 \ No newline at end of file 6 \ No newline at end of file
app/views/projects/show.html.erb
@@ -9,7 +9,9 @@ @@ -9,7 +9,9 @@
9 </p> 9 </p>
10 10
11 <p> 11 <p>
12 - <%= link_to 'Destroy', project_path(@project.id), method: :delete, data: { confirm: 'Are you sure?' } %> 12 + <% if project_owner? @project.id %>
  13 + <%= link_to 'Destroy', project_path(@project.id), method: :delete, data: { confirm: 'Are you sure?' } %>
  14 + <% end %>
13 </p> 15 </p>
14 16
15 <%= link_to 'Back', projects_path %> 17 <%= link_to 'Back', projects_path %>
spec/controllers/projects_controller_spec.rb
@@ -11,7 +11,7 @@ describe ProjectsController do @@ -11,7 +11,7 @@ describe ProjectsController do
11 it { should respond_with(:success) } 11 it { should respond_with(:success) }
12 it { should render_template(:new) } 12 it { should render_template(:new) }
13 end 13 end
14 - 14 +
15 describe 'create' do 15 describe 'create' do
16 before do 16 before do
17 sign_in FactoryGirl.create(:user) 17 sign_in FactoryGirl.create(:user)
@@ -27,7 +27,7 @@ describe ProjectsController do @@ -27,7 +27,7 @@ describe ProjectsController do
27 end 27 end
28 28
29 context 'rendering the show' do 29 context 'rendering the show' do
30 - before :each do 30 + before :each do
31 Project.expects(:exists?).returns(true) 31 Project.expects(:exists?).returns(true)
32 32
33 post :create, :project => @subject_params 33 post :create, :project => @subject_params
@@ -42,7 +42,7 @@ describe ProjectsController do @@ -42,7 +42,7 @@ describe ProjectsController do
42 before :each do 42 before :each do
43 post :create, :project => @subject_params 43 post :create, :project => @subject_params
44 end 44 end
45 - 45 +
46 it { should respond_with(:redirect) } 46 it { should respond_with(:redirect) }
47 end 47 end
48 end 48 end
@@ -51,7 +51,7 @@ describe ProjectsController do @@ -51,7 +51,7 @@ describe ProjectsController do
51 before :each do 51 before :each do
52 @subject = FactoryGirl.build(:project) 52 @subject = FactoryGirl.build(:project)
53 @subject_params = Hash[FactoryGirl.attributes_for(:project).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers 53 @subject_params = Hash[FactoryGirl.attributes_for(:project).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers
54 - 54 +
55 Project.expects(:new).at_least_once.with(@subject_params).returns(@subject) 55 Project.expects(:new).at_least_once.with(@subject_params).returns(@subject)
56 Project.any_instance.expects(:save).returns(false) 56 Project.any_instance.expects(:save).returns(false)
57 57
@@ -84,7 +84,7 @@ describe ProjectsController do @@ -84,7 +84,7 @@ describe ProjectsController do
84 @ownerships = [] 84 @ownerships = []
85 @ownerships.expects(:find_by_project_id).with(@subject.id).returns(@ownership) 85 @ownerships.expects(:find_by_project_id).with(@subject.id).returns(@ownership)
86 User.any_instance.expects(:project_ownerships).returns(@ownerships) 86 User.any_instance.expects(:project_ownerships).returns(@ownerships)
87 - 87 +
88 Project.expects(:find).with(@subject.id.to_s).returns(@subject) 88 Project.expects(:find).with(@subject.id.to_s).returns(@subject)
89 delete :destroy, :id => @subject.id 89 delete :destroy, :id => @subject.id
90 end 90 end
@@ -136,7 +136,7 @@ describe ProjectsController do @@ -136,7 +136,7 @@ describe ProjectsController do
136 end 136 end
137 137
138 context 'rendering the show' do 138 context 'rendering the show' do
139 - before :each do 139 + before :each do
140 Project.expects(:exists?).returns(true) 140 Project.expects(:exists?).returns(true)
141 141
142 post :update, :id => @subject.id, :project => @subject_params 142 post :update, :id => @subject.id, :project => @subject_params
@@ -151,7 +151,7 @@ describe ProjectsController do @@ -151,7 +151,7 @@ describe ProjectsController do
151 before :each do 151 before :each do
152 post :update, :id => @subject.id, :project => @subject_params 152 post :update, :id => @subject.id, :project => @subject_params
153 end 153 end
154 - 154 +
155 it { should respond_with(:redirect) } 155 it { should respond_with(:redirect) }
156 end 156 end
157 end 157 end
@@ -160,7 +160,7 @@ describe ProjectsController do @@ -160,7 +160,7 @@ describe ProjectsController do
160 before :each do 160 before :each do
161 @subject = FactoryGirl.build(:project) 161 @subject = FactoryGirl.build(:project)
162 @subject_params = Hash[FactoryGirl.attributes_for(:project).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers 162 @subject_params = Hash[FactoryGirl.attributes_for(:project).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers
163 - 163 +
164 Project.expects(:find).with(@subject.id.to_s).returns(@subject) 164 Project.expects(:find).with(@subject.id.to_s).returns(@subject)
165 Project.any_instance.expects(:update).with(@subject_params).returns(false) 165 Project.any_instance.expects(:update).with(@subject_params).returns(false)
166 166
spec/helpers/projects_helper_spec.rb 0 → 100644
@@ -0,0 +1,46 @@ @@ -0,0 +1,46 @@
  1 +require 'spec_helper'
  2 +
  3 +describe ProjectsHelper do
  4 +
  5 + describe 'project_owner?' do
  6 + before :each do
  7 + @subject = FactoryGirl.build(:project)
  8 + end
  9 +
  10 + context 'returns false if not logged in' do
  11 + before :each do
  12 + helper.expects(:user_signed_in?).returns(false)
  13 + end
  14 + it { helper.project_owner?(@subject.id).should be_false }
  15 + end
  16 +
  17 + context 'returns false if is not the owner' do
  18 + before :each do
  19 + helper.expects(:user_signed_in?).returns(true)
  20 + helper.expects(:current_user).returns(FactoryGirl.build(:user))
  21 +
  22 + @ownerships = []
  23 + @ownerships.expects(:find_by_project_id).with(@subject.id).returns(nil)
  24 +
  25 + User.any_instance.expects(:project_ownerships).returns(@ownerships)
  26 + end
  27 +
  28 + it { helper.project_owner?(@subject.id).should be_false }
  29 + end
  30 +
  31 + context 'returns true if user is the project owner' do
  32 + before :each do
  33 + helper.expects(:user_signed_in?).returns(true)
  34 + helper.expects(:current_user).returns(FactoryGirl.build(:user))
  35 +
  36 + @ownership = FactoryGirl.build(:project_ownership)
  37 + @ownerships = []
  38 + @ownerships.expects(:find_by_project_id).with(@subject.id).returns(@ownership)
  39 + User.any_instance.expects(:project_ownerships).returns(@ownerships)
  40 + end
  41 +
  42 + it { helper.project_owner?(@subject.id).should be_true }
  43 + end
  44 + end
  45 +
  46 +end
0 \ No newline at end of file 47 \ No newline at end of file