Skip to content
Browse files

added delete comment feature for admin

  • Loading branch information...
1 parent 70d0a92 commit 10fb9d076e28ec6baddd0735f36d814333b5c3c7 @arunsark committed
View
4 README.markdown
@@ -7,3 +7,7 @@ This is simple Blog application. This blog features
RSpec Tests
Cucumber Acceptance Test
Cass & Haml (To be Done)
+Two issues
+In show.html.erb when I render comments it showing one more than the comments.size
+comments_controller_spec when I delete in spec comments size not changing
+but in controller comment size is decreasing
View
9 app/controllers/comments_controller.rb
@@ -1,7 +1,8 @@
class CommentsController < ApplicationController
- load_and_authorize_resource :only=>[:destroy,:update]
+ authorize_resource :only=>[:destroy,:update]
def create
begin
+ puts params[:post_id]
@post = Post.find_by_slug(params[:post_id])
@comment = @post.comments.new(params[:comment])
@@ -21,6 +22,12 @@ def new
end
def destroy
+ @post = Post.find_by_slug(params[:post_id])
+ @comment = @post.comments.find(params[:id])
+ @comment.destroy
+ logger.debug "Comment to be deleted is #{@comment._id}"
+ flash[:notice] = "Comment deleted."
+ redirect_to post_path(@post)
end
def update
View
3 app/views/comments/_comment.html.erb
@@ -1,4 +1,7 @@
<h3> <%= comment.author %> </h3>
<p> <%= comment.published_on %> </p>
<p> <%= comment.content %> </p>
+<% if comment.author && can?(:destroy,comment) %>
+ <p><%= link_to "Delete Comment", [@post,comment],:confirm => "Are you sure?" , :method => :delete %></p>
+<% end %>
View
2 features/create_comment.feature
@@ -14,5 +14,5 @@ Feature: Create comment
And I fill up the Content as "Excellent Post"
And I submit the Comment
Then I should see message "Thanks for the comment"
- And I should see my Comment appearing in the Post
+ And I should see the Comment appearing in the Post
View
8 features/create_post.feature
@@ -3,12 +3,12 @@ Feature: Create a Post
As a registered user
I want to Create a Post in the Blog
So that Post appears in the Blog for others to consume
-
+
+ Background:
+ Given I am logged in as a "author" with name "Arun"
Scenario: Create a Post
- Given I am an "author" with name "Arun", email "arun.vydianathan@gmail.com" and password "foobar"
- And I sign in as "arun.vydianathan@gmail.com/foobar"
- When I visit Create Post Page
+ Given I visit Create Post Page
And I fill up Title as "Foo Post"
And I fill up Content as "Bar Contents for Foo Post"
And I publish the Post
View
30 features/step_definitions/comment_steps.rb
@@ -24,7 +24,35 @@
click_button "Create Comment"
end
-Then /^I should see my Comment appearing in the Post$/ do
+Then /^I should see the Comment appearing in the Post$/ do
page.should have_content(@author)
page.should have_content(@content)
end
+
+When /^I have a comment "([^"]*)"$/ do |comment|
+ When %{I visit the Blog Post}
+ When %{I fill up Author as "Arun"}
+ When %{I fill up Email as "#{Factory.next(:email)}"}
+ When %{I fill up the Content as "#{comment}"}
+ When %{I submit the Comment}
+end
+
+When /^I visit the Show Post Page$/ do
+ visit post_path(@post)
+end
+
+Then /^I should see a Delete link for the comment$/ do
+ page.should have_link("Delete Comment")
+end
+
+When /^I click the "([^"]*)" Link$/ do |link|
+ click_link link
+end
+
+Then /^I should see the message "([^"]*)"$/ do |message|
+ page.should have_content(message)
+end
+
+Then /^I should not see the Comment appearing in the Post$/ do
+ page.should_not have_content(@content)
+end
View
49 features/step_definitions/post_steps.rb
@@ -1,19 +1,3 @@
-Given /^I am an "([^""]*)" with name "([^"]*)", email "([^"]*)" and password "([^"]*)"$/ do |role, name, email, password|
- @user = User.create!(:email => email,
- :password => password,
- :password_confirmation => password,
- :first_name => name,
- :last_name => name,
- :nick_name => name,
- :role => role)
-end
-
-Given /^I sign in as "(.*)\/(.*)"$/ do |email, password|
- Given %{I go to the sign in page}
- And %{I fill in "user_email" with "#{email}"}
- And %{I fill in "user_password" with "#{password}"}
- And %{I press "Sign in"}
-end
When /^I visit Create Post Page$/ do
visit posts_path
@@ -33,21 +17,21 @@
click_button "Create Post"
end
-Then /^I should see post in the index page$/ do
- post = Post.find_by_slug(PostsHelper.generate_slug(@title))
+Then /^I should see the updated title in the page$/ do
page.should have_content(@title)
- page.should have_content("Comment")
end
-Given /^I have post "([^"]*)" authored by me$/ do |title|
+
+Given /^I have post "([^"]*)" authored by "([^"]*)"$/ do |title,author_name|
@title = title
@post = Post.new(:title => title, :content => "Something goes here")
+ @user = find_user_by_name(author_name)
@post.users << @user
@post.save!
end
-When /^I visit Update Post Page$/ do
- visit edit_post_path(@post)# express the regexp above with the code you wish you had
+When /^I visit Update Post Page$/ do
+ visit edit_post_url(@post)# express the regexp above with the code you wish you had
end
When /^I fill up the Title as "([^"]*)"$/ do |title|
@@ -69,14 +53,13 @@
page.should_not have_content(@title)
end
-Given /^I have post "([^"]*)" authored by "([^"]*)"$/ do |title, author|
- @user = Factory(:user2)
- @title = title
- @post = Post.new(:title => title, :content => "Something goes here")
- @post.users << @user
- @post.save!
+Then /^I should see post in the index page$/ do
+ visit posts_path
+ page.should have_content(@title)
end
+
+
When /^I visit Posts Page$/ do
visit posts_path
end
@@ -95,3 +78,13 @@
page.should have_link(link2)
end
+def find_user_by_name(name)
+ user = User.where(nick_name:"#{name}").first
+ unless user
+ puts "create suresh"
+ role = "author"
+ Given %{I am a user with role "#{role}" and name "#{name}"}
+ user = User.where(nick_name:"#{name}").first
+ end
+ user
+end
View
30 features/step_definitions/user_steps.rb
@@ -0,0 +1,30 @@
+Given /^I am an "([^"]*)" with name "([^"]*)", email "([^"]*)" and password "([^"]*)"$/ do |role,name,email,password|
+ puts "create user"
+ @user = User.create!(:email => email,
+ :password => password,
+ :password_confirmation => password,
+ :first_name => name,
+ :last_name => name,
+ :nick_name => name,
+ :role => role)
+end
+
+Given /^I sign in as "(.*)\/(.*)"$/ do |email, password|
+ Given %{I go to the sign in page}
+ And %{I fill in "user_email" with "#{email}"}
+ And %{I fill in "user_password" with "#{password}"}
+ And %{I press "Sign in"}
+end
+
+Given /^I am logged in as a "([^"]*)" with name "([^"]*)"$/ do |role, name|
+ Given %{I am a user with role "#{role}" and name "#{name}"}
+ And %{I sign in as "#{@email}/#{@password}"}
+end
+
+Given /^I am a user with role "([^"]*)" and name "([^"]*)"$/ do |role,name|
+ @email = Factory.next(:email)
+ @password = "foobar"
+ puts @email
+ Given %{I am an "#{role}" with name "#{name}", email "#{@email}" and password "#{@password}"}
+ puts "success"
+end
View
22 features/update_comment.feature
@@ -0,0 +1,22 @@
+
+
+
+Feature: Update/Destroy a Comment
+ As an admin
+ I should be allowed Update or Destroy any comment in the system
+
+ Background: logged in as admin
+ Given I am logged in as a "admin" with name "Arun"
+
+
+ Scenario: Delete a Comment
+ Given a blog post with title "Foo Post" and content "Bar Content"
+ When I visit the Blog Post
+ And I have a comment "Crappy Post"
+ When I visit the Show Post Page
+ Then I should see the Comment appearing in the Post
+ And I should see a Delete link for the comment
+ When I click the "Delete Comment" Link
+ Then I should see the message "Comment deleted."
+ And I should not see the Comment appearing in the Post
+
View
32 features/update_post.feature
@@ -4,41 +4,29 @@ Feature: Update/Destroy a Post
As an author of a Post
I want to Update or Destroy the post created by me
-
+ Background: Logged in as an author
+ Given I am logged in as a "author" with name "Arun"
+
Scenario: Update a Post
- Given I am an "author" with name "Arun", email "arun.vydianathan@gmail.com" and password "foobar"
- And I sign in as "arun.vydianathan@gmail.com/foobar"
- And I have post "Foo Post" authored by me
+ Given I have post "Foo Post" authored by "Arun"
When I visit Update Post Page
And I fill up the Title as "Foo Bar Post"
And I update the Post
Then I should see message "Post was successfully updated."
- And I should see post in the index page
-
+ And I should see the updated title in the page
Scenario: Delete a Post
- Given I am an "author" with name "Arun", email "arun.vydianathan@gmail.com" and password "foobar"
- And I sign in as "arun.vydianathan@gmail.com/foobar"
- And I have post "Foo Post" authored by me
+ Given I have post "Foo Post" authored by "Arun"
When I delete the post
Then I should not see the post in the index page
-
-
+
Scenario: Should not allow Update/Destroy of Post not written by Author
- Given I am an "author" with name "Arun", email "arun.vydianathan@gmail.com" and password "foobar"
- And I sign in as "arun.vydianathan@gmail.com/foobar"
- And I have post "Foo Post" authored by "Suresh"
+ Given I have post "Foo Post" authored by "Suresh"
When I visit Posts Page
Then I should see the post
And I should not have the link to "Edit" or "Destroy"
-
- Scenario: Admin should have privilege to Update/Destroy any Post
- Given I am an "admin" with name "Arun", email "arun.vydianathan@gmail.com" and password "foobar"
- And I sign in as "arun.vydianathan@gmail.com/foobar"
- And I have post "Foo Post" authored by "Suresh"
- When I visit Posts Page
- Then I should see the post
- And I should have the link to "Edit" or "Destroy"
+
+
View
12 features/update_post_admin.feature
@@ -0,0 +1,12 @@
+Feature: Update/Destroy a Post as admin
+ As an admin
+ I want to Update or Destroy any post in the system
+
+ Background: Logged in as Admin
+ Given I am logged in as a "admin" with name "Arun"
+
+ Scenario: Admin should have privilege to Update/Destroy any Post
+ Given I have post "Foo Post" authored by "Suresh"
+ When I visit Posts Page
+ Then I should see the post
+ And I should have the link to "Edit" or "Destroy"
View
82 spec/controllers/comments_controller_spec.rb
@@ -3,37 +3,75 @@
describe CommentsController do
- let (:comment) { mock_model(Comment).as_null_object }
- before(:each) do
- @post = Factory(:post)
- @attr = { :author => "Arun", :email => "arun@gmail.com", :content => "Excellent Post!"}
- Post.stub(:find_by_slug).with("foo-post").and_return(@post)
- @post.comments.stub(:new).and_return(comment)
- end
-
- context "#success" do
+ context "Create Comment" do
+ let (:comment) { mock_model(Comment).as_null_object }
before(:each) do
- comment.stub(:save!)
+ @post = Factory(:post)
+ @attr = { :author => "Arun", :email => "arun@gmail.com", :content => "Excellent Post!"}
+ Post.stub(:find_by_slug).with(@post.slug).and_return(@post)
+ @post.comments.stub(:new).and_return(comment)
+ end
+
+ context "#success" do
+ before(:each) do
+ comment.stub(:save!)
+ end
+
+ it "should create a comment" do
+ comment.should_receive(:save!)
+ post :create , :post_id=>@post.slug,:comment=> @attr
+ flash[:notice].should =~ /thanks for the comment/i
+ response.should redirect_to(post_path(@post))
+ end
+
end
- it "should create a comment" do
- comment.should_receive(:save!)
- post :create , :post_id=>@post.slug,:comment=> @attr
- flash[:notice].should =~ /thanks for the comment/i
- response.should redirect_to(post_path(@post))
+ context "#failure" do
+ before(:each) do
+ comment.stub(:save!).and_raise
+ end
+
+ it "should not create a comment" do
+ comment.should_receive(:save!).and_raise
+ post :create , :post_id=>@post.slug,:comment=> @attr
+ flash[:alert].should =~ /please fill in all the data/i
+ response.should render_template('posts/show')
+ end
end
end
- context "#failure" do
+ describe "PUT destroy" do
+
before(:each) do
- comment.stub(:save!).and_raise
+ @post = Factory(:post)
+ @attr = { :author => "Arun", :email => "arun@gmail.com", :content => "Excellent Post!"}
+ @comment = @post.comments.new(@attr)
+ @comment.save!
+ puts "enter before each"
end
+ context "Admin" do
+ before(:each) do
+ @admin = sign_in(Factory(:user1))
+ end
- it "should not create a comment" do
- comment.should_receive(:save!).and_raise
- post :create , :post_id=>@post.slug,:comment=> @attr
- flash[:alert].should =~ /please fill in all the data/i
- response.should render_template('posts/show')
+ it "should destroy any comment" do
+ @post.comments.size.should == 1
+ delete :destroy, :post_id => @post.slug, :id => @comment._id
+ @comment = @post.comments.find(@comment._id)
+ puts @comment._id
+ response.should redirect_to(post_path(@post))
+ end
+ end
+ context "Author" do
+ before(:each) do
+ @author = sign_in(Factory(:user2))
+ end
+ it "should not destroy any comment" do
+ lambda do
+ delete :destroy, :post_id => "foo-post", :id => @comment._id
+ end.should_not change(@post.comments,:size)
+ end
end
end
+
end
View
5 spec/factories.rb
@@ -43,9 +43,12 @@
post.after_create { |p| Factory(:user3, :posts => [p]) }
end
+
#Factory.define :author_with_posts, :parent => :user3 do |user|
# user.after_create { |u| Factory(:post1, :users => [u]) }
#end
-
+Factory.sequence :email do |n|
+ "person-#{n}@example.com"
+end
View
11 spec/models/comment_spec.rb
@@ -42,6 +42,15 @@
@comment.save!
@comment.published_on.strftime('%Y%m%d').should == Date.today.strftime('%Y%m%d')
end
-
end
+
+ describe "Comment Deletion" do
+ it "should destroy the comment" do
+ @post.comments.size.should == 1
+ lambda do
+ @comment.destroy
+ end.should change(@post.comments,:size)
+ end
+ end
+
end

0 comments on commit 10fb9d0

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