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:
evazion
2018-09-29 19:05:09 -05:00
parent 929fd29c33
commit 2cc4e35cc9
2 changed files with 33 additions and 13 deletions

View File

@@ -791,7 +791,7 @@ class Post < ApplicationRecord
def filter_metatags(tags)
@pre_metatags, tags = tags.partition {|x| x =~ /\A(?:rating|parent|-parent|-?locked):/i}
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
return tags
end
@@ -841,10 +841,20 @@ class Post < ApplicationRecord
when /^(up|down)vote:(.+)$/i
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
child = Post.find($1)
child.parent_id = id
child.save
Post.numeric_attribute_matches(:id, $1).where.not(id: id).limit(10).each do |post|
post.update!(parent_id: id)
end
when /^-favgroup:(\d+)$/i
favgroup = FavoriteGroup.where("id = ?", $1.to_i).for_creator(CurrentUser.user.id).first

View File

@@ -954,16 +954,26 @@ class PostTest < ActiveSupport::TestCase
end
context "for a child" do
setup do
@child = FactoryBot.create(:post)
end
should "add and remove children" do
@children = FactoryBot.create_list(:post, 3, parent_id: nil)
should "update the parent relationships for both posts" do
@post.update_attributes(:tag_string => "aaa child:#{@child.id}")
@post.reload
@child.reload
assert_equal(@post.id, @child.parent_id)
assert(@post.has_children?)
@post.update(tag_string: "aaa child:#{@children.first.id}..#{@children.last.id}")
assert_equal(true, @post.reload.has_children?)
assert_equal(@post.id, @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:#{@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