Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete posts feature #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def update
end
end

def destroy
@post.destroy
redirect_to root_url, :notice => "Your post has been deleted"
end

protected

def lookup_post
Expand Down
3 changes: 3 additions & 0 deletions app/views/posts/_post.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<h1><%= link_to post.title, post %></h1>
<h4>By: <%= post.author.email %></h4>
<% if user_signed_in? && post.author_id == current_user.id %>
<%= link_to "Delete this post", post, method: :delete, data: { confirm: "Are you sure?" }, title: post.title %>
<% end %>
<p><%= post.body %></p>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

root to: "posts#index"

resources :posts, except: [:destroy] do
resources :posts do
resources :comments, only: [:create]
end

Expand Down
34 changes: 34 additions & 0 deletions features/authors_can_delete_posts.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Feature: Authors can delete posts

Scenario: I can delete a post I wrote
Given I am signed in as "[email protected]"
Given I have created a blog post with title "foo" and body "bar"
When I go to the homepage
And I click "Delete this post"
Then I should see "Your post has been deleted"
And I should be on the homepage
And I should not see the title "foo"
And I should not see "bar"

Scenario: I cannot delete posts if I am not signed in
Given I am not signed in
Given the following post:
| title | foo |
| body | Foo Bar |
When I go to the homepage
Then I should not see "Delete this post"
And I should be on the homepage
And I should see the title "foo"
And I should see "Foo Bar"

Scenario: I cannot delete posts I didn't write
Given the following post:
| title | foo |
| body | Foo Bar |
Given I am signed in as a user that did not write the post
When I go to the homepage
Then I should not see "Delete this post"
And I should be on the homepage
And I should see the title "foo"
And I should see "Foo Bar"

10 changes: 10 additions & 0 deletions features/step_definitions/authentication_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
sign_in_as @user
end

Given /^I am not signed in$/ do
delete destroy_user_session_path
end

Given "I am signed in as a user that did not write the post" do
post = Post.last
@user = User.where("id != ?", post.author_id).first || Fabricate(:user)
sign_in_as @user
end

def sign_in_as user
steps %Q{
Given I am on the homepage
Expand Down
4 changes: 4 additions & 0 deletions features/step_definitions/blog_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
Then /^I should be viewing that blog post$/ do
step %{I should be on that blog post's page}
end

Given /^I have created a blog post with title "(.*?)" and body "(.*?)"$/ do |title, body|
Fabricate(:post, author: @user, title: title, body: body)
end
4 changes: 4 additions & 0 deletions features/step_definitions/interaction_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
page.should have_css("h1, h2", text: title_text)
end

Then /^I should not see the title "(.*?)"$/ do |title_text|
page.should_not have_css("h1, h2", text: title_text)
end

When /^I fill in "(.*?)" for "(.*?)"$/ do |text, field_label|
page.fill_in field_label, with: text
end
Expand Down