From 9867514a782d8b582905afa8222f066c965c2210 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 20 May 2022 22:59:02 -0500 Subject: [PATCH] Fix #5177: ordfav with commentary search raises exception. --- app/logical/post_query_builder.rb | 10 ++++++++-- test/unit/post_query_builder_test.rb | 7 ++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 2e8c747a4..de602fd96 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -242,9 +242,15 @@ class PostQueryBuilder in :not children.first.negate_relation in :and - children.reduce(&:and) + joins = children.flat_map(&:joins_values) + orders = children.flat_map(&:order_values) + nodes = children.map { |child| child.joins(joins).order(orders) } + nodes.reduce(&:and) in :or - children.reduce(&:or) + joins = children.flat_map(&:joins_values) + orders = children.flat_map(&:order_values) + nodes = children.map { |child| child.joins(joins).order(orders) } + nodes.reduce(&:or) end end end diff --git a/test/unit/post_query_builder_test.rb b/test/unit/post_query_builder_test.rb index 601ec2b92..a873ab001 100644 --- a/test/unit/post_query_builder_test.rb +++ b/test/unit/post_query_builder_test.rb @@ -2,7 +2,7 @@ require 'test_helper' class PostQueryBuilderTest < ActiveSupport::TestCase def assert_tag_match(posts, query, current_user: CurrentUser.user, tag_limit: nil, **options) - assert_equal(posts.map(&:id), Post.user_tag_match(query, current_user, tag_limit: tag_limit, **options).pluck(:id)) + assert_equal(posts.map(&:id), Post.user_tag_match(query, current_user, tag_limit: tag_limit, **options).pluck("posts.id")) end def assert_search_error(query, current_user: CurrentUser.user, **options) @@ -284,6 +284,11 @@ class PostQueryBuilderTest < ActiveSupport::TestCase assert_tag_match([post2, post1], "ordfav:#{CurrentUser.user.name}") assert_tag_match([], "ordfav:does_not_exist") + + assert_tag_match([post2, post1], "ordfav:#{CurrentUser.user.name} commentary:false") + assert_tag_match([post2, post1], "ordfav:#{CurrentUser.user.name} favcount:>0") + assert_tag_match([post2, post1], "ordfav:#{CurrentUser.user.name} comments:0") + assert_tag_match([post2, post1], "ordfav:#{CurrentUser.user.name} -has:comments") end should "return posts for the pool: metatag" do