Skip to content

Commit 3e360a2

Browse files
committed
Allow project originators to delete comments on their projects
1 parent 2e5eba3 commit 3e360a2

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

app/controllers/comments_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def destroy
3838
@comment.destroy
3939

4040
respond_to do |format|
41-
format.html { redirect_to comments_path, notice: 'Comment was successfully deleted.' }
41+
format.html { redirect_back fallback_location: root_path, notice: 'Comment was successfully deleted.' }
4242
end
4343
end
4444

app/models/ability.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def initialize(user)
3535
can :read, Update, author_id: user.id
3636
can :manage, Project, originator_id: user.id
3737
can %i[create update], Comment, commenter_id: user.id
38+
can :destroy, Comment do |comment|
39+
comment.project.originator_id == user.id
40+
end
3841
can %i[update add_keyword delete_keyword advance recess add_episode delete_episode],
3942
Project do |project|
4043
project.users.include? user

app/views/comments/_comment.html.haml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
|
1515
%a{ 'href' => 'javascript:void(0)', 'data-target' => "#editComment#{dom_id(comment)}", 'data-toggle' => 'modal', type: 'button' }
1616
Edit
17+
- if can? :destroy, comment
18+
|
19+
= link_to 'Delete', comment_path(comment), method: :delete, data: { confirm: 'Are you sure you want to delete this comment?' }
1720
%p
1821
:markdown
1922
#{ enrich_markdown(markdown: comment.text) }

spec/features/comment_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,34 @@
7575
expect(page).to have_text comment_text
7676
end
7777
end
78+
79+
scenario 'project originator can delete comments on their project', :js do
80+
other_user = create(:user)
81+
comment = create(:comment, commenter: other_user, commentable: project)
82+
83+
visit project_path(nil, project)
84+
85+
within("li#comment_#{comment.id}") do
86+
click_on 'Delete'
87+
end
88+
89+
page.driver.browser.switch_to.alert.accept
90+
91+
expect(page).to have_current_path(project_path(nil, project), ignore_query: true)
92+
expect(page).to have_text 'Comment was successfully deleted'
93+
expect(page).to have_no_css("li#comment_#{comment.id}")
94+
expect(Comment.exists?(comment.id)).to be false
95+
end
96+
97+
scenario 'non-originator cannot delete comments on others projects', :js do
98+
other_user = create(:user)
99+
other_project = create(:idea, originator: other_user)
100+
comment = create(:comment, commenter: other_user, commentable: other_project)
101+
102+
visit project_path(nil, other_project)
103+
104+
within("li#comment_#{comment.id}") do
105+
expect(page).to have_no_link 'Delete'
106+
end
107+
end
78108
end

spec/models/ability_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,27 @@
5555
it { is_expected.to be_able_to(:manage, Announcement.new) }
5656
it { is_expected.to be_able_to(:manage, Faq.new) }
5757
end
58+
59+
context 'when user is project originator' do
60+
let(:user) { create(:user) }
61+
let(:other_user) { create(:user) }
62+
let(:own_project) { create(:project, originator: user) }
63+
let(:foreign_project) { create(:project, originator: other_user) }
64+
65+
context 'when on own project' do
66+
let(:own_comment_on_own_project) { create(:comment, commenter: user, commentable: own_project) }
67+
let(:comment_on_own_project) { create(:comment, commenter: other_user, commentable: own_project) }
68+
69+
it { is_expected.to be_able_to(:destroy, own_comment_on_own_project) }
70+
it { is_expected.to be_able_to(:destroy, comment_on_own_project) }
71+
end
72+
73+
context 'when on foreign project' do
74+
let(:own_comment_on_foreign_project) { create(:comment, commenter: user, commentable: foreign_project) }
75+
let(:comment_on_foreign_project) { create(:comment, commenter: other_user, commentable: foreign_project) }
76+
77+
it { is_expected.not_to be_able_to(:destroy, own_comment_on_foreign_project) }
78+
it { is_expected.not_to be_able_to(:destroy, comment_on_foreign_project) }
79+
end
80+
end
5881
end

0 commit comments

Comments
 (0)