Update SQL query

This commit is contained in:
Urban Rotnik 2020-09-17 13:15:40 +02:00
parent 777afd0e9b
commit 462fb12a41
2 changed files with 15 additions and 17 deletions

View file

@ -2,25 +2,22 @@
module SearchableByNameModel
extend ActiveSupport::Concern
# rubocop:disable Metrics/BlockLength
included do
def self.search_by_name(user, teams = [], query = nil, options = {})
return if user.blank? || teams.blank?
viewable_by_user(user, teams)
.where_attributes_like("#{table_name}.name", query, options)
.limit(Constants::SEARCH_LIMIT)
end
def self.search_by_name_all(user, teams = [], query = nil)
return if user.blank? || teams.blank?
sql_q = viewable_by_user(user, teams)
# Remove non-breaking space, &nbsp
query_array = query.gsub(/[[:space:]]+/, ' ').split(' ')
if query_array.any?
sql_q = sql_q.where("LOWER(#{table_name}.name) like all (array[?])", query_array.map { |c| "%#{c.downcase}%" })
if options[:intersect]
query_array = query.gsub(/[[:space:]]+/, ' ').split(' ')
query_array.each do |string|
sql_q = sql_q.where("trim_html_tags(#{table_name}.name) ILIKE ?", "%#{string}%")
end
else
sql_q = sql_q.where_attributes_like("#{table_name}.name", query, options)
end
sql_q.limit(Constants::SEARCH_LIMIT)
end
@ -55,4 +52,5 @@ module SearchableByNameModel
end
end
end
# rubocop:enable Metrics/BlockLength
end

View file

@ -14,7 +14,7 @@ class SmartAnnotation
def my_modules
# Search tasks
MyModule.search_by_name_all(@current_user, @current_team, @query).active
MyModule.search_by_name(@current_user, @current_team, @query, intersect: true).active
.joins(experiment: :project)
.where(projects: { archived: false }, experiments: { archived: false })
.limit(Constants::ATWHO_SEARCH_LIMIT + 1)
@ -22,14 +22,14 @@ class SmartAnnotation
def projects
# Search projects
Project.search_by_name_all(@current_user, @current_team, @query)
Project.search_by_name(@current_user, @current_team, @query, intersect: true)
.where(archived: false)
.limit(Constants::ATWHO_SEARCH_LIMIT + 1)
end
def experiments
# Search experiments
Experiment.search_by_name_all(@current_user, @current_team, @query)
Experiment.search_by_name(@current_user, @current_team, @query, intersect: true)
.joins(:project)
.where(projects: { archived: false }, experiments: { archived: false })
.limit(Constants::ATWHO_SEARCH_LIMIT + 1)
@ -62,7 +62,7 @@ class SmartAnnotation
res = RepositoryRow
.active
.where(repository: repository)
.search_by_name_all(@current_user, @current_team, @query)
.search_by_name(@current_user, @current_team, @query, intersect: true)
.limit(Constants::ATWHO_SEARCH_LIMIT + 1)
rep_items_list = []
splitted_name = repository.name.gsub(/[^0-9a-z ]/i, '').split