diff --git a/app/controllers/forum_posts_controller.rb b/app/controllers/forum_posts_controller.rb index ac186993c..88a40b51c 100644 --- a/app/controllers/forum_posts_controller.rb +++ b/app/controllers/forum_posts_controller.rb @@ -47,7 +47,7 @@ class ForumPostsController < ApplicationController def destroy @forum_post = ForumPost.find(params[:id]) - raise User::PrivilegeError unless CurrentUser.is_janitor? + raise User::PrivilegeError unless @forum_post.editable_by?(CurrentUser.user) @forum_post.update_attribute(:is_deleted, true) respond_with(@forum_post) end diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 8184c1128..d31eda694 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -10,6 +10,7 @@ class ForumPost < ActiveRecord::Base validates_presence_of :body, :creator_id validate :validate_topic_is_unlocked before_destroy :validate_topic_is_unlocked + after_save :delete_topic_if_original_post module SearchMethods def body_matches(body) @@ -122,6 +123,14 @@ class ForumPost < ActiveRecord::Base ForumPost.exists?(["id = ? and id = (select _.id from forum_posts _ where _.topic_id = ? order by _.id asc limit 1)", id, topic_id]) end + def delete_topic_if_original_post + if is_deleted? && is_original_post? + topic.update_attribute(:is_deleted, true) + end + + true + end + def build_response dup.tap do |x| x.body = x.quoted_response diff --git a/app/views/forum_topics/_form.html.erb b/app/views/forum_topics/_form.html.erb index 5af4655f5..a3d6551bc 100644 --- a/app/views/forum_topics/_form.html.erb +++ b/app/views/forum_topics/_form.html.erb @@ -5,7 +5,7 @@ <%= f.input :title %> <%= f.simple_fields_for :original_post do |pf| %> - <% unless @forum_topic.new_record? %> + <% if !@forum_topic.new_record? %> <%= hidden_field_tag "forum_topic[original_post_attributes][topic_id]", @forum_topic.id %> <% end %> diff --git a/test/unit/forum_post_test.rb b/test/unit/forum_post_test.rb index a33a1ad96..07c785809 100644 --- a/test/unit/forum_post_test.rb +++ b/test/unit/forum_post_test.rb @@ -69,6 +69,18 @@ class ForumPostTest < ActiveSupport::TestCase assert_equal(@user.id, post.creator_id) end + context "that is deleted" do + setup do + @post = FactoryGirl.create(:forum_post, :topic_id => @topic.id) + @post.update_attribute(:is_deleted, true) + @topic.reload + end + + should "also delete the topic" do + assert(@topic.is_deleted) + end + end + context "updated by a second user" do setup do @post = FactoryGirl.create(:forum_post, :topic_id => @topic.id)