Fix #5153: Using the child metatag without an id causes unexpected behavior
This commit is contained in:
@@ -526,11 +526,15 @@ class Post < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
in "-child", ids
|
in "-child", ids
|
||||||
|
next if ids.blank?
|
||||||
|
|
||||||
children.search(id: ids).each do |post|
|
children.search(id: ids).each do |post|
|
||||||
post.update!(parent_id: nil)
|
post.update!(parent_id: nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
in "child", ids
|
in "child", ids
|
||||||
|
next if ids.blank?
|
||||||
|
|
||||||
Post.search(id: ids).where.not(id: id).limit(10).each do |post|
|
Post.search(id: ids).where.not(id: id).limit(10).each do |post|
|
||||||
post.update!(parent_id: id)
|
post.update!(parent_id: id)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -798,27 +798,55 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "for a child" do
|
context "for a child" do
|
||||||
should "add and remove children" do
|
should "add children with child:ids" do
|
||||||
@children = FactoryBot.create_list(:post, 3, parent_id: nil)
|
@children = create_list(:post, 3, parent: nil)
|
||||||
|
|
||||||
@post.update(tag_string: "aaa child:#{@children.first.id}..#{@children.last.id}")
|
@post.update(tag_string: "aaa child:#{@children.first.id}..#{@children.last.id}")
|
||||||
|
|
||||||
assert_equal(true, @post.reload.has_children?)
|
assert_equal(true, @post.reload.has_children?)
|
||||||
assert_equal(@post.id, @children[0].reload.parent_id)
|
assert_equal(@post.id, @children[0].reload.parent_id)
|
||||||
assert_equal(@post.id, @children[1].reload.parent_id)
|
assert_equal(@post.id, @children[1].reload.parent_id)
|
||||||
assert_equal(@post.id, @children[2].reload.parent_id)
|
assert_equal(@post.id, @children[2].reload.parent_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "remove children with -child:ids" do
|
||||||
|
@children = create_list(:post, 3, parent: @post)
|
||||||
@post.update(tag_string: "aaa -child:#{@children.first.id}")
|
@post.update(tag_string: "aaa -child:#{@children.first.id}")
|
||||||
|
|
||||||
assert_equal(true, @post.reload.has_children?)
|
assert_equal(true, @post.reload.has_children?)
|
||||||
assert_nil(@children[0].reload.parent_id)
|
assert_nil(@children[0].reload.parent_id)
|
||||||
assert_equal(@post.id, @children[1].reload.parent_id)
|
assert_equal(@post.id, @children[1].reload.parent_id)
|
||||||
assert_equal(@post.id, @children[2].reload.parent_id)
|
assert_equal(@post.id, @children[2].reload.parent_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "remove all children with child:none" do
|
||||||
|
@children = create_list(:post, 3, parent: @post)
|
||||||
|
@post.update!(tag_string: "aaa child:none")
|
||||||
|
|
||||||
@post.update(tag_string: "aaa child:none")
|
|
||||||
assert_equal(false, @post.reload.has_children?)
|
assert_equal(false, @post.reload.has_children?)
|
||||||
assert_nil(@children[0].reload.parent_id)
|
assert_nil(@children[0].reload.parent_id)
|
||||||
assert_nil(@children[1].reload.parent_id)
|
assert_nil(@children[1].reload.parent_id)
|
||||||
assert_nil(@children[2].reload.parent_id)
|
assert_nil(@children[2].reload.parent_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "not add children with child:" do
|
||||||
|
@children = create_list(:post, 3, parent: nil)
|
||||||
|
@post.update(tag_string: "aaa child:")
|
||||||
|
|
||||||
|
assert_equal(false, @post.reload.has_children?)
|
||||||
|
assert_nil(@children[0].reload.parent_id)
|
||||||
|
assert_nil(@children[1].reload.parent_id)
|
||||||
|
assert_nil(@children[2].reload.parent_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "not remove children with -child:" do
|
||||||
|
@children = create_list(:post, 3, parent: @post)
|
||||||
|
@post.update!(tag_string: "aaa -child:")
|
||||||
|
|
||||||
|
assert_equal(true, @post.reload.has_children?)
|
||||||
|
assert_equal(@post, @children[0].reload.parent)
|
||||||
|
assert_equal(@post, @children[1].reload.parent)
|
||||||
|
assert_equal(@post, @children[2].reload.parent)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "for status:active" do
|
context "for status:active" do
|
||||||
|
|||||||
Reference in New Issue
Block a user