Add project folders to global search [SCI-5180]

This commit is contained in:
Oleksii Kriuchykhin 2020-11-10 15:04:35 +01:00
parent f5860397f5
commit 86e5078622
6 changed files with 107 additions and 33 deletions

View file

@ -10,6 +10,7 @@ class SearchController < ApplicationController
count_search_results
search_projects if @search_category == :projects
search_project_folders if @search_category == :project_folders
search_experiments if @search_category == :experiments
search_modules if @search_category == :modules
search_results if @search_category == :results
@ -166,6 +167,7 @@ class SearchController < ApplicationController
def count_search_results
@project_search_count = fetch_cached_count Project
@project_folder_search_count = fetch_cached_count ProjectFolder
@experiment_search_count = fetch_cached_count Experiment
@module_search_count = fetch_cached_count MyModule
@result_search_count = fetch_cached_count Result
@ -180,6 +182,7 @@ class SearchController < ApplicationController
@comment_search_count = fetch_cached_count Comment
@search_results_count = @project_search_count
@search_results_count += @project_folder_search_count
@search_results_count += @experiment_search_count
@search_results_count += @module_search_count
@search_results_count += @result_search_count
@ -205,69 +208,71 @@ class SearchController < ApplicationController
def search_projects
@project_results = []
@project_results = search_by_name(Project) if @project_search_count > 0
@project_results = search_by_name(Project) if @project_search_count.positive?
@search_count = @project_search_count
end
def search_project_folders
@project_folder_results = []
@project_folder_results = search_by_name(ProjectFolder) if @project_folder_search_count.positive?
@search_count = @project_folder_search_count
end
def search_experiments
@experiment_results = []
if @experiment_search_count > 0
@experiment_results = search_by_name(Experiment)
end
@experiment_results = search_by_name(Experiment) if @experiment_search_count.positive?
@search_count = @experiment_search_count
end
def search_modules
@module_results = []
@module_results = search_by_name(MyModule) if @module_search_count > 0
@module_results = search_by_name(MyModule) if @module_search_count.positive?
@search_count = @module_search_count
end
def search_results
@result_results = []
@result_results = search_by_name(Result) if @result_search_count > 0
@result_results = search_by_name(Result) if @result_search_count.positive?
@search_count = @result_search_count
end
def search_tags
@tag_results = []
@tag_results = search_by_name(Tag) if @tag_search_count > 0
@tag_results = search_by_name(Tag) if @tag_search_count.positive?
@search_count = @tag_search_count
end
def search_reports
@report_results = []
@report_results = search_by_name(Report) if @report_search_count > 0
@report_results = search_by_name(Report) if @report_search_count.positive?
@search_count = @report_search_count
end
def search_protocols
@protocol_results = []
@protocol_results = search_by_name(Protocol) if @protocol_search_count > 0
@protocol_results = search_by_name(Protocol) if @protocol_search_count.positive?
@search_count = @protocol_search_count
end
def search_steps
@step_results = []
@step_results = search_by_name(Step) if @step_search_count > 0
@step_results = search_by_name(Step) if @step_search_count.positive?
@search_count = @step_search_count
end
def search_checklists
@checklist_results = []
if @checklist_search_count > 0
@checklist_results = search_by_name(Checklist)
end
@checklist_results = search_by_name(Checklist) if @checklist_search_count.positive?
@search_count = @checklist_search_count
end
def search_repository
@repository = Repository.find_by_id(params[:repository])
@repository = Repository.find_by(id: params[:repository])
unless current_user.teams.include?(@repository.team) || @repository.private_shared_with?(current_user.teams)
render_403
end
@repository_results = []
if @repository_search_count_total > 0
if @repository_search_count_total.positive?
@repository_results =
Repository.search(current_user, @search_query, @search_page,
@repository,
@ -280,19 +285,19 @@ class SearchController < ApplicationController
def search_assets
@asset_results = []
@asset_results = search_by_name(Asset) if @asset_search_count > 0
@asset_results = search_by_name(Asset) if @asset_search_count.positive?
@search_count = @asset_search_count
end
def search_tables
@table_results = []
@table_results = search_by_name(Table) if @table_search_count > 0
@table_results = search_by_name(Table) if @table_search_count.positive?
@search_count = @table_search_count
end
def search_comments
@comment_results = []
@comment_results = search_by_name(Comment) if @comment_search_count > 0
@comment_results = search_by_name(Comment) if @comment_search_count.positive?
@search_count = @comment_search_count
end
end

View file

@ -9,7 +9,7 @@ class ProjectFolder < ApplicationRecord
maximum: Constants::NAME_MAX_LENGTH },
uniqueness: { scope: :team_id, case_sensitive: false }
before_validation :inherit_team_from_parent_folder, if: -> { parent_folder.present? }
before_validation :inherit_team_from_parent_folder, if: -> { team.blank? && parent_folder.present? }
belongs_to :team, inverse_of: :project_folders, touch: true
belongs_to :parent_folder, class_name: 'ProjectFolder', optional: true
@ -18,6 +18,23 @@ class ProjectFolder < ApplicationRecord
scope :top_level, -> { where(parent_folder: nil) }
def self.search(user, _include_archived, query = nil, page = 1, current_team = nil, options = {})
new_query = if current_team
current_team.project_folders.where_attributes_like(:name, query, options)
else
distinct.joins(team: :user_teams)
.where(teams: { user_teams: { user: user } })
.where_attributes_like('project_folders.name', query, options)
end
# Show all results if needed
if page == Constants::SEARCH_NO_LIMIT
new_query
else
new_query.limit(Constants::SEARCH_LIMIT).offset((page - 1) * Constants::SEARCH_LIMIT)
end
end
def inner_projects
project_folders.map do |inner_folder|
projects + inner_folder.inner_projects

