2016-02-12 23:52:43 +08:00
|
|
|
class SearchController < ApplicationController
|
|
|
|
before_filter :load_vars, only: :index
|
2016-07-21 19:11:15 +08:00
|
|
|
before_filter :load_markdown, only: :index
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
MIN_QUERY_CHARS = 2
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
if not @search_query
|
|
|
|
redirect_to new_search_path
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
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
|
|
|
|
|
|
|
|
@search_pages = (@search_count.to_f / SEARCH_LIMIT.to_f).ceil
|
|
|
|
@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
|
|
|
|
@search_query = params[:q] || ''
|
|
|
|
@search_category = params[:category] || ''
|
|
|
|
@search_category = @search_category.to_sym
|
|
|
|
@search_page = params[:page].to_i || 1
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
error = false
|
|
|
|
@search_query.split().each do |w|
|
|
|
|
if w.length < MIN_QUERY_CHARS
|
|
|
|
error = true
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
if error
|
|
|
|
flash[:error] = t'search.index.error.query_length', n: MIN_QUERY_CHARS
|
|
|
|
redirect_to :back
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
if @search_page < 1
|
|
|
|
@search_page = 1
|
|
|
|
end
|
|
|
|
end
|
2016-07-26 16:55:27 +08:00
|
|
|
|
|
|
|
# Initialize markdown parser
|
2016-07-21 19:11:15 +08:00
|
|
|
def load_markdown
|
|
|
|
if @search_category == :results
|
|
|
|
@markdown = Redcarpet::Markdown.new(
|
|
|
|
Redcarpet::Render::HTML.new(
|
|
|
|
filter_html: true,
|
|
|
|
no_images: true
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def search_by_name(model)
|
|
|
|
model.search(current_user, true, @search_query, @search_page)
|
|
|
|
end
|
|
|
|
|
|
|
|
def count_by_name(model)
|
|
|
|
search_by_name(model).limit(nil).offset(nil).size
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
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
|
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 = []
|
|
|
|
if @project_search_count > 0 then
|
|
|
|
@project_results = search_by_name Project
|
|
|
|
end
|
|
|
|
@search_count = @project_search_count
|
|
|
|
end
|
|
|
|
|
2016-07-26 16:55:27 +08:00
|
|
|
def search_experiments
|
|
|
|
@experiment_results = []
|
|
|
|
if @experiment_search_count > 0 then
|
|
|
|
@experiment_results = search_by_name Experiment
|
|
|
|
end
|
|
|
|
@search_count = @experiment_search_count
|
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def search_workflows
|
|
|
|
@workflow_results = []
|
|
|
|
if @workflow_search_count > 0 then
|
|
|
|
@workflow_results = search_by_name MyModuleGroup
|
|
|
|
end
|
|
|
|
@search_count = @workflow_search_count
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def search_modules
|
|
|
|
@module_results = []
|
|
|
|
if @module_search_count > 0 then
|
|
|
|
@module_results = search_by_name MyModule
|
|
|
|
end
|
|
|
|
@search_count = @module_search_count
|
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def search_results
|
|
|
|
@result_results = []
|
|
|
|
if @result_search_count > 0 then
|
|
|
|
@result_results = search_by_name Result
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
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 = []
|
|
|
|
if @tag_search_count > 0 then
|
|
|
|
@tag_results = search_by_name Tag
|
|
|
|
end
|
|
|
|
@search_count = @tag_search_count
|
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def search_reports
|
|
|
|
@report_results = []
|
|
|
|
if @report_search_count > 0 then
|
|
|
|
@report_results = search_by_name Report
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
@search_count = @report_search_count
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_protocols
|
|
|
|
@protocol_results = []
|
|
|
|
if @protocol_search_count > 0 then
|
|
|
|
@protocol_results = search_by_name Protocol
|
|
|
|
end
|
|
|
|
@search_count = @protocol_search_count
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def search_steps
|
|
|
|
@step_results = []
|
|
|
|
if @step_search_count > 0 then
|
|
|
|
@step_results = search_by_name Step
|
|
|
|
end
|
|
|
|
@search_count = @step_search_count
|
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def search_checklists
|
|
|
|
@checklist_results = []
|
|
|
|
if @checklist_search_count > 0 then
|
|
|
|
@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 = []
|
|
|
|
if @sample_search_count > 0 then
|
|
|
|
@sample_results = search_by_name Sample
|
|
|
|
end
|
|
|
|
@search_count = @sample_search_count
|
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def search_assets
|
|
|
|
@asset_results = []
|
|
|
|
if @asset_search_count > 0 then
|
|
|
|
@asset_results = search_by_name Asset
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
@search_count = @asset_search_count
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_tables
|
|
|
|
@table_results = []
|
|
|
|
if @table_search_count > 0 then
|
|
|
|
@table_results = search_by_name Table
|
|
|
|
end
|
|
|
|
@search_count = @table_search_count
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def search_comments
|
|
|
|
@comment_results = []
|
|
|
|
if @comment_search_count > 0 then
|
|
|
|
@comment_results = search_by_name Comment
|
|
|
|
end
|
|
|
|
@search_count = @comment_search_count
|
|
|
|
end
|
|
|
|
end
|