mirror of
https://github.com/discourse/discourse.git
synced 2025-06-03 19:39:30 +08:00
FEATURE: add advanced order to search (#10385)
Similar to `advanced_filter` I introduced `advanced_order`. I needed a new option because default orders are evaluated after advanced_filter so I couldn't use it. Also, that part is a little bit more generic ``` elsif word =~ /order:\w+/ @order = word.gsub('order:', '').to_sym nil ``` After those changes, I can use them in plugins in this way: ``` Search.advanced_order(:votes) do |posts| posts.reorder("COALESCE((SELECT dvvc.counter FROM discourse_voting_vote_counters dvvc WHERE dvvc.topic_id = subquery.topic_id), 0) DESC") end ```
This commit is contained in:

committed by
GitHub

parent
ab2b6f8dea
commit
12a00d6dc5
@ -1690,4 +1690,24 @@ describe Search do
|
||||
end
|
||||
end
|
||||
|
||||
context 'plugin extensions' do
|
||||
let!(:post0) { Fabricate(:post, raw: 'this is the first post about advanced filter with length more than 50 chars') }
|
||||
let!(:post1) { Fabricate(:post, raw: 'this is the second post about advanced filter') }
|
||||
|
||||
it 'allows to define custom filter' do
|
||||
expect(Search.new("advanced").execute.posts).to eq([post1, post0])
|
||||
Search.advanced_filter(/^min_chars:(\d+)$/) do |posts, match|
|
||||
posts.where("(SELECT LENGTH(p2.raw) FROM posts p2 WHERE p2.id = posts.id) >= ?", match.to_i)
|
||||
end
|
||||
expect(Search.new("advanced min_chars:50").execute.posts).to eq([post0])
|
||||
end
|
||||
|
||||
it 'allows to define custom order' do
|
||||
expect(Search.new("advanced").execute.posts).to eq([post1, post0])
|
||||
Search.advanced_order(:chars) do |posts|
|
||||
posts.reorder("(SELECT LENGTH(raw) FROM posts WHERE posts.topic_id = subquery.topic_id) DESC")
|
||||
end
|
||||
expect(Search.new("advanced order:chars").execute.posts).to eq([post0, post1])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user