diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 11b6683..0c5884c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -22,28 +22,10 @@ - + - - - - - - - - - - - - - - - - - - - + @@ -52,7 +34,7 @@ - + @@ -97,7 +79,25 @@ - + + + + + + + + + + + + + + + + + + + @@ -106,7 +106,7 @@ - + @@ -122,22 +122,22 @@ @@ -172,12 +172,12 @@ - - + + - - @@ -602,12 +602,12 @@ - - + + - - @@ -885,12 +885,11 @@ - - + @@ -900,6 +899,7 @@ + @@ -979,13 +979,6 @@ - - - - - - - @@ -1000,9 +993,9 @@ - + - + @@ -1021,6 +1014,13 @@ + + + + + + + @@ -1030,42 +1030,40 @@ - - - + - + - + - + - + - + - + - + - + - + - + diff --git a/app/controllers/microposts_controller.rb b/app/controllers/microposts_controller.rb index 5aab208..4b8a816 100644 --- a/app/controllers/microposts_controller.rb +++ b/app/controllers/microposts_controller.rb @@ -1,5 +1,6 @@ class MicropostsController < ApplicationController before_filter :signed_in_user, only: [:create, :destroy] + before_filter :correct_user, only: :destroy def create @micropost = current_user.microposts.build(params[:micropost]) @@ -13,5 +14,16 @@ def create end def destroy + @micropost.destroy + redirect_back_or root_path end + + private + + def correct_user + @micropost = current_user.microposts.find(params[:id]) + rescue + redirect_to root_path + end + end \ No newline at end of file diff --git a/app/views/microposts/_micropost.html.erb b/app/views/microposts/_micropost.html.erb index 43c4f43..bfc505c 100644 --- a/app/views/microposts/_micropost.html.erb +++ b/app/views/microposts/_micropost.html.erb @@ -3,4 +3,9 @@ Posted <%= time_ago_in_words(micropost.created_at) %> ago. + <% if current_user?(micropost.user) %> + <%= link_to "delete", micropost, method: :delete, + confirm: "You sure?", + title: micropost.content %> + <% end %> \ No newline at end of file diff --git a/app/views/shared/_feed_item.html.erb b/app/views/shared/_feed_item.html.erb index 6e1d4c7..952df96 100644 --- a/app/views/shared/_feed_item.html.erb +++ b/app/views/shared/_feed_item.html.erb @@ -1,10 +1,15 @@
  • <%= link_to gravatar_for(feed_item.user), feed_item.user %> - <%= link_to feed_item.user.name, feed_item.user %> - + <%= link_to feed_item.user.name, feed_item.user %> + <%= feed_item.content %> - - Posted <%= time_ago_in_words(feed_item.created_at) %> ago. - + + Posted <%= time_ago_in_words(feed_item.created_at) %> ago. + + <% if current_user?(feed_item.user) %> + <%= link_to "delete", feed_item, method: :delete, + confirm: "You sure?", + title: feed_item.content %> + <% end %>
  • \ No newline at end of file diff --git a/spec/requests/micropost_pages_spec.rb b/spec/requests/micropost_pages_spec.rb index 76b5655..72eabf8 100644 --- a/spec/requests/micropost_pages_spec.rb +++ b/spec/requests/micropost_pages_spec.rb @@ -30,4 +30,17 @@ end end end + + describe "micropost destruction" do + before { FactoryGirl.create(:micropost, user: user) } + + describe "as correct user" do + before { visit root_path } + + it "should delete a micropost" do + expect { click_link "delete" }.should change(Micropost, :count).by(-1) + end + end + end + end \ No newline at end of file