Fix #3930: Can't remove children from a parent post through child: metatag.
Add `child:none` and `-child:123` edit metatags. Allow using ranges with these metatags (e.g. `-child:1..10`, `child:1,3,5`).
This commit is contained in:
@@ -791,7 +791,7 @@ class Post < ApplicationRecord
|
|||||||
def filter_metatags(tags)
|
def filter_metatags(tags)
|
||||||
@pre_metatags, tags = tags.partition {|x| x =~ /\A(?:rating|parent|-parent|-?locked):/i}
|
@pre_metatags, tags = tags.partition {|x| x =~ /\A(?:rating|parent|-parent|-?locked):/i}
|
||||||
tags = apply_categorization_metatags(tags)
|
tags = apply_categorization_metatags(tags)
|
||||||
@post_metatags, tags = tags.partition {|x| x =~ /\A(?:-pool|pool|newpool|fav|-fav|child|-favgroup|favgroup|upvote|downvote):/i}
|
@post_metatags, tags = tags.partition {|x| x =~ /\A(?:-pool|pool|newpool|fav|-fav|child|-child|-favgroup|favgroup|upvote|downvote):/i}
|
||||||
apply_pre_metatags
|
apply_pre_metatags
|
||||||
return tags
|
return tags
|
||||||
end
|
end
|
||||||
@@ -841,10 +841,20 @@ class Post < ApplicationRecord
|
|||||||
when /^(up|down)vote:(.+)$/i
|
when /^(up|down)vote:(.+)$/i
|
||||||
vote!($1)
|
vote!($1)
|
||||||
|
|
||||||
|
when /^child:none$/i
|
||||||
|
children.each do |post|
|
||||||
|
post.update!(parent_id: nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
when /^-child:(.+)$/i
|
||||||
|
children.numeric_attribute_matches(:id, $1).each do |post|
|
||||||
|
post.update!(parent_id: nil)
|
||||||
|
end
|
||||||
|
|
||||||
when /^child:(.+)$/i
|
when /^child:(.+)$/i
|
||||||
child = Post.find($1)
|
Post.numeric_attribute_matches(:id, $1).where.not(id: id).limit(10).each do |post|
|
||||||
child.parent_id = id
|
post.update!(parent_id: id)
|
||||||
child.save
|
end
|
||||||
|
|
||||||
when /^-favgroup:(\d+)$/i
|
when /^-favgroup:(\d+)$/i
|
||||||
favgroup = FavoriteGroup.where("id = ?", $1.to_i).for_creator(CurrentUser.user.id).first
|
favgroup = FavoriteGroup.where("id = ?", $1.to_i).for_creator(CurrentUser.user.id).first
|
||||||
|
|||||||
@@ -954,16 +954,26 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "for a child" do
|
context "for a child" do
|
||||||
setup do
|
should "add and remove children" do
|
||||||
@child = FactoryBot.create(:post)
|
@children = FactoryBot.create_list(:post, 3, parent_id: nil)
|
||||||
end
|
|
||||||
|
|
||||||
should "update the parent relationships for both posts" do
|
@post.update(tag_string: "aaa child:#{@children.first.id}..#{@children.last.id}")
|
||||||
@post.update_attributes(:tag_string => "aaa child:#{@child.id}")
|
assert_equal(true, @post.reload.has_children?)
|
||||||
@post.reload
|
assert_equal(@post.id, @children[0].reload.parent_id)
|
||||||
@child.reload
|
assert_equal(@post.id, @children[1].reload.parent_id)
|
||||||
assert_equal(@post.id, @child.parent_id)
|
assert_equal(@post.id, @children[2].reload.parent_id)
|
||||||
assert(@post.has_children?)
|
|
||||||
|
@post.update(tag_string: "aaa -child:#{@children.first.id}")
|
||||||
|
assert_equal(true, @post.reload.has_children?)
|
||||||
|
assert_nil(@children[0].reload.parent_id)
|
||||||
|
assert_equal(@post.id, @children[1].reload.parent_id)
|
||||||
|
assert_equal(@post.id, @children[2].reload.parent_id)
|
||||||
|
|
||||||
|
@post.update(tag_string: "aaa child:none")
|
||||||
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user