2016-02-12 23:52:43 +08:00
|
|
|
class SearchController < ApplicationController
|
2017-06-28 21:44:18 +08:00
|
|
|
before_action :load_vars, only: :index
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
def index
|
2017-05-05 22:41:23 +08:00
|
|
|
redirect_to new_search_path unless @search_query
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
count_search_results
|
|
|
|
|
|
|
|
search_projects if @search_category == :projects
|
2016-07-26 16:55:27 +08:00
|
|
|
search_experiments if @search_category == :experiments
|
2016-02-12 23:52:43 +08:00
|
|
|
search_workflows if @search_category == :workflows
|
2016-07-21 19:11:15 +08:00
|
|
|
search_modules if @search_category == :modules
|
|
|
|
search_results if @search_category == :results
|
2016-02-12 23:52:43 +08:00
|
|
|
search_tags if @search_category == :tags
|
2016-07-21 19:11:15 +08:00
|
|
|
search_reports if @search_category == :reports
|
|
|
|
search_protocols if @search_category == :protocols
|
2016-02-12 23:52:43 +08:00
|
|
|
search_steps if @search_category == :steps
|
2016-07-21 19:11:15 +08:00
|
|
|
search_checklists if @search_category == :checklists
|
2016-02-12 23:52:43 +08:00
|
|
|
search_samples if @search_category == :samples
|
2017-06-23 22:06:11 +08:00
|
|
|
if @search_category == :repositories && params[:repository]
|
|
|
|
search_repository
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
search_assets if @search_category == :assets
|
|
|
|
search_tables if @search_category == :tables
|
2016-02-12 23:52:43 +08:00
|
|
|
search_comments if @search_category == :comments
|
|
|
|
|
2016-10-05 23:45:20 +08:00
|
|
|
@search_pages = (@search_count.to_f / Constants::SEARCH_LIMIT.to_f).ceil
|
2016-02-12 23:52:43 +08:00
|
|
|
@start_page = @search_page - 2
|
|
|
|
@start_page = 1 if @start_page < 1
|
|
|
|
@end_page = @start_page + 4
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
if @end_page > @search_pages
|
|
|
|
@end_page = @search_pages
|
|
|
|
@start_page = @end_page - 4
|
|
|
|
@start_page = 1 if @start_page < 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_vars
|
2017-05-09 00:00:14 +08:00
|
|
|
query = (params.fetch(:q) { '' }).strip
|
2016-02-12 23:52:43 +08:00
|
|
|
@search_category = params[:category] || ''
|
|
|
|
@search_category = @search_category.to_sym
|
|
|
|
@search_page = params[:page].to_i || 1
|
2017-05-05 22:41:23 +08:00
|
|
|
@search_case = params[:match_case] == 'true'
|
|
|
|
@search_whole_word = params[:whole_word] == 'true'
|
|
|
|
@search_whole_phrase = params[:whole_phrase] == 'true'
|
|
|
|
@display_query = query
|
|
|
|
|
|
|
|
if @search_whole_phrase || query.count(' ').zero?
|
|
|
|
if query.length < Constants::NAME_MIN_LENGTH
|
|
|
|
flash[:error] = t('general.query.length_too_short',
|
|
|
|
min_length: Constants::NAME_MIN_LENGTH)
|
|
|
|
redirect_to :back
|
|
|
|
elsif query.length > Constants::TEXT_MAX_LENGTH
|
|
|
|
flash[:error] = t('general.query.length_too_long',
|
|
|
|
max_length: Constants::TEXT_MAX_LENGTH)
|
|
|
|
redirect_to :back
|
|
|
|
else
|
|
|
|
@search_query = query
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# splits the search query to validate all entries
|
|
|
|
splited_query = query.split
|
2016-08-02 17:16:07 +08:00
|
|
|
@search_query = ''
|
2017-05-05 22:41:23 +08:00
|
|
|
splited_query.each_with_index do |w, i|
|
|
|
|
if w.length >= Constants::NAME_MIN_LENGTH &&
|
|
|
|
w.length <= Constants::TEXT_MAX_LENGTH
|
|
|
|
@search_query += "#{splited_query[i]} "
|
2016-10-05 23:45:20 +08:00
|
|
|
end
|
2016-07-29 22:32:54 +08:00
|
|
|
end
|
2017-05-05 22:41:23 +08:00
|
|
|
if @search_query.empty?
|
|
|
|
flash[:error] = t('general.query.wrong_query',
|
|
|
|
min_length: Constants::NAME_MIN_LENGTH,
|
|
|
|
max_length: Constants::TEXT_MAX_LENGTH)
|
|
|
|
redirect_to :back
|
|
|
|
else
|
|
|
|
@search_query.strip!
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
2016-08-18 19:14:31 +08:00
|
|
|
@search_page = 1 if @search_page < 1
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2016-07-26 16:55:27 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
protected
|
|
|
|
|
|
|
|
def search_by_name(model)
|
2017-05-05 22:41:23 +08:00
|
|
|
model.search(current_user,
|
|
|
|
true,
|
|
|
|
@search_query,
|
|
|
|
@search_page,
|
|
|
|
nil,
|
|
|
|
match_case: @search_case,
|
|
|
|
whole_word: @search_whole_word,
|
|
|
|
whole_phrase: @search_whole_phrase)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def count_by_name(model)
|
2017-05-17 15:08:40 +08:00
|
|
|
model.search(current_user,
|
|
|
|
true,
|
|
|
|
@search_query,
|
|
|
|
Constants::SEARCH_NO_LIMIT,
|
|
|
|
nil,
|
|
|
|
match_case: @search_case,
|
|
|
|
whole_word: @search_whole_word,
|
|
|
|
whole_phrase: @search_whole_phrase).size
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2017-06-23 22:06:11 +08:00
|
|
|
def count_by_repository
|
2017-06-27 19:23:51 +08:00
|
|
|
count_total = 0
|
2017-06-23 22:06:11 +08:00
|
|
|
search_results = Repository.search(current_user,
|
|
|
|
true,
|
|
|
|
@search_query,
|
|
|
|
Constants::SEARCH_NO_LIMIT,
|
|
|
|
nil,
|
|
|
|
match_case: @search_case,
|
|
|
|
whole_word: @search_whole_word,
|
|
|
|
whole_phrase: @search_whole_phrase)
|
2017-06-27 19:23:51 +08:00
|
|
|
@repository_search_count = {}
|
2017-06-23 22:06:11 +08:00
|
|
|
current_user.teams.includes(:repositories).each do |team|
|
|
|
|
team_results = {}
|
|
|
|
team_results[:count] = 0
|
|
|
|
team_results[:repositories] = {}
|
|
|
|
team.repositories.each do |repository|
|
|
|
|
repository_results = {}
|
|
|
|
repository_results[:id] = repository.id
|
|
|
|
repository_results[:count] = 0
|
|
|
|
search_results.each do |result|
|
|
|
|
if repository.id == result.id
|
2017-06-27 19:23:51 +08:00
|
|
|
count_total += result.counter
|
2017-06-23 22:06:11 +08:00
|
|
|
repository_results[:count] += result.counter
|
|
|
|
end
|
|
|
|
end
|
|
|
|
team_results[:repositories][repository.name] = repository_results
|
|
|
|
team_results[:count] += repository_results[:count]
|
|
|
|
end
|
2017-06-27 19:23:51 +08:00
|
|
|
@repository_search_count[team.name] = team_results
|
2017-06-23 22:06:11 +08:00
|
|
|
end
|
2017-06-27 19:23:51 +08:00
|
|
|
count_total
|
2017-06-23 22:06:11 +08:00
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def count_search_results
|
|
|
|
@project_search_count = count_by_name Project
|
2016-07-26 16:55:27 +08:00
|
|
|
@experiment_search_count = count_by_name Experiment
|
2016-02-12 23:52:43 +08:00
|
|
|
@workflow_search_count = count_by_name MyModuleGroup
|
2016-07-21 19:11:15 +08:00
|
|
|
@module_search_count = count_by_name MyModule
|
|
|
|
@result_search_count = count_by_name Result
|
2016-02-12 23:52:43 +08:00
|
|
|
@tag_search_count = count_by_name Tag
|
2016-07-21 19:11:15 +08:00
|
|
|
@report_search_count = count_by_name Report
|
|
|
|
@protocol_search_count = count_by_name Protocol
|
2016-02-12 23:52:43 +08:00
|
|
|
@step_search_count = count_by_name Step
|
2016-07-21 19:11:15 +08:00
|
|
|
@checklist_search_count = count_by_name Checklist
|
2016-02-12 23:52:43 +08:00
|
|
|
@sample_search_count = count_by_name Sample
|
2017-06-27 19:23:51 +08:00
|
|
|
@repository_search_count_total = count_by_repository
|
2016-07-21 19:11:15 +08:00
|
|
|
@asset_search_count = count_by_name Asset
|
|
|
|
@table_search_count = count_by_name Table
|
2016-02-12 23:52:43 +08:00
|
|
|
@comment_search_count = count_by_name Comment
|
|
|
|
|
|
|
|
@search_results_count = @project_search_count
|
2016-07-26 16:55:27 +08:00
|
|
|
@search_results_count += @experiment_search_count
|
2016-02-12 23:52:43 +08:00
|
|
|
@search_results_count += @workflow_search_count
|
2016-07-21 19:11:15 +08:00
|
|
|
@search_results_count += @module_search_count
|
|
|
|
@search_results_count += @result_search_count
|
2016-02-12 23:52:43 +08:00
|
|
|
@search_results_count += @tag_search_count
|
2016-07-21 19:11:15 +08:00
|
|
|
@search_results_count += @report_search_count
|
|
|
|
@search_results_count += @protocol_search_count
|
2016-02-12 23:52:43 +08:00
|
|
|
@search_results_count += @step_search_count
|
2016-07-21 19:11:15 +08:00
|
|
|
@search_results_count += @checklist_search_count
|
2016-02-12 23:52:43 +08:00
|
|
|
@search_results_count += @sample_search_count
|
2017-06-23 22:06:11 +08:00
|
|
|
@search_results_count += @repository_search_count_total
|
2016-07-21 19:11:15 +08:00
|
|
|
@search_results_count += @asset_search_count
|
|
|
|
@search_results_count += @table_search_count
|
2016-02-12 23:52:43 +08:00
|
|
|
@search_results_count += @comment_search_count
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_projects
|
|
|
|
@project_results = []
|
2017-05-05 22:41:23 +08:00
|
|
|
@project_results = search_by_name(Project) if @project_search_count > 0
|
2016-02-12 23:52:43 +08:00
|
|
|
@search_count = @project_search_count
|
|
|
|
end
|
|
|
|
|
2016-07-26 16:55:27 +08:00
|
|
|
def search_experiments
|
|
|
|
@experiment_results = []
|
2016-08-17 15:44:23 +08:00
|
|
|
if @experiment_search_count > 0
|
2017-05-05 22:41:23 +08:00
|
|
|
@experiment_results = search_by_name(Experiment)
|
2016-07-26 16:55:27 +08:00
|
|
|
end
|
|
|
|
@search_count = @experiment_search_count
|
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def search_workflows
|
|
|
|
@workflow_results = []
|
2016-08-17 15:44:23 +08:00
|
|
|
if @workflow_search_count > 0
|
2017-05-05 22:41:23 +08:00
|
|
|
@workflow_results = search_by_name(MyModuleGroup)
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
@search_count = @workflow_search_count
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def search_modules
|
|
|
|
@module_results = []
|
2017-05-05 22:41:23 +08:00
|
|
|
@module_results = search_by_name(MyModule) if @module_search_count > 0
|
2016-02-12 23:52:43 +08:00
|
|
|
@search_count = @module_search_count
|
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def search_results
|
|
|
|
@result_results = []
|
2017-05-05 22:41:23 +08:00
|
|
|
@result_results = search_by_name(Result) if @result_search_count > 0
|
2016-07-21 19:11:15 +08:00
|
|
|
@search_count = @result_search_count
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def search_tags
|
|
|
|
@tag_results = []
|
2017-05-05 22:41:23 +08:00
|
|
|
@tag_results = search_by_name(Tag) if @tag_search_count > 0
|
2016-02-12 23:52:43 +08:00
|
|
|
@search_count = @tag_search_count
|
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def search_reports
|
|
|
|
@report_results = []
|
2017-05-05 22:41:23 +08:00
|
|
|
@report_results = search_by_name(Report) if @report_search_count > 0
|
2016-07-21 19:11:15 +08:00
|
|
|
@search_count = @report_search_count
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_protocols
|
|
|
|
@protocol_results = []
|
2017-05-05 22:41:23 +08:00
|
|
|
@protocol_results = search_by_name(Protocol) if @protocol_search_count > 0
|
2016-07-21 19:11:15 +08:00
|
|
|
@search_count = @protocol_search_count
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def search_steps
|
|
|
|
@step_results = []
|
2017-05-05 22:41:23 +08:00
|
|
|
@step_results = search_by_name(Step) if @step_search_count > 0
|
2016-02-12 23:52:43 +08:00
|
|
|
@search_count = @step_search_count
|
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def search_checklists
|
|
|
|
@checklist_results = []
|
2016-08-17 15:44:23 +08:00
|
|
|
if @checklist_search_count > 0
|
2017-05-05 22:41:23 +08:00
|
|
|
@checklist_results = search_by_name(Checklist)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
@search_count = @checklist_search_count
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def search_samples
|
|
|
|
@sample_results = []
|
2017-05-05 22:41:23 +08:00
|
|
|
@sample_results = search_by_name(Sample) if @sample_search_count > 0
|
2016-02-12 23:52:43 +08:00
|
|
|
@search_count = @sample_search_count
|
|
|
|
end
|
|
|
|
|
2017-06-23 22:06:11 +08:00
|
|
|
def search_repository
|
|
|
|
@repository = Repository.find_by_id(params[:repository])
|
|
|
|
render_403 unless can_view_repository(@repository)
|
|
|
|
@repository_results = []
|
|
|
|
if @repository_search_count_total > 0
|
|
|
|
@repository_results =
|
|
|
|
RepositoryRow.search(@repository, @search_query, @search_page,
|
|
|
|
match_case: @search_case,
|
|
|
|
whole_word: @search_whole_word,
|
|
|
|
whole_phrase: @search_whole_phrase)
|
|
|
|
end
|
|
|
|
@search_count = @repository_search_count_total
|
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def search_assets
|
|
|
|
@asset_results = []
|
2017-05-05 22:41:23 +08:00
|
|
|
@asset_results = search_by_name(Asset) if @asset_search_count > 0
|
2016-07-21 19:11:15 +08:00
|
|
|
@search_count = @asset_search_count
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_tables
|
|
|
|
@table_results = []
|
2017-05-05 22:41:23 +08:00
|
|
|
@table_results = search_by_name(Table) if @table_search_count > 0
|
2016-07-21 19:11:15 +08:00
|
|
|
@search_count = @table_search_count
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def search_comments
|
|
|
|
@comment_results = []
|
2017-05-05 22:41:23 +08:00
|
|
|
@comment_results = search_by_name(Comment) if @comment_search_count > 0
|
2016-02-12 23:52:43 +08:00
|
|
|
@search_count = @comment_search_count
|
|
|
|
end
|
|
|
|
end
|