mirror of
https://github.com/ENSL/ensl.org.git
synced 2025-01-27 19:50:56 +00:00
Add more efficient recent topics class method
This commit is contained in:
parent
e94bbba48d
commit
c6589b9f1d
2 changed files with 20 additions and 0 deletions
|
@ -47,6 +47,10 @@ class Topic < ActiveRecord::Base
|
||||||
|
|
||||||
acts_as_readable
|
acts_as_readable
|
||||||
|
|
||||||
|
def self.recent_topics
|
||||||
|
Post.order('id desc').select('DISTINCT topic_id').limit(5).map(&:topic)
|
||||||
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
title
|
title
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,4 +27,20 @@ describe Topic do
|
||||||
end.to change(Topic, :count).by(1)
|
end.to change(Topic, :count).by(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".recent_topics" do
|
||||||
|
before(:all) do
|
||||||
|
5.times do
|
||||||
|
topic = create :topic
|
||||||
|
3.times do
|
||||||
|
post = create :post, topic: topic
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
it "returns 5 unique, most recently posted topics" do
|
||||||
|
recent_topics = Topic.recent_topics
|
||||||
|
expect(recent_topics.length).to eq(5)
|
||||||
|
expect(recent_topics.map(&:id).uniq.length).to eq(5)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue