Skip to content

Commit f8f8c58

Browse files
author
Rahoul Baruah
committed
delete comments link now appears on page
1 parent a24de0e commit f8f8c58

File tree

9 files changed

+145
-7
lines changed

9 files changed

+145
-7
lines changed

app/controllers/comments_controller.rb

+36-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ class CommentsController < ApplicationController
22
cache_sweeper :code_and_comment_sweeper
33
skip_before_filter :verify_authenticity_token
44

5+
# index expects to be called in the context of a Code
6+
# so a code_id is passed in
57
def index
68
@code = Code.find_by_slug_name! params[:code_id]
79
respond_to do |wants|
@@ -15,20 +17,53 @@ def index
1517
end
1618
end
1719

20+
# create expects to be called in the context of a Code
21+
# so a code_id is passed in
1822
def create
1923
@code = Code.find params[:code_id]
2024
@comment = @code.build_comment params[:comment]
25+
26+
# validate the comment, then test the captcha to ensure all errors are shown on the form
2127
@comment.valid?
22-
raise ActiveRecord::RecordInvalid.new(@comment) unless validate_recap(params, @comment.errors, :rcc_pub => RECAPTCHA_PUBLIC_KEY, :rcc_priv => RECAPTCHA_PRIVATE_KEY)
28+
raise ActiveRecord::RecordInvalid.new(@comment) unless captcha_is_valid_for @comment, :with => params
2329
@comment.save!
30+
2431
cookies[:comment_name] = @comment.name
2532
cookies[:comment_email] = @comment.email
2633
cookies[:comment_url] = @comment.url
34+
my_comments << @comment.id
2735
flash[:notice] = 'Thanks for your comment'
2836
redirect_to code_by_slug_path(@code.slug_name)
2937

3038
rescue ActiveRecord::RecordInvalid
3139
render :template => 'codes/show'
3240
flash[:error] = 'Sorry, unable to save your comment'
3341
end
42+
43+
# destroy expects to be called with just an id, no code_id necessary
44+
def destroy
45+
@comment = Comment.find params[:id]
46+
if can_delete @comment
47+
@comment.destroy
48+
flash[:notice] = 'Your comment has been deleted'
49+
else
50+
flash[:error] = 'You are not allowed to delete that comment'
51+
end
52+
redirect_to code_by_slug_path(@comment.code_slug_name)
53+
end
54+
55+
def can_delete comment
56+
my_comments.include? comment.id
57+
end
58+
59+
private
60+
def my_comments
61+
session[:my_comments] ||= []
62+
end
63+
64+
def captcha_is_valid_for comment, options
65+
return true if ENV['RAILS_ENV'] == 'test' # captcha is always valid in test mode
66+
return validate_recap(options[:with], comment.errors, :rcc_pub => RECAPTCHA_PUBLIC_KEY, :rcc_priv => RECAPTCHA_PRIVATE_KEY)
67+
end
68+
3469
end

app/helpers/comments_helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ def name_link_for comment
77
end
88
end
99

10+
def delete_link_for comment
11+
link_to "DELETE", comment_path(comment), :method => :delete, :class => 'delete-comment' unless comment.new_record?
12+
end
13+
1014
def opinion_for comment
1115
if comment.works_for_me?
1216
"<span class=\"works\">Working</span>"

app/views/comments/_comment.html.erb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<div class="comment rounded">
2-
<%= gravatar_for comment %>
2+
<%= gravatar_for comment %>
33
<p class="status">
44
<%= link_to h(comment.code.slug_name), code_by_slug_path(comment.code.slug_name), :class => 'gem' %>
55
<%= "(#{comment.version})" unless comment.version.blank? %>
66
is <%= opinion_for comment %><br />
77
for <%= name_link_for comment %> <span class="lighterblue">(<%= comment.platform %>)</span><br />
88
</p>
99
<%= "<blockquote>#{format_comment(comment.body)}</blockquote>" if local_assigns[:show_quote] && !comment.body.blank? %>
10+
<p><%= delete_link_for comment %></p>
1011
</div>

features/adding-a-comment.feature

+12-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ Feature: adding a comment
55
So that I can help the community track which gems work with Ruby 1.9
66

77
Scenario: adding a comment
8-
9-
Given a gem called "rubynuts"
8+
9+
Given an initialised database
10+
And a gem called "rubynuts"
1011

1112
When I visit the page for "rubynuts"
1213
Then I see the comment form
@@ -15,6 +16,14 @@ Feature: adding a comment
1516
And I press "submit comment"
1617
Then I see my comment on the page
1718

