FIX: filter by "nobody" was broken (#176)

FIX: filter by “nobody” was broken (#176)

Meta ref: Filter by nobody seems broken - bug - Discourse Meta

diff --git a/plugin.rb b/plugin.rb
index 60dcbf3..bb6fa4e 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -211,7 +211,8 @@ after_initialize do
 
       if user_id || special
         if username == "nobody"
-          results = results.joins("LEFT JOIN assignments a ON a.topic_id = topics.id AND a.assigned_to_id IS NULL")
+          results = results.joins("LEFT JOIN assignments a ON a.topic_id = topics.id")
+            .where("a.assigned_to_id IS NULL")
         else
           if username == "*"
             filter = "a.assigned_to_id IS NOT NULL"
diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb
index 09519e0..b8242ee 100644
--- a/spec/components/topic_query_spec.rb
+++ b/spec/components/topic_query_spec.rb
@@ -141,6 +141,19 @@ describe TopicQuery do
     end
   end
 
+  context 'assigned' do
+    it "filters assigned topics correctly" do
+      assigned_topic = Fabricate(:post).topic
+      unassigned_topic = Fabricate(:topic)
+
+      TopicAssigner.new(assigned_topic, user).assign(user)
+      query = TopicQuery.new(user, assigned: 'nobody').list_latest
+
+      expect(query.topics.length).to eq(1)
+      expect(query.topics.first).to eq(unassigned_topic)
+    end
+  end
+
   def assign_to(topic, user)
     topic.tap do |t|
       t.posts << Fabricate(:post)

GitHub sha: 4024eb078c8f649945a0355a5711c45e7129edf4

This commit appears in #176 which was approved by CvX. It was merged by techAPJ.