diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 287cf432d..5e42759e7 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -162,12 +162,12 @@ class TopicsController < ApplicationController params.require(:category_id) category_id = params[:category_id].to_i - topics = Topic.listable_topics.visible + visible_topics = Topic.listable_topics.visible render json: { - pinned_in_category_count: topics.where(category_id: category_id).where(pinned_globally: false).where.not(pinned_at: nil).count, - pinned_globally_count: topics.where(pinned_globally: true).where.not(pinned_at: nil).count, - banner_count: topics.where(archetype: Archetype.banner).count, + pinned_in_category_count: visible_topics.where(category_id: category_id).where(pinned_globally: false).where.not(pinned_at: nil).count, + pinned_globally_count: visible_topics.where(pinned_globally: true).where.not(pinned_at: nil).count, + banner_count: Topic.listable_topics.where(archetype: Archetype.banner).count, } end diff --git a/spec/controllers/topics_controller_spec.rb b/spec/controllers/topics_controller_spec.rb index c3f05529c..19281aebe 100644 --- a/spec/controllers/topics_controller_spec.rb +++ b/spec/controllers/topics_controller_spec.rb @@ -976,7 +976,6 @@ describe TopicsController do xhr :put, :remove_bookmarks, topic_id: post.topic_id expect(PostAction.where(user_id: user.id, post_action_type: bookmark).count).to eq(0) - end end @@ -996,8 +995,27 @@ describe TopicsController do xhr :put, :reset_new user.reload expect(user.user_stat.new_since.to_date).not_to eq(old_date.to_date) - end end + + describe "feature_stats" do + it "works" do + xhr :get, :feature_stats, category_id: 1 + + expect(response).to be_success + json = JSON.parse(response.body) + expect(json["pinned_in_category_count"]).to eq(0) + expect(json["pinned_globally_count"]).to eq(0) + expect(json["banner_count"]).to eq(0) + end + + it "allows unlisted banner topic" do + Fabricate(:topic, category_id: 1, archetype: Archetype.banner, visible: false) + + xhr :get, :feature_stats, category_id: 1 + json = JSON.parse(response.body) + expect(json["banner_count"]).to eq(1) + end + end end