scinote-web/app/utilities/smart_annotation.rb

71 lines
2.1 KiB
Ruby
Raw Normal View History

2020-09-09 20:17:19 +08:00
# frozen_string_literal: true
2017-01-05 17:52:00 +08:00
class SmartAnnotation
include ActionView::Helpers::SanitizeHelper
include ActionView::Helpers::TextHelper
2017-01-25 17:24:50 +08:00
attr_writer :current_user, :current_team, :query
2017-01-05 17:52:00 +08:00
2017-01-25 17:24:50 +08:00
def initialize(current_user, current_team, query)
2017-01-05 17:52:00 +08:00
@current_user = current_user
2017-01-25 17:24:50 +08:00
@current_team = current_team
2017-01-05 17:52:00 +08:00
@query = query
end
def my_modules
# Search tasks
2020-09-17 19:15:40 +08:00
MyModule.search_by_name(@current_user, @current_team, @query, intersect: true).active
2020-09-09 20:17:19 +08:00
.joins(experiment: :project)
.where(projects: { archived: false }, experiments: { archived: false })
2020-09-11 19:00:13 +08:00
.limit(Constants::ATWHO_SEARCH_LIMIT + 1)
2017-01-05 17:52:00 +08:00
end
def projects
# Search projects
2020-09-17 19:15:40 +08:00
Project.search_by_name(@current_user, @current_team, @query, intersect: true)
2020-09-09 20:17:19 +08:00
.where(archived: false)
2020-09-11 19:00:13 +08:00
.limit(Constants::ATWHO_SEARCH_LIMIT + 1)
2017-01-05 17:52:00 +08:00
end
def experiments
# Search experiments
2020-09-17 19:15:40 +08:00
Experiment.search_by_name(@current_user, @current_team, @query, intersect: true)
2020-09-09 20:17:19 +08:00
.joins(:project)
.where(projects: { archived: false }, experiments: { archived: false })
2020-09-11 19:00:13 +08:00
.limit(Constants::ATWHO_SEARCH_LIMIT + 1)
2017-01-05 17:52:00 +08:00
end
def repository_rows(repository)
res = RepositoryRow
.active
.where(repository: repository)
2020-09-17 19:15:40 +08:00
.search_by_name(@current_user, @current_team, @query, intersect: true)
2020-09-11 19:00:13 +08:00
.limit(Constants::ATWHO_SEARCH_LIMIT + 1)
rep_items_list = []
splitted_name = repository.name.gsub(/[^0-9a-z ]/i, '').split
repository_tag =
case splitted_name.length
when 1
splitted_name[0][0..2]
when 2
if splitted_name[0].length == 1
splitted_name[0][0] + splitted_name[1][0..1]
else
splitted_name[0][0..1] + splitted_name[1][0]
end
else
splitted_name[0][0] + splitted_name[1][0] + splitted_name[2][0]
end
repository_tag.downcase!
res.each do |rep_row|
rep_item = {}
2020-09-04 22:48:53 +08:00
rep_item[:id] = rep_row.id.base62_encode
rep_item[:name] = sanitize(rep_row.name)
rep_item[:repository_tag] = repository_tag
rep_items_list << rep_item
end
rep_items_list
end
2017-01-05 17:52:00 +08:00
end