Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

delete comments link now appears on page

  • Loading branch information...
commit f8f8c5873b240614f9c700e70fffa2b049edc54a 1 parent a24de0e
@rahoulb rahoulb authored
View
37 app/controllers/comments_controller.rb
@@ -2,6 +2,8 @@ class CommentsController < ApplicationController
cache_sweeper :code_and_comment_sweeper
skip_before_filter :verify_authenticity_token
+ # index expects to be called in the context of a Code
+ # so a code_id is passed in
def index
@code = Code.find_by_slug_name! params[:code_id]
respond_to do |wants|
@@ -15,15 +17,21 @@ def index
end
end
+ # create expects to be called in the context of a Code
+ # so a code_id is passed in
def create
@code = Code.find params[:code_id]
@comment = @code.build_comment params[:comment]
+
+ # validate the comment, then test the captcha to ensure all errors are shown on the form
@comment.valid?
- raise ActiveRecord::RecordInvalid.new(@comment) unless validate_recap(params, @comment.errors, :rcc_pub => RECAPTCHA_PUBLIC_KEY, :rcc_priv => RECAPTCHA_PRIVATE_KEY)
+ raise ActiveRecord::RecordInvalid.new(@comment) unless captcha_is_valid_for @comment, :with => params
@comment.save!
+
cookies[:comment_name] = @comment.name
cookies[:comment_email] = @comment.email
cookies[:comment_url] = @comment.url
+ my_comments << @comment.id
flash[:notice] = 'Thanks for your comment'
redirect_to code_by_slug_path(@code.slug_name)
@@ -31,4 +39,31 @@ def create
render :template => 'codes/show'
flash[:error] = 'Sorry, unable to save your comment'
end
+
+ # destroy expects to be called with just an id, no code_id necessary
+ def destroy
+ @comment = Comment.find params[:id]
+ if can_delete @comment
+ @comment.destroy
+ flash[:notice] = 'Your comment has been deleted'
+ else
+ flash[:error] = 'You are not allowed to delete that comment'
+ end
+ redirect_to code_by_slug_path(@comment.code_slug_name)
+ end
+
+ def can_delete comment
+ my_comments.include? comment.id
+ end
+
+private
+ def my_comments
+ session[:my_comments] ||= []
+ end
+
+ def captcha_is_valid_for comment, options
+ return true if ENV['RAILS_ENV'] == 'test' # captcha is always valid in test mode
+ return validate_recap(options[:with], comment.errors, :rcc_pub => RECAPTCHA_PUBLIC_KEY, :rcc_priv => RECAPTCHA_PRIVATE_KEY)
+ end
+
end
View
4 app/helpers/comments_helper.rb
@@ -7,6 +7,10 @@ def name_link_for comment
end
end
+ def delete_link_for comment
+ link_to "DELETE", comment_path(comment), :method => :delete, :class => 'delete-comment' unless comment.new_record?
+ end
+
def opinion_for comment
if comment.works_for_me?
"<span class=\"works\">Working</span>"
View
3  app/views/comments/_comment.html.erb
@@ -1,5 +1,5 @@
<div class="comment rounded">
- <%= gravatar_for comment %>
+ <%= gravatar_for comment %>
<p class="status">
<%= link_to h(comment.code.slug_name), code_by_slug_path(comment.code.slug_name), :class => 'gem' %>
<%= "(#{comment.version})" unless comment.version.blank? %>
@@ -7,4 +7,5 @@
for <%= name_link_for comment %> <span class="lighterblue">(<%= comment.platform %>)</span><br />
</p>
<%= "<blockquote>#{format_comment(comment.body)}</blockquote>" if local_assigns[:show_quote] && !comment.body.blank? %>
+ <p><%= delete_link_for comment %></p>
</div>
View
15 features/adding-a-comment.feature
@@ -5,8 +5,9 @@ Feature: adding a comment
So that I can help the community track which gems work with Ruby 1.9
Scenario: adding a comment
-
- Given a gem called "rubynuts"
+
+ Given an initialised database
+ And a gem called "rubynuts"
When I visit the page for "rubynuts"
Then I see the comment form
@@ -15,6 +16,14 @@ Feature: adding a comment
And I press "submit comment"
Then I see my comment on the page
- Scenario: adding a comment and then editing it
+ Scenario: adding a comment and then deleting it
+
+ GivenScenario: adding a comment
+
+ When I visit the page for "rubynuts"
+ Then I see the delete comment link
+
+ When I click the delete comment link
+ Then I do not see my comment on the page
Scenario: viewing someone else's comment
View
13 features/step_definitions/comment_steps.rb
@@ -7,12 +7,23 @@
fill_in "Email", :with => 'henry@testing.com'
choose :comment_works_for_me_true
fill_in "Version", :with => '1.0'
- fill_in "Platform", :with => 'Mac OSX'
+ select 'Mac OSX', :from => 'Platform'
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
end
+When /^I click the delete comment link$/ do
+ click_link 'DELETE'
+end
+
+
Then /^I see my comment on the page$/ do
response.should include_text('Here is my test comment')
end
+Then /^I see the delete comment link$/ do
+ response.should have_tag('a.delete-comment')
+end
+Then /^I do not see my comment on the page$/ do
+ response.should_not include_text('Here is my test comment')
+end
View
3  features/step_definitions/general_steps.rb
@@ -0,0 +1,3 @@
+Given /^an initialised database$/ do
+ Platform.load_defaults
+end
View
BIN  public/images/misc/recreate.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
11 public/stylesheets/styles.css
@@ -300,7 +300,6 @@ dd {
#new-comment-form label {
font-size: 11px;
line-height: 130%;
-
}
@@ -314,3 +313,13 @@ dd {
#new-comment-form .text_field {
width: 250px;
}
+
+a.delete-comment {
+ background: url(/images/misc/recreate.gif) no-repeat;
+ padding: 2px 4px 4px 18px;
+ font-size: 8pt;
+ font-weight: bold;
+ text-transform: uppercase;
+ text-decoration: none;
+ color: #95ABC3;
+}
View
66 spec/controllers/comments_controller_spec.rb
@@ -0,0 +1,66 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe CommentsController do
+ describe "creating a comment" do
+ it "should create a new comment" do
+ @code = mock_model Code, :slug_name => 'thingy'
+ @comment = mock_model Comment, :name => 'This', :email => 'That', :url => 'The other'
+
+ on_posting_to :create, :code_id => '1', :comment => { :some => :fields } do
+ Code.should_receive(:find).with('1').and_return(@code)
+ @code.should_receive(:build_comment).with("some" => :fields).and_return(@comment)
+ @comment.should_receive(:valid?).and_return(true)
+ controller.should_receive(:captcha_is_valid_for).and_return(true)
+ @comment.should_receive(:save!).and_return(true)
+ end
+
+ cookies[:comment_name].should == ['This']
+ cookies[:comment_email].should == ['That']
+ cookies[:comment_url].should == ['The other']
+ session[:my_comments].should == [@comment.id]
+ response.should redirect_to(code_by_slug_path('thingy'))
+ flash[:notice].should == 'Thanks for your comment'
+ end
+ end
+
+ describe "deleting a comment" do
+ it "should allow you to delete if the session marks the comment as one of yours" do
+ @comment = mock_model Comment
+ session[:my_comments] = [@comment.id]
+
+ controller.can_delete(@comment).should be_true
+ end
+
+ it "should allow you to delete if the session marks the comment as one of yours" do
+ @comment = mock_model Comment
+ session[:my_comments] = []
+
+ controller.can_delete(@comment).should be_false
+ end
+
+ it "should not allow you to delete another person's comment" do
+ @comment = mock_model Comment, :code_slug_name => 'thingy'
+
+ on_deleting_from :destroy, :id => '1' do
+ Comment.should_receive(:find).with('1').and_return(@comment)
+ controller.should_receive(:can_delete).with(@comment).and_return(false)
+ end
+
+ response.should redirect_to(code_by_slug_path('thingy'))
+ flash[:error].should == 'You are not allowed to delete that comment'
+ end
+
+ it "should allow you to delete your own comments" do
+ @comment = mock_model Comment, :code_slug_name => 'thingy'
+
+ on_deleting_from :destroy, :id => '1' do
+ Comment.should_receive(:find).with('1').and_return(@comment)
+ controller.should_receive(:can_delete).with(@comment).and_return(true)
+ @comment.should_receive(:destroy)
+ end
+
+ response.should redirect_to(code_by_slug_path('thingy'))
+ flash[:notice].should == 'Your comment has been deleted'
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.