Added forumer exclusion on recent_topics

This commit is contained in:
Chris Blanchard 2015-08-20 19:33:22 +01:00
parent e34d21dd00
commit 5f53893756
2 changed files with 14 additions and 2 deletions

View file

@ -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

View file

@ -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