Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

replace hand written autocomplete filtering by typeahead

ulferts23fdfe6d4841
diff --git a/app/controllers/work_packages/auto_completes_controller.rb b/app/controllers/work_packages/auto_completes_controller.rb
index 44a8e3daf1c..add661381a3 100644
--- a/app/controllers/work_packages/auto_completes_controller.rb
+++ b/app/controllers/work_packages/auto_completes_controller.rb
@@ -30,7 +30,7 @@ require 'rack/utils'
class WorkPackages::AutoCompletesController < ApplicationController
def index
- @work_packages = work_package_with_id | work_packages_by_subject_or_id
+ @work_packages = work_packages_matching_query_prop
respond_to do |format|
format.json { render request.format.to_sym => wp_hashes_with_string(@work_packages) }
@@ -39,50 +39,20 @@ class WorkPackages::AutoCompletesController < ApplicationController
private
- def work_package_with_id
- query_term = params[:q].to_s
-
- if query_term =~ /\A\d+\z/
- work_package_scope
- .visible.where(id: query_term.to_i)
- .order("#{WorkPackage.table_name}.updated_at DESC") # :id does not work because...
- else
- []
+ def work_packages_matching_query_prop
+ Query.new.tap do |query|
+ query.add_filter(:typeahead, '**', params[:q])
+ query.sort_criteria = [%i[updated_at desc]]
+ query.include_subprojects = true
end
- end
-
- def work_packages_by_subject_or_id
- query_term_sql = subject_or_id_query(params[:q].to_s)
-
- work_package_scope
- .visible
- .where(query_term_sql)
- .order("#{WorkPackage.table_name}.updated_at DESC") # :id does not work because...
+ .results
+ .work_packages
.limit(10)
- .includes(:type)
end
def wp_hashes_with_string(work_packages)
work_packages.map do |work_package|
- wp_hash = Hash.new
- work_package.attributes.each { |key, value| wp_hash[key] = value }
- wp_hash['to_s'] = work_package.to_s
- wp_hash
+ work_package.attributes.merge('to_s' => work_package.to_s)
end
end
-
- def subject_or_id_query(query_term)
- if query_term =~ /\A\d+\z/
- ["#{WorkPackage.table_name}.subject LIKE :q OR
- CAST(#{WorkPackage.table_name}.id AS CHAR(13)) LIKE :q",
- { q: "%#{query_term}%" }]
- else
- ["LOWER(#{WorkPackage.table_name}.subject) LIKE :q",
- { q: "%#{query_term.downcase}%" }]
- end
- end
-
- def work_package_scope
- WorkPackage.all
- end
end