diff --git a/app/models/topic.rb b/app/models/topic.rb index e34e64f..31800ab 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -42,7 +42,13 @@ class Topic < ActiveRecord::Base acts_as_readable def self.recent_topics - Post.order("id desc").select("DISTINCT topic_id").limit(5).map(&:topic) + Post.joins("LEFT OUTER JOIN topics ON topics.id = posts.topic_id") + .joins("LEFT OUTER JOIN forums on forums.id = topics.forum_id") + .joins("LEFT OUTER JOIN forumers on forums.id = forumers.forum_id") + .order("posts.id desc") + .where("forumers.id IS NULL") + .select("DISTINCT topic_id") + .limit(5).map(&:topic) end def to_s diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 836e9cb..e9f740c 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -29,7 +29,7 @@ describe Topic do end describe ".recent_topics" do - before(:all) do + before(:each) do 5.times do topic = create :topic 3.times { create :post, topic: topic } @@ -40,5 +40,11 @@ describe Topic do expect(recent_topics.length).to eq(5) expect(recent_topics.map(&:id).uniq.length).to eq(5) end + it "does not return posts from restricted forums" do + restricted_topic = create :topic, title: "Restricted" + create :forumer, forum: restricted_topic.forum + create :post, topic: restricted_topic + expect(Topic.recent_topics).to_not include(restricted_topic) + end end end