From 948a545cb164f9e2c45b3c5d067c8d205c31a868 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 6 Nov 2013 15:05:06 -0500 Subject: [PATCH] Category descriptions should not appear in digests --- app/models/topic.rb | 24 ++++++++++++++++-------- spec/models/topic_spec.rb | 21 ++++++++++++++++++++- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index 560c0b5af..07113b6da 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -5,6 +5,7 @@ require_dependency 'rate_limiter' require_dependency 'text_sentinel' require_dependency 'text_cleaner' require_dependency 'trashable' +require_dependency 'archetype' class Topic < ActiveRecord::Base include ActionView::Helpers::SanitizeHelper @@ -228,14 +229,21 @@ class Topic < ActiveRecord::Base # Returns hot topics since a date for display in email digest. def self.for_digest(user, since) - Topic - .visible - .secured(Guardian.new(user)) - .where(closed: false, archived: false) - .created_since(since) - .listable_topics - .order(:percent_rank) - .limit(100) + topics = Topic + .visible + .secured(Guardian.new(user)) + .where(closed: false, archived: false) + .created_since(since) + .listable_topics + .order(:percent_rank) + .limit(100) + + category_topic_ids = Category.pluck(:topic_id).compact! + if category_topic_ids.present? + topics = topics.where("id NOT IN (?)", category_topic_ids) + end + + topics end def update_meta_data(data) diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 0a82f14c4..eab083ab7 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -1116,6 +1116,25 @@ describe Topic do end end + describe 'for_digest' do + let(:user) { Fabricate.build(:user) } + + it "returns none when there are no topics" do + Topic.for_digest(user, 1.year.ago).should be_blank + end + + it "doesn't return category topics" do + Fabricate(:category) + Topic.for_digest(user, 1.year.ago).should be_blank + end + + it "returns regular topics" do + topic = Fabricate(:topic) + Topic.for_digest(user, 1.year.ago).should == [topic] + end + + end + describe 'secured' do it 'can remove secure groups' do category = Fabricate(:category, read_restricted: true) @@ -1127,7 +1146,7 @@ describe Topic do # for_digest Topic.for_digest(Fabricate(:user), 1.year.ago).count.should == 0 - Topic.for_digest(Fabricate(:admin), 1.year.ago).count.should == 2 + Topic.for_digest(Fabricate(:admin), 1.year.ago).count.should == 1 end end