Commit 2bb61b50eb08513d1224f0140b7a2ec633d33da7
Committed by
Rafael Manzo
1 parent
a801d1d1
Exists in
colab
and in
4 other branches
Projects helper
Projects helper and destroy link is not shown anymore if the user is not the owner
Showing
4 changed files
with
62 additions
and
9 deletions
Show diff stats
app/views/projects/show.html.erb
... | ... | @@ -9,7 +9,9 @@ |
9 | 9 | </p> |
10 | 10 | |
11 | 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 | 15 | </p> |
14 | 16 | |
15 | 17 | <%= link_to 'Back', projects_path %> | ... | ... |
spec/controllers/projects_controller_spec.rb
... | ... | @@ -11,7 +11,7 @@ describe ProjectsController do |
11 | 11 | it { should respond_with(:success) } |
12 | 12 | it { should render_template(:new) } |
13 | 13 | end |
14 | - | |
14 | + | |
15 | 15 | describe 'create' do |
16 | 16 | before do |
17 | 17 | sign_in FactoryGirl.create(:user) |
... | ... | @@ -27,7 +27,7 @@ describe ProjectsController do |
27 | 27 | end |
28 | 28 | |
29 | 29 | context 'rendering the show' do |
30 | - before :each do | |
30 | + before :each do | |
31 | 31 | Project.expects(:exists?).returns(true) |
32 | 32 | |
33 | 33 | post :create, :project => @subject_params |
... | ... | @@ -42,7 +42,7 @@ describe ProjectsController do |
42 | 42 | before :each do |
43 | 43 | post :create, :project => @subject_params |
44 | 44 | end |
45 | - | |
45 | + | |
46 | 46 | it { should respond_with(:redirect) } |
47 | 47 | end |
48 | 48 | end |
... | ... | @@ -51,7 +51,7 @@ describe ProjectsController do |
51 | 51 | before :each do |
52 | 52 | @subject = FactoryGirl.build(:project) |
53 | 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 | 55 | Project.expects(:new).at_least_once.with(@subject_params).returns(@subject) |
56 | 56 | Project.any_instance.expects(:save).returns(false) |
57 | 57 | |
... | ... | @@ -84,7 +84,7 @@ describe ProjectsController do |
84 | 84 | @ownerships = [] |
85 | 85 | @ownerships.expects(:find_by_project_id).with(@subject.id).returns(@ownership) |
86 | 86 | User.any_instance.expects(:project_ownerships).returns(@ownerships) |
87 | - | |
87 | + | |
88 | 88 | Project.expects(:find).with(@subject.id.to_s).returns(@subject) |
89 | 89 | delete :destroy, :id => @subject.id |
90 | 90 | end |
... | ... | @@ -136,7 +136,7 @@ describe ProjectsController do |
136 | 136 | end |
137 | 137 | |
138 | 138 | context 'rendering the show' do |
139 | - before :each do | |
139 | + before :each do | |
140 | 140 | Project.expects(:exists?).returns(true) |
141 | 141 | |
142 | 142 | post :update, :id => @subject.id, :project => @subject_params |
... | ... | @@ -151,7 +151,7 @@ describe ProjectsController do |
151 | 151 | before :each do |
152 | 152 | post :update, :id => @subject.id, :project => @subject_params |
153 | 153 | end |
154 | - | |
154 | + | |
155 | 155 | it { should respond_with(:redirect) } |
156 | 156 | end |
157 | 157 | end |
... | ... | @@ -160,7 +160,7 @@ describe ProjectsController do |
160 | 160 | before :each do |
161 | 161 | @subject = FactoryGirl.build(:project) |
162 | 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 | 164 | Project.expects(:find).with(@subject.id.to_s).returns(@subject) |
165 | 165 | Project.any_instance.expects(:update).with(@subject_params).returns(false) |
166 | 166 | ... | ... |
... | ... | @@ -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 | 47 | \ No newline at end of file | ... | ... |