Skip to content

Commit 8e0d257

Browse files
author
Rahoul Baruah
committed
only shows the delete link if you have permission to delete
1 parent f8f8c58 commit 8e0d257

File tree

7 files changed

+45
-6
lines changed

7 files changed

+45
-6
lines changed

app/controllers/application.rb

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class ApplicationController < ActionController::Base
66
filter_parameter_logging :password
77
layout 'isitruby19'
88
include ReCaptcha::AppHelper
9+
helper_method :my_comments
910

1011
protected
1112
# build an rss feed for the given collection
@@ -29,4 +30,11 @@ def rss_for objects, mapping
2930
end
3031
end.to_s
3132
end
33+
34+
# uses the session to store which comments I created
35+
def my_comments
36+
session[:my_comments] ||= []
37+
end
38+
39+
3240
end

app/controllers/comments_controller.rb

-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ def can_delete comment
5757
end
5858

5959
private
60-
def my_comments
61-
session[:my_comments] ||= []
62-
end
63-
6460
def captcha_is_valid_for comment, options
6561
return true if ENV['RAILS_ENV'] == 'test' # captcha is always valid in test mode
6662
return validate_recap(options[:with], comment.errors, :rcc_pub => RECAPTCHA_PUBLIC_KEY, :rcc_priv => RECAPTCHA_PRIVATE_KEY)

app/helpers/comments_helper.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ def name_link_for comment
88
end
99

1010
def delete_link_for comment
11-
link_to "DELETE", comment_path(comment), :method => :delete, :class => 'delete-comment' unless comment.new_record?
11+
return nil if comment.new_record?
12+
return nil unless my_comments.include?(comment.id)
13+
link_to "DELETE", comment_path(comment), :method => :delete, :class => 'delete-comment', :confirm => 'Are you sure?'
1214
end
1315

1416
def opinion_for comment

features/adding-a-comment.feature

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,11 @@ Feature: adding a comment
2626
When I click the delete comment link
2727
Then I do not see my comment on the page
2828

29-
Scenario: viewing someone else's comment
29+
Scenario: viewing someone else's comment
30+
31+
Given an initialised database
32+
And a gem called "rubynuts"
33+
And a comment against "rubynuts"
34+
35+
When I visit the page for "rubynuts"
36+
Then I do not see the delete comment link

features/step_definitions/comment_steps.rb

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Given /^a comment against "(.*)"$/ do | name |
2+
code = Code.find_by_name! name
3+
comment = a_saved Comment, :code => code
4+
end
5+
6+
17
Then /^I see the comment form$/ do
28
response.should have_tag('div#new-comment-form')
39
end
@@ -24,6 +30,11 @@
2430
response.should have_tag('a.delete-comment')
2531
end
2632

33+
Then /^I do not see the delete comment link$/ do
34+
response.should_not have_tag('a.delete-comment')
35+
end
36+
37+
2738
Then /^I do not see my comment on the page$/ do
2839
response.should_not include_text('Here is my test comment')
2940
end

lib/object_factory_config.rb

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
def prepare_object_factory
22
when_creating_a Code, :auto_generate => :name
33

4+
when_creating_a Comment,
5+
:auto_generate => :name,
6+
:generate_email_address => :email,
7+
:generate => {
8+
:code => lambda { a_saved Code },
9+
:platform => lambda { Platform.first }
10+
}
411
end

public/stylesheets/styles.css

+8
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ div.errorExplanation {
279279
margin-bottom: 10px;
280280
}
281281

282+
#flash_error {
283+
border: 1px solid red;
284+
background-color: #CC0000;
285+
padding: 10px;
286+
margin-bottom: 10px;
287+
}
288+
289+
282290
dt {
283291
color: #95ABC3;
284292
}

0 commit comments

Comments
 (0)