FIX: Search advanced order after changes in core. (#79)

FIX: Search advanced order after changes in core. (#79)

See https://github.com/discourse/discourse/commit/4b3f65bb26c9d72f85b10f69956cd8ab75978d0a

diff --git a/plugin.rb b/plugin.rb
index 491b188..a00aaf1 100755
--- a/plugin.rb
+++ b/plugin.rb
@@ -112,7 +112,7 @@ after_initialize do
     # TODO: Remove if check once Discourse 2.6 is stable
     if respond_to?(:register_search_advanced_order)
       register_search_advanced_order(:votes) do |posts|
-        posts.reorder("COALESCE((SELECT dvtvc.votes_count FROM discourse_voting_topic_vote_count dvtvc WHERE dvtvc.topic_id = subquery.topic_id), 0) DESC")
+        posts.reorder("COALESCE((SELECT dvtvc.votes_count FROM discourse_voting_topic_vote_count dvtvc WHERE dvtvc.topic_id = topics.id), 0) DESC")
       end
     end
 
diff --git a/spec/requests/search_controller_spec.rb b/spec/requests/search_controller_spec.rb
new file mode 100644
index 0000000..b345f4e
--- /dev/null
+++ b/spec/requests/search_controller_spec.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe SearchController do
+  fab!(:user) { Fabricate(:user) }
+
+  fab!(:category) { Fabricate(:category) }
+
+  fab!(:topic) { Fabricate(:topic, category: category) }
+  fab!(:topic_2) { Fabricate(:topic, category: category) }
+
+  fab!(:post_1) do
+    SearchIndexer.enable
+    Fabricate(:post, topic: topic, raw: 'this is an awesome topic')
+  end
+
+  fab!(:post_2) do
+    SearchIndexer.enable
+    Fabricate(:post, topic: topic_2, raw: 'this is an awesome topic')
+  end
+
+  before do
+    DiscourseVoting::CategorySetting.create!(category: category)
+    SiteSetting.voting_enabled = true
+    sign_in(user)
+  end
+
+  it 'can search for posts ordered by votes' do
+    post "/voting/vote.json", params: { topic_id: post_2.topic_id }
+
+    expect(response.status).to eq(200)
+
+    get "/search/query.json", params: {
+      term: 'awesome order:votes',
+    }
+
+    expect(response.status).to eq(200)
+
+    data = response.parsed_body
+
+    expect(data['posts'].length).to eq(2)
+    expect(data['posts'][0]['id']).to eq(post_2.id)
+    expect(data['posts'][1]['id']).to eq(post_1.id)
+  end
+
+end

GitHub sha: ca82bab1

This commit appears in #79 which was approved by lis2. It was merged by tgxworld.