FEATURE: status:open and status:closed magic search strings

This commit is contained in:
Sam 2014-09-03 21:54:10 +10:00
parent c41d47b8f5
commit 3a76dd3463
2 changed files with 46 additions and 0 deletions

View File

@ -95,6 +95,7 @@ class Search
end
def initialize(term, opts=nil)
term = process_advanced_search!(term)
if term.present?
@term = Search.prepare_data(term.to_s)
@original_term = PG::Connection.escape_string(@term)
@ -140,6 +141,21 @@ class Search
private
def process_advanced_search!(term)
term.to_s.split(/\s+/).map do |word|
if word == 'status:open'
@status = :open
nil
elsif word == 'status:closed'
@status = :closed
nil
else
word
end
end.compact.join(' ')
end
def find_grouped_results
if @results.type_filter.present?
@ -238,6 +254,12 @@ class Search
posts = posts.where("post_search_data.search_data @@ #{ts_query}")
end
if @status == :open
posts = posts.where('NOT topics.closed AND NOT topics.archived')
elsif @status == :closed
posts = posts.where('topics.closed OR topics.archived')
end
# If we have a search context, prioritize those posts first
if @search_context.present?

View File

@ -301,5 +301,29 @@ describe Search do
end
end
describe 'Advanced search' do
it 'can find by status' do
post = Fabricate(:post, raw: 'hi this is a test 123 123')
topic = post.topic
Search.execute('test status:closed').posts.length.should == 0
Search.execute('test status:open').posts.length.should == 1
topic.closed = true
topic.save
Search.execute('test status:closed').posts.length.should == 1
Search.execute('test status:open').posts.length.should == 0
topic.archived = true
topic.closed = false
topic.save
Search.execute('test status:closed').posts.length.should == 1
Search.execute('test status:open').posts.length.should == 0
end
end
end