FIX: order:latest not working for search within topic

FEATURE: use 'l' as a shorthand for order:latest
This commit is contained in:
Sam 2017-05-24 11:24:41 -04:00
parent 0756602dfa
commit f12490eae0
2 changed files with 9 additions and 3 deletions

View File

@ -474,7 +474,7 @@ class Search
end
end
if word == 'order:latest'
if word == 'order:latest' || word == 'l'
@order = :latest
nil
elsif word == 'order:latest_topic'
@ -664,7 +664,7 @@ class Search
posts = posts.where("topics.category_id in (?)", category_ids)
elsif @search_context.is_a?(Topic)
posts = posts.where("topics.id = #{@search_context.id}")
.order("posts.post_number")
.order("posts.post_number #{@order == :latest ? "DESC" : ""}")
end
end
@ -673,7 +673,7 @@ class Search
if opts[:aggregate_search]
posts = posts.order("MAX(posts.created_at) DESC")
else
posts = posts.order("posts.created_at DESC")
posts = posts.reorder("posts.created_at DESC")
end
elsif @order == :latest_topic
if opts[:aggregate_search]
@ -710,6 +710,7 @@ class Search
else
posts = posts.where("(categories.id IS NULL) OR (NOT categories.read_restricted)").references(:categories)
end
posts.limit(limit)
end

View File

@ -253,6 +253,9 @@ describe Search do
results = Search.execute('posting', search_context: post1.topic)
expect(results.posts.map(&:id)).to eq([post1.id, post2.id, post3.id, post4.id])
results = Search.execute('posting l', search_context: post1.topic)
expect(results.posts.map(&:id)).to eq([post4.id, post3.id, post2.id, post1.id])
# stop words should work
results = Search.execute('this', search_context: post1.topic)
expect(results.posts.length).to eq(4)
@ -633,6 +636,8 @@ describe Search do
expect(Search.execute('sam').posts.map(&:id)).to eq([post1.id, post2.id])
expect(Search.execute('sam order:latest').posts.map(&:id)).to eq([post2.id, post1.id])
expect(Search.execute('sam l').posts.map(&:id)).to eq([post2.id, post1.id])
expect(Search.execute('l sam').posts.map(&:id)).to eq([post2.id, post1.id])
end
it 'can order by topic creation' do