Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

delete comments link now appears on page

  • Loading branch information...
commit f8f8c5873b240614f9c700e70fffa2b049edc54a 1 parent a24de0e
Rahoul Baruah rahoulb authored
37 app/controllers/comments_controller.rb
@@ -2,6 +2,8 @@ class CommentsController < ApplicationController
2 2 cache_sweeper :code_and_comment_sweeper
3 3 skip_before_filter :verify_authenticity_token
4 4
  5 + # index expects to be called in the context of a Code
  6 + # so a code_id is passed in
5 7 def index
6 8 @code = Code.find_by_slug_name! params[:code_id]
7 9 respond_to do |wants|
@@ -15,15 +17,21 @@ def index
15 17 end
16 18 end
17 19
  20 + # create expects to be called in the context of a Code
  21 + # so a code_id is passed in
18 22 def create
19 23 @code = Code.find params[:code_id]
20 24 @comment = @code.build_comment params[:comment]
  25 +
  26 + # validate the comment, then test the captcha to ensure all errors are shown on the form
21 27 @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
23 29 @comment.save!
  30 +
24 31 cookies[:comment_name] = @comment.name
25 32 cookies[:comment_email] = @comment.email
26 33 cookies[:comment_url] = @comment.url
  34 + my_comments << @comment.id
27 35 flash[:notice] = 'Thanks for your comment'
28 36 redirect_to code_by_slug_path(@code.slug_name)
29 37
@@ -31,4 +39,31 @@ def create
31 39 render :template => 'codes/show'
32 40 flash[:error] = 'Sorry, unable to save your comment'
33 41 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 +
34 69 end
4 app/helpers/comments_helper.rb
@@ -7,6 +7,10 @@ def name_link_for comment
7 7 end
8 8 end
9 9
  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 +
10 14 def opinion_for comment
11 15 if comment.works_for_me?
12 16 "<span class=\"works\">Working</span>"
3  app/views/comments/_comment.html.erb
... ... @@ -1,5 +1,5 @@
1 1 <div class="comment rounded">
2   - <%= gravatar_for comment %>
  2 + <%= gravatar_for comment %>
3 3 <p class="status">
4 4 <%= link_to h(comment.code.slug_name), code_by_slug_path(comment.code.slug_name), :class => 'gem' %>
5 5 <%= "(#{comment.version})" unless comment.version.blank? %>
@@ -7,4 +7,5 @@
7 7 for <%= name_link_for comment %> <span class="lighterblue">(<%= comment.platform %>)</span><br />
8 8 </p>
9 9 <%= "<blockquote>#{format_comment(comment.body)}</blockquote>" if local_assigns[:show_quote] && !comment.body.blank? %>
  10 + <p><%= delete_link_for comment %></p>
10 11 </div>
15 features/adding-a-comment.feature
@@ -5,8 +5,9 @@ Feature: adding a comment
5 5 So that I can help the community track which gems work with Ruby 1.9
6 6
7 7 Scenario: adding a comment
8   -
9   - Given a gem called "rubynuts"
  8 +
  9 + Given an initialised database
  10 + And a gem called "rubynuts"
10 11
11 12 When I visit the page for "rubynuts"
12 13 Then I see the comment form
@@ -15,6 +16,14 @@ Feature: adding a comment
15 16 And I press "submit comment"
16 17 Then I see my comment on the page
17 18
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
19 28
20 29 Scenario: viewing someone else's comment
13 features/step_definitions/comment_steps.rb
@@ -7,12 +7,23 @@
7 7 fill_in "Email", :with => 'henry@testing.com'
8 8 choose :comment_works_for_me_true
9 9 fill_in "Version", :with => '1.0'
10   - fill_in "Platform", :with => 'Mac OSX'
  10 + select 'Mac OSX', :from => 'Platform'
11 11 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
12 12 end
13 13
  14 +When /^I click the delete comment link$/ do
  15 + click_link 'DELETE'
  16 +end
  17 +
  18 +
14 19 Then /^I see my comment on the page$/ do
15 20 response.should include_text('Here is my test comment')
16 21 end
17 22
  23 +Then /^I see the delete comment link$/ do
  24 + response.should have_tag('a.delete-comment')
  25 +end
18 26
  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
3  features/step_definitions/general_steps.rb
... ... @@ -0,0 +1,3 @@
  1 +Given /^an initialised database$/ do
  2 + Platform.load_defaults
  3 +end
BIN  public/images/misc/recreate.gif
11 public/stylesheets/styles.css
@@ -300,7 +300,6 @@ dd {
300 300 #new-comment-form label {
301 301 font-size: 11px;
302 302 line-height: 130%;
303   -
304 303 }
305 304
306 305
@@ -314,3 +313,13 @@ dd {
314 313 #new-comment-form .text_field {
315 314 width: 250px;
316 315 }
  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 +}
66 spec/controllers/comments_controller_spec.rb
... ... @@ -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 comments on commit f8f8c58

Please sign in to comment.
Something went wrong with that request. Please try again.