From 6483780d33795447e24db2075a923a9fbfc88dda Mon Sep 17 00:00:00 2001 From: wandji20 Date: Thu, 4 Apr 2024 10:51:01 +0100 Subject: [PATCH] Add asset results to global search page [SCI-10475] --- app/controllers/search_controller.rb | 19 +++++- .../vue/global_search/groups/assets.vue | 54 +++++++++++++++- .../global_search/asset_serializer.rb | 61 +++++++++++++++++++ config/locales/en.yml | 5 ++ 4 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 app/serializers/global_search/asset_serializer.rb diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index b04412be8..2cc7326bf 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -89,6 +89,23 @@ class SearchController < ApplicationController next_page: (results.next_page if results.respond_to?(:next_page)) } return + when 'assets' + @asset_search_count = fetch_cached_count(Protocol) + search_assets + includes = [{ step: { protocol: { my_module: :experiment } } }, { result: { my_module: :experiment } }, :team] + results = if params[:preview] == 'true' + @asset_results.limit(Constants::GLOBAL_SEARCH_PREVIEW_LIMIT) + else + @asset_results.page(params[:page]).per(Constants::SEARCH_LIMIT) + end + + render json: results.includes(includes), + each_serializer: GlobalSearch::AssetSerializer, + meta: { + total: @search_count, + next_page: (results.next_page if results.respond_to?(:next_page)) + } + return end #@search_id = params[:search_id] ? params[:search_id] : generate_search_id @@ -395,7 +412,7 @@ class SearchController < ApplicationController end def search_assets - @asset_results = [] + @asset_results = Asset.none @asset_results = search_by_name(Asset) if @asset_search_count.positive? @search_count = @asset_search_count end diff --git a/app/javascript/vue/global_search/groups/assets.vue b/app/javascript/vue/global_search/groups/assets.vue index 7ce54c19c..cd6815673 100644 --- a/app/javascript/vue/global_search/groups/assets.vue +++ b/app/javascript/vue/global_search/groups/assets.vue @@ -1,14 +1,64 @@ diff --git a/app/serializers/global_search/asset_serializer.rb b/app/serializers/global_search/asset_serializer.rb new file mode 100644 index 000000000..751cc1037 --- /dev/null +++ b/app/serializers/global_search/asset_serializer.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +module GlobalSearch + class AssetSerializer < ActiveModel::Serializer + include Rails.application.routes.url_helpers + include FileIconsHelper + attributes :id, :file_name, :icon, :created_at, :updated_at, :team, :parent, :experiment + + def file_name + object.render_file_name + end + + def icon + file_fa_icon_class(object) + end + + def created_at + I18n.l(object.created_at, format: :full_date) + end + + def updated_at + I18n.l(object.updated_at, format: :full_date) + end + + def team + { name: object.team.name } + end + + def experiment + return { name: '' } unless object.my_module + + { + name: object.my_module.experiment.name, + url: my_modules_experiment_path(id: object.my_module.experiment.id, search: object.my_module.code) + } + end + + def parent + parent = object.parent + if parent.is_a?(Result) && object.my_module + parent_type = 'task' + parent_name = object.my_module.name + parent_url = my_module_results_path(my_module_id: object.my_module.id) + elsif parent.is_a?(Step) && object.my_module + parent_type = 'task' + parent_name = object.my_module.name + parent_url = protocols_my_module_path(object.my_module.id) + elsif parent.is_a?(Step) + parent_type = 'protocol_template' + parent_name = parent.protocol.name || I18n.t('search.index.untitled_protocol') + parent_url = protocol_path(parent.protocol_id) + elsif parent.is_a?(RepositoryCell) + parent_type = 'inventory_item' + parent_name = parent.repository_row.name + parent_url = repository_repository_rows_path(repository_id: parent.repository_row.repository_id) + end + + { name: parent_name, url: parent_url, type: parent_type } + end + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 175e9320c..9be16a9d7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -463,6 +463,11 @@ en: team: "Team" folder: "Folder" project: "Project" + experiment: "Experiment" + task: "Task" + untitled_protocol: 'Untitled protocol' + protocol_template: "Protocol template" + inventory_item: "Inventory item" comments: save_changes: "Save changes" empty_state: