diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index 910b81a60..ec5b5f615 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -2,7 +2,7 @@ class ForumTopic < ActiveRecord::Base attr_accessible :title, :original_post_attributes belongs_to :creator, :class_name => "User" belongs_to :updater, :class_name => "User" - has_many :posts, :class_name => "ForumPost", :order => "forum_posts.id asc", :foreign_key => "topic_id" + has_many :posts, :class_name => "ForumPost", :order => "forum_posts.id asc", :foreign_key => "topic_id", :dependent => :destroy has_one :original_post, :class_name => "ForumPost", :order => "forum_posts.id asc", :foreign_key => "topic_id" before_validation :initialize_creator, :on => :create before_validation :initialize_updater diff --git a/test/functional/forum_topics_controller_test.rb b/test/functional/forum_topics_controller_test.rb index 84af303a0..d9a50a0cb 100644 --- a/test/functional/forum_topics_controller_test.rb +++ b/test/functional/forum_topics_controller_test.rb @@ -74,7 +74,16 @@ class ForumTopicsControllerTest < ActionController::TestCase end context "destroy action" do + setup do + @post = Factory.create(:forum_post, :topic_id => @forum_topic.id) + end + should "destroy the topic and any associated posts" do + assert_difference(["ForumPost.count", "ForumTopic.count"], -1) do + post :destroy, {:id => @forum_topic.id}, {:user_id => @user.id} + end + assert_redirected_to(forum_topics_path) + end end end end diff --git a/test/unit/forum_topic_test.rb b/test/unit/forum_topic_test.rb index 54a7c12b8..13dd07c11 100644 --- a/test/unit/forum_topic_test.rb +++ b/test/unit/forum_topic_test.rb @@ -42,5 +42,19 @@ class ForumTopicTest < ActiveSupport::TestCase assert_equal(@second_user.id, @topic.updater_id) end end + + context "with multiple posts that has been deleted" do + setup do + 5.times do + Factory.create(:forum_post, :topic_id => @topic.id) + end + end + + should "delete any associated posts" do + assert_difference("ForumPost.count", -5) do + @topic.destroy + end + end + end end end