mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Refactored two class methods into scopes (to achieve a better 'rails way')
Also added some tests to the methods
This commit is contained in:
parent
3898d90142
commit
24e32092e7
@ -94,6 +94,10 @@ class Topic < ActiveRecord::Base
|
|||||||
|
|
||||||
scope :by_newest, order('topics.created_at desc, topics.id desc')
|
scope :by_newest, order('topics.created_at desc, topics.id desc')
|
||||||
|
|
||||||
|
scope :visible, where(visible: true)
|
||||||
|
|
||||||
|
scope :created_since, lambda { |time_ago| where('created_at > ?', time_ago) }
|
||||||
|
|
||||||
# Helps us limit how many favorites can be made in a day
|
# Helps us limit how many favorites can be made in a day
|
||||||
class FavoriteLimiter < RateLimiter
|
class FavoriteLimiter < RateLimiter
|
||||||
def initialize(user)
|
def initialize(user)
|
||||||
@ -206,13 +210,7 @@ class Topic < ActiveRecord::Base
|
|||||||
meta_data[key.to_s]
|
meta_data[key.to_s]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.visible
|
|
||||||
where(visible: true)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.created_since(time_ago)
|
|
||||||
where("created_at > ?", time_ago)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.listable_count_per_day(sinceDaysAgo=30)
|
def self.listable_count_per_day(sinceDaysAgo=30)
|
||||||
listable_topics.where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
|
listable_topics.where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
|
||||||
|
@ -966,6 +966,33 @@ describe Topic do
|
|||||||
Topic.by_newest.should == [c,b,d,a]
|
Topic.by_newest.should == [c,b,d,a]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#created_since' do
|
||||||
|
it 'returns topics created after some date' do
|
||||||
|
now = Time.now
|
||||||
|
a = Fabricate(:topic, created_at: now - 2.minutes)
|
||||||
|
b = Fabricate(:topic, created_at: now - 1.minute)
|
||||||
|
c = Fabricate(:topic, created_at: now)
|
||||||
|
d = Fabricate(:topic, created_at: now + 1.minute)
|
||||||
|
e = Fabricate(:topic, created_at: now + 2.minutes)
|
||||||
|
Topic.created_since(now).should_not include a
|
||||||
|
Topic.created_since(now).should_not include b
|
||||||
|
Topic.created_since(now).should_not include c
|
||||||
|
Topic.created_since(now).should include d
|
||||||
|
Topic.created_since(now).should include e
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#visible' do
|
||||||
|
it 'returns topics set as visible' do
|
||||||
|
a = Fabricate(:topic, visible: false)
|
||||||
|
b = Fabricate(:topic, visible: true)
|
||||||
|
c = Fabricate(:topic, visible: true)
|
||||||
|
Topic.visible.should_not include a
|
||||||
|
Topic.visible.should include b
|
||||||
|
Topic.visible.should include c
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'auto-close' do
|
describe 'auto-close' do
|
||||||
|
Loading…
Reference in New Issue
Block a user