REFACTOR: Use core `default_results` function (#111)
This significantly reduces the amount of logic we need to carry in the discourse-assign plugin. One side effect is that I had to rename the 'q' parameter to 'search', so that it matches core's implementation.
This commit is contained in:
		
							parent
							
								
									f6fc09ff13
								
							
						
					
					
						commit
						c4aefd5917
					
				|  | @ -9,20 +9,20 @@ export default UserTopicsList.extend({ | ||||||
|   taskActions: Ember.inject.service(), |   taskActions: Ember.inject.service(), | ||||||
|   order: "", |   order: "", | ||||||
|   ascending: false, |   ascending: false, | ||||||
|   q: "", |   search: "", | ||||||
|   bulkSelectEnabled: false, |   bulkSelectEnabled: false, | ||||||
|   selected: [], |   selected: [], | ||||||
|   canBulkSelect: alias("currentUser.staff"), |   canBulkSelect: alias("currentUser.staff"), | ||||||
| 
 | 
 | ||||||
|   queryParams: ["order", "ascending", "q"], |   queryParams: ["order", "ascending", "search"], | ||||||
| 
 | 
 | ||||||
|   @discourseComputed("q") |   @discourseComputed("search") | ||||||
|   searchTerm(q) { |   searchTerm(search) { | ||||||
|     return q; |     return search; | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|   _setSearchTerm(searchTerm) { |   _setSearchTerm(searchTerm) { | ||||||
|     this.set("q", searchTerm); |     this.set("search", searchTerm); | ||||||
|     this.refreshModel(); |     this.refreshModel(); | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|  | @ -34,7 +34,7 @@ export default UserTopicsList.extend({ | ||||||
|         params: { |         params: { | ||||||
|           order: this.order, |           order: this.order, | ||||||
|           ascending: this.ascending, |           ascending: this.ascending, | ||||||
|           q: this.q, |           search: this.search, | ||||||
|         }, |         }, | ||||||
|       }) |       }) | ||||||
|       .then((result) => this.set("model", result)) |       .then((result) => this.set("model", result)) | ||||||
|  |  | ||||||
|  | @ -24,9 +24,9 @@ export default Controller.extend({ | ||||||
|     return ascending || false; |     return ascending || false; | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|   @discourseComputed("router.currentRoute.queryParams.q") |   @discourseComputed("router.currentRoute.queryParams.search") | ||||||
|   q(q) { |   search(search) { | ||||||
|     return q || ""; |     return search || ""; | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|   @discourseComputed("site.mobileView") |   @discourseComputed("site.mobileView") | ||||||
|  |  | ||||||
|  | @ -6,18 +6,18 @@ import { INPUT_DELAY } from "discourse-common/config/environment"; | ||||||
| export default UserTopicsList.extend({ | export default UserTopicsList.extend({ | ||||||
|   user: Ember.inject.controller(), |   user: Ember.inject.controller(), | ||||||
|   taskActions: Ember.inject.service(), |   taskActions: Ember.inject.service(), | ||||||
|   queryParams: ["order", "ascending", "q"], |   queryParams: ["order", "ascending", "search"], | ||||||
|   order: "", |   order: "", | ||||||
|   ascending: false, |   ascending: false, | ||||||
|   q: "", |   search: "", | ||||||
| 
 | 
 | ||||||
|   @discourseComputed("q") |   @discourseComputed("search") | ||||||
|   searchTerm(q) { |   searchTerm(search) { | ||||||
|     return q; |     return search; | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|   _setSearchTerm(searchTerm) { |   _setSearchTerm(searchTerm) { | ||||||
|     this.set("q", searchTerm); |     this.set("search", searchTerm); | ||||||
|     this.refreshModel(); |     this.refreshModel(); | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|  | @ -29,7 +29,7 @@ export default UserTopicsList.extend({ | ||||||
|         params: { |         params: { | ||||||
|           order: this.order, |           order: this.order, | ||||||
|           ascending: this.ascending, |           ascending: this.ascending, | ||||||
|           q: this.q, |           search: this.search, | ||||||
|         }, |         }, | ||||||
|       }) |       }) | ||||||
|       .then((result) => this.set("model", result)) |       .then((result) => this.set("model", result)) | ||||||
|  |  | ||||||
|  | @ -28,7 +28,7 @@ export default DiscourseRoute.extend({ | ||||||
|           params: { |           params: { | ||||||
|             order: params.order, |             order: params.order, | ||||||
|             ascending: params.ascending, |             ascending: params.ascending, | ||||||
|             q: params.q, |             search: params.search, | ||||||
|           }, |           }, | ||||||
|         }); |         }); | ||||||
|   }, |   }, | ||||||
|  | @ -36,7 +36,7 @@ export default DiscourseRoute.extend({ | ||||||
|   setupController(controller, model) { |   setupController(controller, model) { | ||||||
|     controller.setProperties({ |     controller.setProperties({ | ||||||
|       model, |       model, | ||||||
|       searchTerm: this.currentModel.params.q, |       searchTerm: this.currentModel.params.search, | ||||||
|     }); |     }); | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -15,7 +15,7 @@ export default UserTopicListRoute.extend({ | ||||||
|         exclude_category_ids: [-1], |         exclude_category_ids: [-1], | ||||||
|         order: params.order, |         order: params.order, | ||||||
|         ascending: params.ascending, |         ascending: params.ascending, | ||||||
|         q: params.q, |         search: params.search, | ||||||
|       }, |       }, | ||||||
|     }); |     }); | ||||||
|   }, |   }, | ||||||
|  |  | ||||||
|  | @ -1,5 +1,5 @@ | ||||||
| {{#if show-avatar}} | {{#if show-avatar}} | ||||||
|   {{#link-to "group.assigned.show" filter.username_lower (query-params order=order ascending=ascending q=q)}} |   {{#link-to "group.assigned.show" filter.username_lower (query-params order=order ascending=ascending search=search)}} | ||||||
|     <div class="assign-image"> |     <div class="assign-image"> | ||||||
|       <a href={{filter.userPath}} data-user-card={{filter.username}}>{{avatar filter imageSize="large"}}</a> |       <a href={{filter.userPath}} data-user-card={{filter.username}}>{{avatar filter imageSize="large"}}</a> | ||||||
|     </div> |     </div> | ||||||
|  | @ -14,7 +14,7 @@ | ||||||
|     </div> |     </div> | ||||||
|   {{/link-to}} |   {{/link-to}} | ||||||
| {{else}} | {{else}} | ||||||
|   {{#link-to "group.assigned.show" filter (query-params order=order ascending=ascending q=q)}} |   {{#link-to "group.assigned.show" filter (query-params order=order ascending=ascending search=search)}} | ||||||
|     <div class="assign-everyone"> |     <div class="assign-everyone"> | ||||||
|       {{i18n "discourse_assign.group_everyone"}} |       {{i18n "discourse_assign.group_everyone"}} | ||||||
|     </div> |     </div> | ||||||
|  |  | ||||||
|  | @ -17,7 +17,7 @@ | ||||||
|         filter="everyone" |         filter="everyone" | ||||||
|         routeType=route_type |         routeType=route_type | ||||||
|         assignmentCount=group.assignment_count |         assignmentCount=group.assignment_count | ||||||
|         q=q |         search=search | ||||||
|         ascending=ascending |         ascending=ascending | ||||||
|         order=order}} |         order=order}} | ||||||
|       {{#each members as |member|}} |       {{#each members as |member|}} | ||||||
|  | @ -25,7 +25,7 @@ | ||||||
|           show-avatar=true |           show-avatar=true | ||||||
|           filter=member |           filter=member | ||||||
|           routeType=route_type |           routeType=route_type | ||||||
|           q=q |           search=search | ||||||
|           ascending=ascending |           ascending=ascending | ||||||
|           order=order}} |           order=order}} | ||||||
|       {{/each}} |       {{/each}} | ||||||
|  |  | ||||||
							
								
								
									
										76
									
								
								plugin.rb
								
								
								
								
							
							
						
						
									
										76
									
								
								plugin.rb
								
								
								
								
							|  | @ -20,8 +20,8 @@ load File.expand_path('../lib/discourse_assign/helpers.rb', __FILE__) | ||||||
| Discourse::Application.routes.append do | Discourse::Application.routes.append do | ||||||
|   mount ::DiscourseAssign::Engine, at: "/assign" |   mount ::DiscourseAssign::Engine, at: "/assign" | ||||||
|   get "topics/private-messages-assigned/:username" => "list#private_messages_assigned", as: "topics_private_messages_assigned", constraints: { username: ::RouteFormat.username } |   get "topics/private-messages-assigned/:username" => "list#private_messages_assigned", as: "topics_private_messages_assigned", constraints: { username: ::RouteFormat.username } | ||||||
|   get "/topics/messages-assigned/:username" => "list#messages_assigned", constraints: { username: ::RouteFormat.username } |   get "/topics/messages-assigned/:username" => "list#messages_assigned", constraints: { username: ::RouteFormat.username }, as: "messages_assigned" | ||||||
|   get "/topics/group-topics-assigned/:groupname" => "list#group_topics_assigned", constraints: { username: ::RouteFormat.username } |   get "/topics/group-topics-assigned/:groupname" => "list#group_topics_assigned", constraints: { username: ::RouteFormat.username }, as: "group_topics_assigned" | ||||||
|   get "/g/:id/assigned" => "groups#index" |   get "/g/:id/assigned" => "groups#index" | ||||||
|   get "/g/:id/assigned/:route_type" => "groups#index" |   get "/g/:id/assigned/:route_type" => "groups#index" | ||||||
| end | end | ||||||
|  | @ -233,92 +233,46 @@ after_initialize do | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   add_to_class(:topic_query, :list_messages_assigned) do |user| |   add_to_class(:topic_query, :list_messages_assigned) do |user| | ||||||
|     secure = Topic.listable_topics.secured(@guardian).or(Topic.private_messages_for_user(@user)) |     list = default_results(include_pms: true) | ||||||
|     list = joined_topic_user.where(" | 
 | ||||||
|  |     list = list.where(" | ||||||
|       topics.id IN ( |       topics.id IN ( | ||||||
|         SELECT topic_id FROM topic_custom_fields |         SELECT topic_id FROM topic_custom_fields | ||||||
|         WHERE name = 'assigned_to_id' |         WHERE name = 'assigned_to_id' | ||||||
|         AND value = ?) |         AND value = ?) | ||||||
|     ", user.id.to_s) |     ", user.id.to_s) | ||||||
|       .includes(:tags) |  | ||||||
| 
 |  | ||||||
|     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) |  | ||||||
| 
 | 
 | ||||||
|     create_list(:assigned, { unordered: true }, list) |     create_list(:assigned, { unordered: true }, list) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   add_to_class(:list_controller, :messages_assigned) do |   add_to_class(:list_controller, :messages_assigned) do | ||||||
|     page = (params[:page].to_i || 0).to_i |  | ||||||
| 
 |  | ||||||
|     user = User.find_by_username(params[:username]) |     user = User.find_by_username(params[:username]) | ||||||
|     raise Discourse::NotFound unless user |     raise Discourse::NotFound unless user | ||||||
|     raise Discourse::InvalidAccess unless current_user.can_assign? |     raise Discourse::InvalidAccess unless current_user.can_assign? | ||||||
| 
 | 
 | ||||||
|     list_opts = build_topic_list_options |     list_opts = build_topic_list_options | ||||||
|     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) |     list = generate_list_for("messages_assigned", user, list_opts) | ||||||
| 
 | 
 | ||||||
|     more_topics_url = "/topics/messages-assigned/#{params[:username]}.json?page=#{page + 1}" |     list.more_topics_url = construct_url_with(:next, list_opts) | ||||||
|     more_topics_url += "&ascending=#{params[:ascending]}&order=#{params[:order]}" if params[:order] |     list.prev_topics_url = construct_url_with(:prev, list_opts) | ||||||
| 
 | 
 | ||||||
|     list.more_topics_url = more_topics_url |  | ||||||
|     respond_with_list(list) |     respond_with_list(list) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   add_to_class(:topic_query, :list_group_topics_assigned) do |group| |   add_to_class(:topic_query, :list_group_topics_assigned) do |group| | ||||||
|     secure = Topic.listable_topics.secured(@guardian).or(Topic.private_messages_for_user(@user)) |     list = default_results(include_pms: true) | ||||||
|     list = joined_topic_user.where(" | 
 | ||||||
|  |     list = list.where(" | ||||||
|       topics.id IN ( |       topics.id IN ( | ||||||
|         SELECT topic_id FROM topic_custom_fields |         SELECT topic_id FROM topic_custom_fields | ||||||
|         WHERE name = 'assigned_to_id' |         WHERE name = 'assigned_to_id' | ||||||
|         AND value IN (SELECT user_id::varchar(255) from group_users where group_id = ?)) |         AND value IN (SELECT user_id::varchar(255) from group_users where group_id = ?)) | ||||||
|     ", group.id.to_s) |     ", group.id.to_s) | ||||||
|       .includes(:tags) |  | ||||||
| 
 |  | ||||||
|     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) |  | ||||||
| 
 | 
 | ||||||
|     create_list(:assigned, { unordered: true }, list) |     create_list(:assigned, { unordered: true }, list) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   add_to_class(:list_controller, :group_topics_assigned) do |   add_to_class(:list_controller, :group_topics_assigned) do | ||||||
|     page = (params[:page].to_i || 0).to_i |  | ||||||
| 
 |  | ||||||
|     group = Group.find_by("name = ?", params[:groupname]) |     group = Group.find_by("name = ?", params[:groupname]) | ||||||
|     guardian.ensure_can_see_group_members!(group) |     guardian.ensure_can_see_group_members!(group) | ||||||
| 
 | 
 | ||||||
|  | @ -327,17 +281,11 @@ after_initialize do | ||||||
|     raise Discourse::InvalidAccess unless group.can_show_assigned_tab? |     raise Discourse::InvalidAccess unless group.can_show_assigned_tab? | ||||||
| 
 | 
 | ||||||
|     list_opts = build_topic_list_options |     list_opts = build_topic_list_options | ||||||
|     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) |     list = generate_list_for("group_topics_assigned", group, list_opts) | ||||||
| 
 | 
 | ||||||
|     more_topics_url = "/topics/group-topics-assigned/#{params[:groupname]}.json?page=#{page + 1}" |     list.more_topics_url = construct_url_with(:next, list_opts) | ||||||
|     more_topics_url += "&ascending=#{params[:ascending]}&order=#{params[:order]}" if params[:order] |     list.prev_topics_url = construct_url_with(:prev, list_opts) | ||||||
| 
 | 
 | ||||||
|     list.more_topics_url = more_topics_url |  | ||||||
|     respond_with_list(list) |     respond_with_list(list) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -228,13 +228,13 @@ describe ListController do | ||||||
|       topic2.save! |       topic2.save! | ||||||
|       topic3.save! |       topic3.save! | ||||||
| 
 | 
 | ||||||
|       get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { q: 'Testing' } |       get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { search: 'Testing' } | ||||||
|       expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic1.id, topic2.id, topic3.id]) |       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' } |       get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { search: 'RSpec' } | ||||||
|       expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic2.id]) |       expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic2.id]) | ||||||
| 
 | 
 | ||||||
|       get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { q: 'love' } |       get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { search: 'love' } | ||||||
|       expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic1.id]) |       expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic1.id]) | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|  | @ -247,10 +247,10 @@ describe ListController do | ||||||
|       topic2.save! |       topic2.save! | ||||||
|       topic3.save! |       topic3.save! | ||||||
| 
 | 
 | ||||||
|       get "/topics/messages-assigned/#{user.username}.json", params: { q: 'Testing' } |       get "/topics/messages-assigned/#{user.username}.json", params: { search: 'Testing' } | ||||||
|       expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic1.id, topic3.id]) |       expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic1.id, topic3.id]) | ||||||
| 
 | 
 | ||||||
|       get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { q: 'love' } |       get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { search: 'love' } | ||||||
|       expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic1.id]) |       expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic1.id]) | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue