FEATURE: Allow searching topics in group assigned tab (#97)

FEATURE: Allow searching topics in group assigned tab (#97)

diff --git a/assets/javascripts/discourse-assign/controllers/group-assigned-show.js.es6 b/assets/javascripts/discourse-assign/controllers/group-assigned-show.js.es6
index 059a2fa..f0d2c3d 100644
--- a/assets/javascripts/discourse-assign/controllers/group-assigned-show.js.es6
+++ b/assets/javascripts/discourse-assign/controllers/group-assigned-show.js.es6
@@ -3,9 +3,12 @@ import UserTopicsList from "discourse/controllers/user-topics-list";
 export default UserTopicsList.extend({
   user: Ember.inject.controller(),
   taskActions: Ember.inject.service(),
-  queryParams: ["order", "ascending"],
   order: null,
   ascending: false,
+  searchTerm: null,
+  q: "",
+
+  queryParams: ["order", "ascending", "q"],
 
   actions: {
     unassign(topic) {
@@ -25,6 +28,9 @@ export default UserTopicsList.extend({
         this.setProperties({ order: sortBy, ascending: false });
         this.model.refreshSort(sortBy, false);
       }
+    },
+    search() {
+      this.set("q", this.searchTerm);
     }
   }
 });
diff --git a/assets/javascripts/discourse-assign/routes/group-assigned-show.js.es6 b/assets/javascripts/discourse-assign/routes/group-assigned-show.js.es6
index f9c236b..4552ab4 100644
--- a/assets/javascripts/discourse-assign/routes/group-assigned-show.js.es6
+++ b/assets/javascripts/discourse-assign/routes/group-assigned-show.js.es6
@@ -4,7 +4,8 @@ import { findOrResetCachedTopicList } from "discourse/lib/cached-topic-list";
 export default DiscourseRoute.extend({
   queryParams: {
     order: { refreshModel: true },
-    ascending: { refreshModel: true }
+    ascending: { refreshModel: true },
+    q: { refreshModel: true }
   },
 
   beforeModel(transition) {
@@ -32,7 +33,8 @@ export default DiscourseRoute.extend({
           filter: filter,
           params: {
             order: params.order,
-            ascending: params.ascending
+            ascending: params.ascending,
+            q: params.q
           }
         });
   },
diff --git a/assets/javascripts/discourse/templates/group-topics-list.hbs b/assets/javascripts/discourse/templates/group-topics-list.hbs
index 7746af7..979ca0d 100644
--- a/assets/javascripts/discourse/templates/group-topics-list.hbs
+++ b/assets/javascripts/discourse/templates/group-topics-list.hbs
@@ -1,3 +1,15 @@
+<div class="form-horizontal bookmark-search-form">
+  {{input type="text"
+      value=searchTerm
+      enter=(action "search")
+      placeholder=(i18n "discourse_assign.topic_search_placeholder")
+      autocomplete="discourse"}}
+  {{d-button
+      class="btn-primary"
+      enter=(action "search")
+      type="button"
+      icon="search"}}
+</div>
 {{#load-more class="paginated-topics-list" selector=".paginated-topics-list .topic-list tr" action=(action "loadMore")}}
   {{basic-assigned-topic-list topicList=model
                               hideCategory=hideCategory
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 261eec9..e31e653 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -11,6 +11,7 @@ en:
       add_unassigned_filter: "Add 'unassigned' filter to category"
       cant_act: "You cannot act on flags that have been assigned to other users"
       cant_act_unclaimed: "You must claim this topic before acting on flags."
+      topic_search_placeholder: "Search topics by title or post content"
       sidebar_name_filter_placeholder: "Name/Username"
       assigned: "Assigned"
       group_everyone: "Everyone"
diff --git a/plugin.rb b/plugin.rb
index 808b439..95c0910 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -221,11 +221,22 @@ after_initialize do
 
     list = apply_ordering(list, options)
 
+    list = list.merge(secure)
+
+    if options[:q].present?
+      term = options[:q]
+      ts_query = Search.ts_query(term: term)
+
+      list = list
+        .joins("LEFT JOIN topic_search_data ON topic_search_data.topic_id=topics.id")
+        .where(
+          "#{ts_query} @@ topic_search_data.search_data"
+        )
+    end
+
     list = list.offset(per_page_setting * options[:page])
       .limit(per_page_setting)
 
-    list = list.merge(secure)
-
     create_list(:assigned, { unordered: true }, list)
   end
 
@@ -240,6 +251,7 @@ after_initialize do
     list_opts[:page] = page
     list_opts[:ascending] = params[:ascending]
     list_opts[:order] = params[:order]
+    list_opts[:q] = params[:q] if params[:q]
 
     list = generate_list_for("messages_assigned", user, list_opts)
 
@@ -262,11 +274,22 @@ after_initialize do
 
     list = apply_ordering(list, options)
 
+    list = list.merge(secure)
+
+    if options[:q].present?
+      term = options[:q]
+      ts_query = Search.ts_query(term: term)
+
+      list = list
+        .joins("LEFT JOIN topic_search_data ON topic_search_data.topic_id=topics.id")
+        .where(
+          "#{ts_query} @@ topic_search_data.search_data"
+        )
+    end
+
     list = list.offset(per_page_setting * options[:page])
       .limit(per_page_setting)
 
-    list = list.merge(secure)
-
     create_list(:assigned, { unordered: true }, list)
   end
 
@@ -283,6 +306,7 @@ after_initialize do
     list_opts[:page] = page
     list_opts[:ascending] = params[:ascending]
     list_opts[:order] = params[:order]
+    list_opts[:q] = params[:q] if params[:q]
 
     list = generate_list_for("group_topics_assigned", group, list_opts)
 
diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rb
index 88b0777..413af9c 100644
--- a/spec/requests/list_controller_spec.rb
+++ b/spec/requests/list_controller_spec.rb
@@ -57,8 +57,8 @@ describe ListController do
     before do
       add_to_assign_allowed_group(user)
 
-      TopicAssigner.new(post1.topic, user).assign(user)
-      TopicAssigner.new(post2.topic, user).assign(user2)
+      TopicAssigner.new(topic1, user).assign(user)
+      TopicAssigner.new(topic2, user).assign(user2)
 
       sign_in(user)
     end
@@ -137,9 +137,9 @@ describe ListController do
       topic2.posts_count = 1
       topic3.posts_count = 5
 
-      topic1.reload
-      topic2.reload
-      topic3.reload
+      topic1.save!
+      topic2.save!
+      topic3.save!
 
       get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json?order=posts"
       expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic2.id, topic1.id, topic3.id])
@@ -194,6 +194,67 @@ describe ListController do
     end
   end
 
+  context 'filtering of topics as per parameter' do
+    include_context 'A group that is allowed to assign'
+
+    fab!(:post1) { Fabricate(:post) }
+    fab!(:post2) { Fabricate(:post) }
+    fab!(:post3) { Fabricate(:post) }
+    fab!(:topic1) { post1.topic }
+    fab!(:topic2) { post2.topic }
+    fab!(:topic3) { post3.topic }
+
+    before do
+      SearchIndexer.enable
+
+      add_to_assign_allowed_group(user)
+      add_to_assign_allowed_group(user2)
+
+      TopicAssigner.new(post1.topic, user).assign(user)
+      TopicAssigner.new(post2.topic, user).assign(user2)
+      TopicAssigner.new(post3.topic, user).assign(user)
+
+      sign_in(user)
+    end
+
+    after { SearchIndexer.disable }
+
+    it 'returns topics as per filter for #group_topics_assigned' do
+      topic1.title = 'QUnit testing is love'
+      topic2.title = 'RSpec testing is too fun'
+      topic3.title = 'Testing is main part of programming'
+
+      topic1.save!
+      topic2.save!
+      topic3.save!
+
+      get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { q: 'Testing' }
+      expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic1.id, topic2.id, topic3.id])
+
+      get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { q: 'RSpec' }
+      expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic2.id])
+

[... diff too long, it was truncated ...]

GitHub sha: 069adc1a

1 Like

This commit appears in #97 which was merged by davidtaylorhq.