View file

@ -59,6 +59,20 @@
<%= t'Projects' %>
</a>
</li>
<li role="presentation"
class="
<%= "active" if @search_category.present? and @search_category == :project_folders %>
<%= "disabled" if @project_folder_search_count == 0 %>"
>
<a href="?<%= { category: 'project_folders', q: @search_query,
whole_word: @search_whole_word, whole_phrase: @search_whole_phrase,
match_case: @search_case, utf8: '✓',
search_id: @search_id }.to_query %>">
<span class="badge pull-right"><%= @project_folder_search_count %></span>
<span class="fas fa-folder"></span>
<%= t('ProjectFolders') %>
</a>
</li>
<li role="presentation"
class="
<%= "active" if @search_category.present? and @search_category == :experiments %>
@ -216,7 +230,7 @@
</li>
<% @repository_search_count.each do |team, results| %>
<li class="repositories-team <%= 'active' if results[:count] > 0 %>">
<li class="repositories-team <%= 'active' if results[:count].positive? %>">
<i class="fas fa-list-alt"></i>
<%= t('Repositories_team', team: team) %>
</li>
@ -251,43 +265,46 @@
<hr class="visible-xs">
<% if @search_category == :projects and @project_search_count > 0 %>
<% if @search_category == :projects and @project_search_count.positive? %>
<%= render 'search/results/projects', search_query: @search_query, results: @project_results %>
<% end %>
<% if @search_category == :experiments and @experiment_search_count > 0 %>
<% if @search_category == :project_folders and @project_folder_search_count.positive? %>
<%= render 'search/results/project_folders', search_query: @search_query, results: @project_folder_results %>
<% end %>
<% if @search_category == :experiments and @experiment_search_count.positive? %>
<%= render 'search/results/experiments', search_query: @search_query, results: @experiment_results %>
<% end %>
<% if @search_category == :modules and @module_search_count > 0 %>
<% if @search_category == :modules and @module_search_count.positive? %>
<%= render 'search/results/modules', search_query: @search_query, results: @module_results %>
<% end %>
<% if @search_category == :results and @result_search_count > 0 %>
<% if @search_category == :results and @result_search_count.positive? %>
<%= render 'search/results/results', search_query: @search_query, results: @result_results %>
<% end %>
<% if @search_category == :tags and @tag_search_count > 0 %>
<% if @search_category == :tags and @tag_search_count.positive? %>
<%= render 'search/results/tags', search_query: @search_query, results: @tag_results %>
<% end %>
<% if @search_category == :reports and @report_search_count > 0 %>
<% if @search_category == :reports and @report_search_count.positive? %>
<%= render 'search/results/reports', search_query: @search_query, results: @report_results %>
<% end %>
<% if @search_category == :protocols and @protocol_search_count > 0 %>
<% if @search_category == :protocols and @protocol_search_count.positive? %>
<%= render 'search/results/protocols', search_query: @search_query, results: @protocol_results %>
<% end %>
<% if @search_category == :steps and @step_search_count > 0 %>
<% if @search_category == :steps and @step_search_count.positive? %>
<%= render 'search/results/steps', search_query: @search_query, results: @step_results %>
<% end %>
<% if @search_category == :checklists and @checklist_search_count > 0 %>
<% if @search_category == :checklists and @checklist_search_count.positive? %>
<%= render 'search/results/checklists', search_query: @search_query, results: @checklist_results %>
<% end %>
<% if @search_category == :repositories and @repository_search_count_total > 0 %>
<% if @search_category == :repositories and @repository_search_count_total.positive? %>
<%= render 'search/results/repositories', search_query: @search_query, results: @repository_results, repository: @repository %>
<% end %>
<% if @search_category == :assets and @asset_search_count > 0 %>
<% if @search_category == :assets and @asset_search_count.positive? %>
<%= render 'search/results/assets', search_query: @search_query, results: @asset_results %>
<% end %>
<% if @search_category == :tables and @table_search_count > 0 %>
<% if @search_category == :tables and @table_search_count.positive? %>
<%= render 'search/results/tables', search_query: @search_query, results: @table_results %>
<% end %>
<% if @search_category == :comments and @comment_search_count > 0 %>
<% if @search_category == :comments and @comment_search_count.positive? %>
<%= render 'search/results/comments', search_query: @search_query, results: @comment_results %>
<% end %>

View file

@ -0,0 +1,22 @@
<% results.each do |project_folder| %>
<h5>
<span class="fas fa-folder"></span>
<%= render partial: 'search/results/partials/project_folder_text.html.erb',
locals: { project_folder: project_folder, query: search_query, link_to_page: :show } %>
</h5>
<p>
<span>
<%= t('search.index.created_at') %>
<%= l(project_folder.created_at, format: :full) %>
</span>
<br>
<span>
<%= t('search.index.team') %>
<%= render partial: 'search/results/partials/team_text.html.erb', locals: { team: project_folder.team } %>
</span>
<span data-hook="search-result-project-folder"></span>
</p>
<hr>
<% end %>

View file

@ -0,0 +1,12 @@
<% query ||= nil %>
<% text = query.present? ? highlight(project_folder.name, query.strip.split(/\s+/)) : project_folder.name %>
<% if can_read_team?(project_folder.team) %>
<% if link_to_page == :show %>
<%= route_to_other_team(projects_path, project_folder.team, text) %>
<% else %>
<%= route_to_other_team(projects_path, project_folder.team, text) %>
<% end %>
<% else %>
<%= text %>
<% end %>

View file

@ -2316,6 +2316,7 @@ en:
Modules: "Tasks"
Project: "Project"
Projects: "Projects"
ProjectFolders: "Folders"
Experiments: "Experiments"
Result: "Result"
Results: "Results"