From 22f81a59d125b43ed1c45f689b864d71063b34b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor=20Geonizeli?= Date: Mon, 28 Feb 2022 14:21:31 -0300 Subject: [PATCH] improve filter by terms logic --- app/services/posts_query_resolver_service.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/services/posts_query_resolver_service.rb b/app/services/posts_query_resolver_service.rb index ebebf7c..3868b2a 100644 --- a/app/services/posts_query_resolver_service.rb +++ b/app/services/posts_query_resolver_service.rb @@ -25,12 +25,16 @@ class PostsQueryResolverService def filter_by_terms(scope) result = scope.by_terms(filter[:terms]) - posts_with_quotes = result.where.not(quoted_post_id: nil) posts_without_quotes = result.where(quoted_post_id: nil) + posts_with_quotes = filter_post_that_match_with_quote(result) + posts_without_quotes.or(posts_with_quotes).order(created_at: :desc) + end + + def filter_post_that_match_with_quote(scope) + posts_with_quotes = scope.where.not(quoted_post_id: nil) quotes_ids = posts_with_quotes.pluck(:quoted_post_id) - quoted_posts = Post.where(id: quotes_ids).by_terms(filter[:terms]) - posts_without_quotes.or(quoted_posts).order(created_at: :desc) + Post.where(id: quotes_ids).by_terms(filter[:terms]) end end \ No newline at end of file