mirror of
https://github.com/ENSL/ensl.org.git
synced 2024-12-27 04:51:14 +00:00
Add filter to recent topics
This commit is contained in:
parent
d26305b523
commit
23b60878e4
3 changed files with 23 additions and 15 deletions
|
@ -42,14 +42,21 @@ class Topic < ActiveRecord::Base
|
||||||
acts_as_readable
|
acts_as_readable
|
||||||
|
|
||||||
def self.recent_topics
|
def self.recent_topics
|
||||||
Post.joins("LEFT OUTER JOIN topics ON topics.id = posts.topic_id").
|
self.find_by_sql %q{
|
||||||
joins("LEFT OUTER JOIN forums on forums.id = topics.forum_id").
|
SELECT DISTINCT topics.*
|
||||||
joins("LEFT OUTER JOIN forumers on forums.id = forumers.forum_id").
|
FROM (SELECT id, topic_id
|
||||||
order("posts.id desc").
|
FROM posts
|
||||||
where("forumers.id IS NULL").
|
ORDER BY id DESC
|
||||||
select("DISTINCT topic_id").
|
LIMIT 20) AS T
|
||||||
limit(5).
|
INNER JOIN topics
|
||||||
map(&:topic)
|
ON T.topic_id = topics.id
|
||||||
|
INNER JOIN forums
|
||||||
|
ON forums.id = topics.forum_id
|
||||||
|
LEFT OUTER JOIN forumers
|
||||||
|
ON forumers.forum_id = forums.id
|
||||||
|
WHERE forumers.id IS NULL
|
||||||
|
LIMIT 5
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
FactoryGirl.define do
|
FactoryGirl.define do
|
||||||
factory :topic do
|
factory :topic do
|
||||||
sequence(:title) { |n| "Forum Title #{n}" }
|
sequence(:title) { |n| "Topic Title #{n}" }
|
||||||
forum
|
forum
|
||||||
user
|
user
|
||||||
before(:create) do |topic|
|
before(:create) do |topic|
|
||||||
|
|
|
@ -29,16 +29,17 @@ describe Topic do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".recent_topics" do
|
describe ".recent_topics" do
|
||||||
before(:each) do
|
it "returns 5 unique, most recently posted topics" do
|
||||||
5.times do
|
topics = []
|
||||||
|
6.times do
|
||||||
topic = create :topic
|
topic = create :topic
|
||||||
|
topics << topic
|
||||||
3.times { create :post, topic: topic }
|
3.times { create :post, topic: topic }
|
||||||
end
|
end
|
||||||
end
|
|
||||||
it "returns 5 unique, most recently posted topics" do
|
|
||||||
recent_topics = Topic.recent_topics
|
recent_topics = Topic.recent_topics
|
||||||
expect(recent_topics.length).to eq(5)
|
topics.last(5).each do |topic|
|
||||||
expect(recent_topics.map(&:id).uniq.length).to eq(5)
|
expect(recent_topics).to include(topic)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
it "does not return posts from restricted forums" do
|
it "does not return posts from restricted forums" do
|
||||||
restricted_topic = create :topic, title: "Restricted"
|
restricted_topic = create :topic, title: "Restricted"
|
||||||
|
|
Loading…
Reference in a new issue