18-
Scenario: adding a comment and then editing it
19+
Scenario: adding a comment and then deleting it
20+
21+
GivenScenario: adding a comment
22+
23+
When I visit the page for "rubynuts"
24+
Then I see the delete comment link
25+
26+
When I click the delete comment link
27+
Then I do not see my comment on the page
1928

2029
Scenario: viewing someone else's comment

features/step_definitions/comment_steps.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,23 @@
77
fill_in "Email", :with => '[email protected]'
88
choose :comment_works_for_me_true
99
fill_in "Version", :with => '1.0'
10-
fill_in "Platform", :with => 'Mac OSX'
10+
select 'Mac OSX', :from => 'Platform'
1111
fill_in :comment_body, :with => 'Here is my test comment' # have to request via ID rather than label because of the span around the optional, making it hard to find
1212
end
1313

14+
When /^I click the delete comment link$/ do
15+
click_link 'DELETE'
16+
end
17+
18+
1419
Then /^I see my comment on the page$/ do
1520
response.should include_text('Here is my test comment')
1621
end
1722

23+
Then /^I see the delete comment link$/ do
24+
response.should have_tag('a.delete-comment')
25+
end
1826

27+
Then /^I do not see my comment on the page$/ do
28+
response.should_not include_text('Here is my test comment')
29+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Given /^an initialised database$/ do
2+
Platform.load_defaults
3+
end

public/images/misc/recreate.gif

1.02 KB
Loading

public/stylesheets/styles.css

+10-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ dd {
300300
#new-comment-form label {
301301
font-size: 11px;
302302
line-height: 130%;
303-
304303
}
305304

306305

@@ -314,3 +313,13 @@ dd {
314313
#new-comment-form .text_field {
315314
width: 250px;
316315
}
316+
317+
a.delete-comment {
318+
background: url(/images/misc/recreate.gif) no-repeat;
319+
padding: 2px 4px 4px 18px;
320+
font-size: 8pt;
321+
font-weight: bold;
322+
text-transform: uppercase;
323+
text-decoration: none;
324+
color: #95ABC3;
325+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2+
3+
describe CommentsController do
4+
describe "creating a comment" do
5+
it "should create a new comment" do
6+
@code = mock_model Code, :slug_name => 'thingy'
7+
@comment = mock_model Comment, :name => 'This', :email => 'That', :url => 'The other'
8+
9+
on_posting_to :create, :code_id => '1', :comment => { :some => :fields } do
10+
Code.should_receive(:find).with('1').and_return(@code)
11+
@code.should_receive(:build_comment).with("some" => :fields).and_return(@comment)
12+
@comment.should_receive(:valid?).and_return(true)
13+
controller.should_receive(:captcha_is_valid_for).and_return(true)
14+
@comment.should_receive(:save!).and_return(true)
15+
end
16+
17+
cookies[:comment_name].should == ['This']
18+
cookies[:comment_email].should == ['That']
19+
cookies[:comment_url].should == ['The other']
20+
session[:my_comments].should == [@comment.id]
21+
response.should redirect_to(code_by_slug_path('thingy'))
22+
flash[:notice].should == 'Thanks for your comment'
23+
end
24+
end
25+
26+
describe "deleting a comment" do
27+
it "should allow you to delete if the session marks the comment as one of yours" do
28+
@comment = mock_model Comment
29+
session[:my_comments] = [@comment.id]
30+
31+
controller.can_delete(@comment).should be_true
32+
end
33+
34+
it "should allow you to delete if the session marks the comment as one of yours" do
35+
@comment = mock_model Comment
36+
session[:my_comments] = []
37+
38+
controller.can_delete(@comment).should be_false
39+
end
40+
41+
it "should not allow you to delete another person's comment" do
42+
@comment = mock_model Comment, :code_slug_name => 'thingy'
43+
44+
on_deleting_from :destroy, :id => '1' do
45+
Comment.should_receive(:find).with('1').and_return(@comment)
46+
controller.should_receive(:can_delete).with(@comment).and_return(false)
47+
end
48+
49+
response.should redirect_to(code_by_slug_path('thingy'))
50+
flash[:error].should == 'You are not allowed to delete that comment'
51+
end
52+
53+
it "should allow you to delete your own comments" do
54+
@comment = mock_model Comment, :code_slug_name => 'thingy'
55+
56+
on_deleting_from :destroy, :id => '1' do
57+
Comment.should_receive(:find).with('1').and_return(@comment)
58+
controller.should_receive(:can_delete).with(@comment).and_return(true)
59+
@comment.should_receive(:destroy)
60+
end
61+
62+
response.should redirect_to(code_by_slug_path('thingy'))
63+
flash[:notice].should == 'Your comment has been deleted'
64+
end
65+
end
66+
end

0 commit comments

Comments
 (0)