Skip to content

Commit 8e1c6fa

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

5 files changed

Lines changed: 50 additions & 1 deletion

File tree

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(current_path).to eq(project_path(nil, project))
92+
expect(page).to have_text 'Comment was successfully deleted'
93+
expect(page).not_to have_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).not_to have_link 'Delete'
106+
end
107+
end
78108
end

spec/models/ability_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@
3939
it { is_expected.not_to be_able_to(:update, foreign_comment) }
4040
it { is_expected.not_to be_able_to(:destroy, foreign_comment) }
4141

42+
context 'comment deletion by project originator' do
43+
let(:comment_on_own_project) { create(:comment, commenter: other_user, commentable: own_project) }
44+
let(:comment_on_foreign_project) { create(:comment, commenter: other_user, commentable: foreign_project) }
45+
46+
it 'can delete comments on their own project' do
47+
expect(ability).to be_able_to(:destroy, comment_on_own_project)
48+
end
49+
50+
it 'cannot delete comments on others projects' do
51+
expect(ability).not_to be_able_to(:destroy, comment_on_foreign_project)
52+
end
53+
end
54+
4255
%i[edit update add_keyword delete_keyword advance recess add_episode delete_episode].each do |action|
4356
it { is_expected.to be_able_to(action, collaborated_project) }
4457
end

0 commit comments

Comments
 (0)