From b1335616dd44166bbe94e895f85f50ea28b34eaf Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 11 Dec 2018 18:10:20 -0600 Subject: [PATCH] search: add {flag,appeal,approval,replacement,child,pool}_count metatags (#4004). --- app/models/post.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ app/models/tag.rb | 4 ++++ 2 files changed, 50 insertions(+) diff --git a/app/models/post.rb b/app/models/post.rb index c928ac07f..827ae84c1 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1625,6 +1625,52 @@ class Post < ApplicationRecord relation end + def with_flag_stats + relation = left_outer_joins(:flags).group(:id).select("posts.*") + relation = relation.select("COUNT(post_flags.id) AS flag_count") + relation = relation.select("COUNT(post_flags.id) FILTER (WHERE post_flags.is_resolved = TRUE) AS resolved_flag_count") + relation = relation.select("COUNT(post_flags.id) FILTER (WHERE post_flags.is_resolved = FALSE) AS unresolved_flag_count") + relation + end + + def with_appeal_stats + relation = left_outer_joins(:appeals).group(:id).select("posts.*") + relation = relation.select("COUNT(post_appeals.id) AS appeal_count") + relation + end + + def with_approval_stats + relation = left_outer_joins(:approvals).group(:id).select("posts.*") + relation = relation.select("COUNT(post_approvals.id) AS approval_count") + relation + end + + def with_replacement_stats + relation = left_outer_joins(:replacements).group(:id).select("posts.*") + relation = relation.select("COUNT(post_replacements.id) AS replacement_count") + relation + end + + def with_child_stats + relation = left_outer_joins(:children).group(:id).select("posts.*") + relation = relation.select("COUNT(children_posts.id) AS child_count") + relation = relation.select("COUNT(children_posts.id) FILTER (WHERE children_posts.is_deleted = TRUE) AS deleted_child_count") + relation = relation.select("COUNT(children_posts.id) FILTER (WHERE children_posts.is_deleted = FALSE) AS active_child_count") + relation + end + + def with_pool_stats + pool_posts = Pool.joins("CROSS JOIN unnest(post_ids) AS post_id").select(:id, :is_deleted, :category, "post_id") + relation = joins("LEFT OUTER JOIN (#{pool_posts.to_sql}) pools ON pools.post_id = posts.id").group(:id).select("posts.*") + + relation = relation.select("COUNT(pools.id) AS pool_count") + relation = relation.select("COUNT(pools.id) FILTER (WHERE pools.is_deleted = TRUE) AS deleted_pool_count") + relation = relation.select("COUNT(pools.id) FILTER (WHERE pools.is_deleted = FALSE) AS active_pool_count") + relation = relation.select("COUNT(pools.id) FILTER (WHERE pools.category = 'series') AS series_pool_count") + relation = relation.select("COUNT(pools.id) FILTER (WHERE pools.category = 'collection') AS collection_pool_count") + relation + end + def with_stats(tables) return all if tables.empty? diff --git a/app/models/tag.rb b/app/models/tag.rb index 1a2176d33..a043abb3f 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -3,6 +3,10 @@ class Tag < ApplicationRecord COUNT_METATAGS = %w[ comment_count deleted_comment_count active_comment_count note_count deleted_note_count active_note_count + flag_count resolved_flag_count unresolved_flag_count + child_count deleted_child_count active_child_count + pool_count deleted_pool_count active_pool_count series_pool_count collection_pool_count + appeal_count approval_count replacement_count ] METATAGS = %w[ -user user -approver approver commenter comm noter noteupdater artcomm