FEATURE: Add ?status=deleted querystring

Add tests around the ?status=deleted querystring
This commit is contained in:
cpradio 2014-11-19 17:46:55 -05:00
parent 22fa46b1f2
commit 0d6e5470d4
2 changed files with 20 additions and 1 deletions

View file

@ -233,7 +233,7 @@ class TopicQuery
options.reverse_merge!(per_page: SiteSetting.topics_per_page) options.reverse_merge!(per_page: SiteSetting.topics_per_page)
# Start with a list of all topics # Start with a list of all topics
result = Topic result = Topic.unscoped
if @user if @user
result = result.joins("LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = #{@user.id.to_i})") result = result.joins("LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = #{@user.id.to_i})")
@ -286,6 +286,7 @@ class TopicQuery
notification_level = ?)', @user.id, level) notification_level = ?)', @user.id, level)
end end
require_deleted_clause = true
if status = options[:status] if status = options[:status]
case status case status
when 'open' when 'open'
@ -298,9 +299,16 @@ class TopicQuery
result = result.where('topics.visible') result = result.where('topics.visible')
when 'invisible' when 'invisible'
result = result.where('NOT topics.visible') result = result.where('NOT topics.visible')
when 'deleted'
guardian = Guardian.new(@user)
if guardian.is_staff?
result = result.where('topics.deleted_at IS NOT NULL')
require_deleted_clause = false
end
end end
end end
result = result.where('topics.deleted_at IS NULL') if require_deleted_clause
result = result.where('topics.posts_count <= ?', options[:max_posts]) if options[:max_posts].present? result = result.where('topics.posts_count <= ?', options[:max_posts]) if options[:max_posts].present?
result = result.where('topics.posts_count >= ?', options[:min_posts]) if options[:min_posts].present? result = result.where('topics.posts_count >= ?', options[:min_posts]) if options[:min_posts].present?

View file

@ -40,6 +40,17 @@ describe TopicQuery do
end end
context 'deleted filter' do
it "filters deleted topics correctly" do
topic = Fabricate(:topic, deleted_at: 1.year.ago)
TopicQuery.new(admin, status: 'deleted').list_latest.topics.size.should == 1
TopicQuery.new(moderator, status: 'deleted').list_latest.topics.size.should == 1
TopicQuery.new(user, status: 'deleted').list_latest.topics.size.should == 0
TopicQuery.new(nil, status: 'deleted').list_latest.topics.size.should == 0
end
end
context 'category filter' do context 'category filter' do
let(:category) { Fabricate(:category) } let(:category) { Fabricate(:category) }