posts: don't reparent children when expunging posts.

If an expunged post has children, just set their parent to null instead
of reparenting them. Before we reparented them to the oldest child.
This commit is contained in:
evazion
2020-08-04 12:28:20 -05:00
parent b57122cf7f
commit f9aa1e9718
2 changed files with 1 additions and 24 deletions

View File

@@ -920,14 +920,7 @@ class Post < ApplicationRecord
end end
def update_children_on_destroy def update_children_on_destroy
return unless children.present? children.update(parent: nil)
eldest = children[0]
siblings = children[1..-1]
eldest.update(parent_id: nil)
Post.where(id: siblings).find_each { |p| p.update(parent_id: eldest.id) }
# Post.where(id: siblings).update(parent_id: eldest.id) # XXX rails 5
end end
def update_parent_on_save def update_parent_on_save

View File

@@ -254,17 +254,6 @@ class PostTest < ActiveSupport::TestCase
end end
end end
should "reparent all children to the first child" do
@p1.expunge!
@c1.reload
@c2.reload
@c3.reload
assert_nil(@c1.parent_id)
assert_equal(@c1.id, @c2.parent_id)
assert_equal(@c1.id, @c3.parent_id)
end
should "save a post version record for each child" do should "save a post version record for each child" do
assert_difference(["@c1.versions.count", "@c2.versions.count", "@c3.versions.count"]) do assert_difference(["@c1.versions.count", "@c2.versions.count", "@c3.versions.count"]) do
@p1.expunge! @p1.expunge!
@@ -273,11 +262,6 @@ class PostTest < ActiveSupport::TestCase
@c3.reload @c3.reload
end end
end end
should "set the has_children flag on the new parent" do
@p1.expunge!
assert_equal(true, @c1.reload.has_children?)
end
end end
end end