Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: allow filtering posts report by multiple categories (#23669)

Arpit Jalan3669723a8682
diff --git a/app/models/post.rb b/app/models/post.rb
index 0801a77f731..d39ffaf7115 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -889,7 +889,7 @@ class Post < ActiveRecord::Base
if include_subcategories
result = result.where("topics.category_id IN (?)", Category.subcategory_ids(category_id))
else
- result = result.where("topics.category_id = ?", category_id)
+ result = result.where("topics.category_id IN (?)", category_id)
end
end
if group_ids
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index 564ba925a8d..f251b8ef6dd 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -2166,6 +2166,32 @@ RSpec.describe Post do
expect(Post.public_posts_count_per_day(10.days.ago, 5.days.ago)).to be_empty
end
+ it "returns the correct number of public posts per day with category filter" do
+ category = Fabricate(:category)
+ another_category = Fabricate(:category)
+
+ topic = Fabricate(:topic, category: category)
+ another_topic = Fabricate(:topic, category: another_category)
+
+ Fabricate(:post, topic: topic, created_at: 6.days.ago)
+ Fabricate(:post, topic: topic, created_at: 7.days.ago)
+ Fabricate(:post, topic: another_topic, created_at: 6.days.ago)
+ Fabricate(:post, topic: another_topic, created_at: 7.days.ago)
+
+ expect(Post.public_posts_count_per_day(10.days.ago, 5.days.ago, category.id)).to eq(
+ 6.days.ago.to_date => 1,
+ 7.days.ago.to_date => 1,
+ )
+
+ expect(
+ Post.public_posts_count_per_day(
+ 10.days.ago,
+ 5.days.ago,
+ [category.id, another_category.id],
+ ),
+ ).to eq(6.days.ago.to_date => 2, 7.days.ago.to_date => 2)
+ end
+
it "returns the correct number of public posts per day with group filter" do
user = Fabricate(:user)
group_user = Fabricate(:user)