2019-02-26 18:01:15 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module SearchableByNameModel
|
|
|
|
extend ActiveSupport::Concern
|
2020-09-17 19:15:40 +08:00
|
|
|
# rubocop:disable Metrics/BlockLength
|
2019-02-26 18:01:15 +08:00
|
|
|
included do
|
|
|
|
def self.search_by_name(user, teams = [], query = nil, options = {})
|
|
|
|
return if user.blank? || teams.blank?
|
2019-04-09 15:08:27 +08:00
|
|
|
|
2020-09-15 21:47:46 +08:00
|
|
|
sql_q = viewable_by_user(user, teams)
|
2020-09-17 19:15:40 +08: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 21:47:46 +08:00
|
|
|
end
|
2020-09-17 19:15:40 +08:00
|
|
|
|
2020-09-15 21:47:46 +08:00
|
|
|
sql_q.limit(Constants::SEARCH_LIMIT)
|
|
|
|
end
|
|
|
|
|
2019-04-09 15:08:27 +08:00
|
|
|
def self.filter_by_teams(teams = [])
|
2021-07-23 17:56:28 +08:00
|
|
|
return self if teams.blank?
|
2020-09-15 21:47:46 +08:00
|
|
|
|
2019-04-09 15:08:27 +08:00
|
|
|
if column_names.include? 'team_id'
|
2020-09-15 21:47:46 +08:00
|
|
|
where(team_id: teams)
|
2019-04-09 15:08:27 +08:00
|
|
|
else
|
|
|
|
valid_subjects = Extends::ACTIVITY_SUBJECT_CHILDREN
|
|
|
|
parent_array = [to_s.underscore]
|
|
|
|
find_parent = true
|
|
|
|
# Trying to build parent array
|
|
|
|
while find_parent
|
|
|
|
possible_parent = valid_subjects.find { |_sub, ch| ((ch || []).include? parent_array[-1].pluralize.to_sym) }
|
|
|
|
possible_parent = { Project: nil } if parent_array[-1] == 'experiment'
|
|
|
|
if possible_parent
|
|
|
|
parent_array.push(possible_parent.flatten[0].to_s.underscore)
|
|
|
|
else
|
|
|
|
find_parent = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
parent_array.shift
|
|
|
|
# Now we will go from parent to child direction
|
|
|
|
last_parent = parent_array.reverse!.shift
|
|
|
|
query = last_parent.to_s.camelize.constantize.where(team_id: teams)
|
|
|
|
parent_array.each do |child|
|
|
|
|
query = child.to_s.camelize.constantize.where("#{last_parent}_id" => query)
|
|
|
|
last_parent = child
|
|
|
|
end
|
2020-09-15 21:47:46 +08:00
|
|
|
where("#{last_parent}_id" => query)
|
2019-04-09 15:08:27 +08:00
|
|
|
end
|
|
|
|
end
|
2019-02-26 18:01:15 +08:00
|
|
|
end
|
2020-09-17 19:15:40 +08:00
|
|
|
# rubocop:enable Metrics/BlockLength
|
2019-02-26 18:01:15 +08:00
|
|
|
end
|