FEATURE: status:open and status:closed magic search strings
This commit is contained in:
parent
c41d47b8f5
commit
3a76dd3463
|
@ -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?
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue