diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 4a7c2700a..ba2ee6e04 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -81,7 +81,7 @@ class PostsController < ApplicationController @post.destroy respond_to do |format| - format.html { redirect_to posts_url } + format.html { redirect_to posts_url, notice: 'Post was deleted.' } format.json { head :no_content } end end diff --git a/app/models/post.rb b/app/models/post.rb index de3c9cdcf..e38920623 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -3,7 +3,7 @@ class Post < ActiveRecord::Base friendly_id :author_date_subject, use: :slugged attr_accessible :body, :subject, :author_id belongs_to :author, :class_name => 'Member' - has_many :comments + has_many :comments, :dependent => :destroy default_scope order("created_at desc") def author_date_subject diff --git a/app/views/posts/show.html.haml b/app/views/posts/show.html.haml index a978c6e3f..7813e8501 100644 --- a/app/views/posts/show.html.haml +++ b/app/views/posts/show.html.haml @@ -5,9 +5,9 @@ - if can? :edit, @post or can? :destroy, @post .post-actions - if can? :edit, @post - = link_to 'Edit', edit_post_path(@post), :class => 'btn' + = link_to 'Edit Post', edit_post_path(@post), :class => 'btn' - if can? :destroy, @post - = link_to 'Delete', @post, method: :delete, | + = link_to 'Delete Post', @post, method: :delete, | data: { confirm: 'Are you sure?' }, :class => 'btn' diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index e889322eb..784bad0d3 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -27,4 +27,14 @@ describe Post do @comment2 = FactoryGirl.create(:comment, :post => @post) @post.comments.length.should == 2 end + + it "destroys comments when deleted" do + @post = FactoryGirl.create(:post, :author => @member) + @comment1 = FactoryGirl.create(:comment, :post => @post) + @comment2 = FactoryGirl.create(:comment, :post => @post) + @post.comments.length.should == 2 + all = Comment.count + @post.destroy + Comment.count.should == all - 2 + end end