2019-02-26 11:01:15 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module SearchableByNameModel
|
|
|
|
extend ActiveSupport::Concern
|
2020-09-17 13:15:40 +02:00
|
|
|
# rubocop:disable Metrics/BlockLength
|
2019-02-26 11:01:15 +01:00
|
|
|
included do
|
|
|
|
def self.search_by_name(user, teams = [], query = nil, options = {})
|
|
|
|
return if user.blank? || teams.blank?
|
2019-04-09 09:08:27 +02:00
|
|
|
|
2020-09-15 15:47:46 +02:00
|
|
|
sql_q = viewable_by_user(user, teams)
|
2020-09-17 13:15:40 +02:00
|
|
|
|
|
|
|
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)
|
2020-09-15 15:47:46 +02:00
|
|
|
end
|
2020-09-17 13:15:40 +02:00
|
|
|
|
2020-09-15 15:47:46 +02:00
|
|
|
sql_q.limit(Constants::SEARCH_LIMIT)
|
|
|
|
end
|
2019-02-26 11:01:15 +01:00
|
|
|
end
|
2020-09-17 13:15:40 +02:00
|
|
|
# rubocop:enable Metrics/BlockLength
|
2019-02-26 11:01:15 +01:00
|
|
|
end
|